static void MojoInput_file_close(MojoInput *io) { MojoInputFileInstance *inst = (MojoInputFileInstance *) io->opaque; MojoPlatform_close(inst->handle); free(inst->path); free(inst); free(io); } // MojoInput_file_close
void MojoLog_deinitLogging(void) { if (logFile != NULL) { MojoPlatform_close(logFile); logFile = NULL; } // if } // MojoLog_deinitLogging
// !!! FIXME: I'd rather not use a callback here, but I can't see a cleaner // !!! FIXME: way right now... boolean MojoInput_toPhysicalFile(MojoInput *in, const char *fname, uint16 perms, MojoChecksums *checksums, int64 maxbytes, MojoInput_FileCopyCallback cb, void *data) { boolean retval = false; uint32 start = MojoPlatform_ticks(); void *out = NULL; boolean iofailure = false; int64 flen = 0; int64 bw = 0; MojoChecksumContext sumctx; if (in == NULL) return false; if (checksums != NULL) { memset(checksums, '\0', sizeof (MojoChecksums)); MojoChecksum_init(&sumctx); } // if // Wait for a ready(), so length() can be meaningful on network streams. while ((!in->ready(in)) && (!iofailure)) { MojoPlatform_sleep(100); if (cb != NULL) { if (!cb(MojoPlatform_ticks() - start, 0, 0, -1, data)) iofailure = true; } // if } // while flen = in->length(in); if ((maxbytes >= 0) && (flen > maxbytes)) flen = maxbytes; MojoPlatform_unlink(fname); if (!iofailure) { const uint32 flags = MOJOFILE_WRITE|MOJOFILE_CREATE|MOJOFILE_TRUNCATE; const uint16 mode = MojoPlatform_defaultFilePerms(); out = MojoPlatform_open(fname, flags, mode); } // if if (out != NULL) { while (!iofailure) { int64 br = 0; int64 maxread = sizeof (scratchbuf_128k); // see if we need to clamp to eof or maxbytes... if (flen >= 0) { const int64 avail = flen - bw; if (avail < maxread) { maxread = avail; if (maxread == 0) break; // nothing left to do, break out. } // if } // if // If there's a callback, then poll. Otherwise, just block on // the reads from the MojoInput. if ((cb != NULL) && (!in->ready(in))) MojoPlatform_sleep(100); else { br = in->read(in, scratchbuf_128k, (uint32) maxread); if (br == 0) // we're done! break; else if (br < 0) iofailure = true; else { if (MojoPlatform_write(out, scratchbuf_128k, (uint32) br) != br) iofailure = true; else { if (checksums != NULL) MojoChecksum_append(&sumctx, scratchbuf_128k, (uint32) br); bw += br; } // else } // else } // else if (cb != NULL) { if (!cb(MojoPlatform_ticks() - start, br, bw, flen, data)) iofailure = true; } // if } // while if (MojoPlatform_close(out) != 0) iofailure = true; else if (bw != flen) iofailure = true; if (iofailure) MojoPlatform_unlink(fname); else { MojoPlatform_chmod(fname, perms); if (checksums != NULL) MojoChecksum_finish(&sumctx, checksums); retval = true; } // else } // if in->close(in); return retval; } // MojoInput_toPhysicalFile