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
static const MojoArchiveEntry *MojoArchive_uz2_enumNext(MojoArchive *ar) { UZ2info *info = (UZ2info *) ar->opaque; MojoArchive_resetEntry(&ar->prevEnum); if (info->enumerated) return NULL; // only one file in this "archive". ar->prevEnum.perms = MojoPlatform_defaultFilePerms(); ar->prevEnum.filesize = info->outsize; ar->prevEnum.filename = xstrdup(info->outname); ar->prevEnum.type = MOJOARCHIVE_ENTRY_FILE; info->enumerated = true; return &ar->prevEnum; } // MojoArchive_uz2_enumNext
// !!! 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
static boolean MojoArchive_pck_enumerate(MojoArchive *ar) { MojoArchiveEntry *archiveEntries = NULL; PCKinfo *info = (PCKinfo *) ar->opaque; const int dataStart = info->dataStart; const int fileCount = dataStart / sizeof (PCKentry); const size_t len = fileCount * sizeof (MojoArchiveEntry); PCKentry fileEntry; uint64 i, realFileCount = 0; char directory[256] = {'\0'}; MojoInput *io = ar->io; MojoArchive_resetEntry(&ar->prevEnum); archiveEntries = (MojoArchiveEntry *) xmalloc(len); for (i = 0; i < fileCount; i++) { int dotdot; int64 br; br = io->read(io, fileEntry.filename, sizeof (fileEntry.filename)); if (br != sizeof (fileEntry.filename)) return false; else if (!MojoInput_readui32(io, &fileEntry.filesize)) return false; dotdot = (strcmp(fileEntry.filename, "..") == 0); if ((!dotdot) && (fileEntry.filesize == 0x80000000)) { MojoArchiveEntry *entry = &archiveEntries[realFileCount]; strcat(directory, fileEntry.filename); strcat(directory, "/"); entry->filename = xstrdup(directory); entry->type = MOJOARCHIVE_ENTRY_DIR; entry->perms = MojoPlatform_defaultDirPerms(); entry->filesize = 0; realFileCount++; } // if else if ((dotdot) && (fileEntry.filesize == 0x80000000)) { // remove trailing path separator char *pathSep; const size_t strLength = strlen(directory); directory[strLength - 1] = '\0'; pathSep = strrchr(directory, '/'); if(pathSep != NULL) { pathSep++; *pathSep = '\0'; } // if } // else if else { MojoArchiveEntry *entry = &archiveEntries[realFileCount]; if (directory[0] == '\0') entry->filename = xstrdup(fileEntry.filename); else { const size_t len = sizeof (char) * strlen(directory) + strlen(fileEntry.filename) + 1; entry->filename = (char *) xmalloc(len); strcat(entry->filename, directory); strcat(entry->filename, fileEntry.filename); } // else entry->perms = MojoPlatform_defaultFilePerms(); entry->type = MOJOARCHIVE_ENTRY_FILE; entry->filesize = fileEntry.filesize; realFileCount++; } // else } // for info->fileCount = realFileCount; info->archiveEntries = archiveEntries; info->nextEnumPos = 0; info->nextFileStart = dataStart; return true; } // MojoArchive_pck_enumerate