MojoInput *MojoInput_newFromFile(const char *path) { MojoInput *io = NULL; void *f = NULL; f = MojoPlatform_open(path, MOJOFILE_READ, 0); if (f != NULL) { MojoInputFileInstance *inst; inst = (MojoInputFileInstance *) xmalloc(sizeof (MojoInputFileInstance)); inst->path = xstrdup(path); inst->handle = f; io = (MojoInput *) xmalloc(sizeof (MojoInput)); io->ready = MojoInput_file_ready; io->read = MojoInput_file_read; io->seek = MojoInput_file_seek; io->tell = MojoInput_file_tell; io->length = MojoInput_file_length; io->duplicate = MojoInput_file_duplicate; io->close = MojoInput_file_close; io->opaque = inst; } // if return io; } // MojoInput_newFromFile
void MojoLog_initLogging(void) { const char *level = cmdlinestr("loglevel","MOJOSETUP_LOGLEVEL", DEFLOGLEV); const char *fname = cmdlinestr("log", "MOJOSETUP_LOG", NULL); if (strcmp(level, "nothing") == 0) MojoLog_logLevel = MOJOSETUP_LOG_NOTHING; else if (strcmp(level, "errors") == 0) MojoLog_logLevel = MOJOSETUP_LOG_ERRORS; else if (strcmp(level, "warnings") == 0) MojoLog_logLevel = MOJOSETUP_LOG_WARNINGS; else if (strcmp(level, "info") == 0) MojoLog_logLevel = MOJOSETUP_LOG_INFO; else if (strcmp(level, "debug") == 0) MojoLog_logLevel = MOJOSETUP_LOG_DEBUG; else // Unknown string gets everything...that'll teach you. MojoLog_logLevel = MOJOSETUP_LOG_EVERYTHING; if ((fname != NULL) && (strcmp(fname, "-") == 0)) logFile = MojoPlatform_stdout(); else if (fname != NULL) { const uint32 flags = MOJOFILE_WRITE|MOJOFILE_CREATE|MOJOFILE_TRUNCATE; const uint16 mode = MojoPlatform_defaultFilePerms(); logFile = MojoPlatform_open(fname, flags, mode); } // if } // MojoLog_initLogging
// !!! 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