rc_t vds_diff( const char * f1, const char * f2 ) { KDirectory * dir; rc_t rc = KDirectoryNativeDir( &dir ); if ( rc != 0 ) KOutMsg( "KDirectoryNativeDir() failed\n" ); else { const KFile * kf1; rc = KDirectoryOpenFileRead ( dir, &kf1, "%s", f1 ); if ( rc != 0 ) KOutMsg( "cannot open file '%s'\n", f1 ); else { const KFile * kf2; rc = KDirectoryOpenFileRead ( dir, &kf2, "%s", f2 ); if ( rc != 0 ) KOutMsg( "cannot open file '%s'\n", f2 ); else { rc = vds_diff_files( kf1, kf2 ); KFileRelease( kf2 ); } KFileRelease( kf1 ); } KDirectoryRelease( dir ); } clear_recorded_errors(); return rc; }
/* Open */ rc_t KColumnIdx2OpenRead ( KColumnIdx2 *self, const KDirectory *dir, uint64_t eof ) { rc_t rc = KDirectoryOpenFileRead ( dir, ( const KFile** ) & self -> f, "idx2" ); #if IDX2_READ_FILE_BUFFER if ( rc == 0 ) { KFile * orig = self -> f; rc = KBufFileMakeRead ( ( const KFile** ) & self -> f, self -> f, eof + 1 ); if ( rc == 0 ) { KFileRelease ( orig ); } else { self -> f = orig; rc = 0; } } #endif if ( rc == 0 ) rc = KColumnIdx2Init ( self, eof ); return rc; }
LIB_EXPORT rc_t CC LoadFileByNameToNameList( VNamelist * namelist, const char * filename ) { rc_t rc; if ( namelist == NULL || filename == NULL ) rc = RC( rcFS, rcFile, rcValidating, rcParam, rcNull ); else { KDirectory * dir; rc = KDirectoryNativeDir ( &dir ); if ( rc == 0 ) { KFile const * f; rc = KDirectoryOpenFileRead ( dir, &f, "%s", filename ); if ( rc == 0 ) { if ( rc == 0 ) rc = LoadFromFile( f, namelist ); KFileRelease ( f ); } KDirectoryRelease ( dir ); } } return rc; }
static rc_t prepare_decompress( const compress_context * ctx, KDirectory *dir, const KFile **src, KFile **dst ) { const KFile *temp = NULL; rc_t rc = KDirectoryCreateFile ( dir, dst, false, 0664, kcmInit, ctx->dst_file ); DISP_RC( rc, "KDirectoryCreateFile() failed" ); if ( rc != 0 ) return rc; rc = KDirectoryOpenFileRead ( dir, &temp, ctx->src_file ); DISP_RC( rc, "KDirectoryOpenFileRead() failed" ); if ( rc == 0 ) switch( ctx->method ) { case KC_GZIP : rc = KFileMakeGzipForRead ( src, temp ); DISP_RC( rc, "KFileMakeGzipForRead() failed" ); break; case KC_BZIP : rc = KFileMakeBzip2ForRead ( src, temp ); DISP_RC( rc, "KFileMakeBzip2ForRead() failed" ); break; case KC_SZIP : /* rc = KFileMakeSzipForRead ( src, temp ); */ rc = RC( rcExe, rcFile, rcPacking, rcFormat, rcUnknown ); DISP_RC( rc, "KFileMakeSzip2ForRead() failed" ); break; } KFileRelease ( temp ); return rc; }
static rc_t XML_Open(const char* path, const FSNode** tree) { rc_t rc = 0; char errmsg[4096] = ""; KDirectory *dir = NULL; PLOGMSG(klogInfo, (klogInfo, "Reading XML file '$(x)'", PLOG_S(x), path)); if( (rc = KDirectoryNativeDir(&dir)) == 0 ) { const KFile* file = NULL; if( (rc = KDirectoryOpenFileRead(dir, &file, "%s", path)) == 0 ) { if( (rc = FSNode_Make((FSNode**)tree, "ROOT", &RootNode_vtbl)) == 0 ) { const KXMLDoc* xmldoc = NULL; if( (rc = KXMLMgrMakeDocRead(g_xmlmgr, &xmldoc, file)) == 0 ) { const KXMLNodeset* ns = NULL; if( (rc = KXMLDocOpenNodesetRead(xmldoc, &ns, "/FUSE/*")) == 0 ) { uint32_t count = 0; if( (rc = KXMLNodesetCount(ns, &count)) == 0 ) { if( count == 0 ) { rc = RC(rcExe, rcDoc, rcValidating, rcData, rcEmpty); } else { uint32_t i = 0; while(rc == 0 && i < count) { const KXMLNode* n = NULL; if( (rc = KXMLNodesetGetNodeRead(ns, &n, i++)) == 0 ) { SRAConfigFlags flags = ~0; errmsg[0] = '\0'; rc = XML_ValidateNode((FSNode*)*tree, n, flags, errmsg); ReleaseComplain(KXMLNodeRelease, n); } } if( rc == 0 ) { rc = SRAList_NextVersion(); } } } ReleaseComplain(KXMLNodesetRelease, ns); } ReleaseComplain(KXMLDocRelease, xmldoc); } if( rc != 0 ) { FSNode_Release(*tree); *tree = NULL; } } ReleaseComplain(KFileRelease, file); } ReleaseComplain(KDirectoryRelease, dir); } if( rc == 0 ) { PLOGMSG(klogInfo, (klogInfo, "XML file '$(x)' ok", PLOG_S(x), path)); } else { if( strlen(errmsg) < 1 ) { strcpy(errmsg, path); } LOGERR(klogErr, rc, errmsg); } return rc; }
static rc_t perform_file_load( const p_context ctx, KDirectory *dir, const TableWriterRefSeq* wr ) { rc_t rc = 0; uint32_t pt = KDirectoryPathType ( dir, "%s", ctx->src ); if ( pt != kptFile ) { rc = RC( rcExe, rcFile, rcConstructing, rcParam, rcInvalid ); LOGERR( klogErr, rc, "input-file not found" ); } else { const KFile * f; rc = KDirectoryOpenFileRead ( dir, &f, "%s", ctx->src ); if ( rc != 0 ) { LOGERR( klogErr, rc, "cannot open input-file for read" ); } else { data_ctx dctx; rc = init_data_context( ctx, wr, &dctx ); if ( rc == 0 ) { uint64_t pos = 0; bool done = false; while ( !done ) { size_t num_read; char buffer[ 8192 ]; rc = KFileReadAll ( f, pos, buffer, sizeof buffer, &num_read ); if ( rc != 0 ) { PLOGERR( klogErr, ( klogErr, rc, "$(path) at $(pos)", "path=%s,pos=%lu", ctx->src, pos ) ); done = true; } else { on_curl_data( buffer, 1, num_read, &dctx ); if ( num_read < sizeof( buffer ) ) done = true; else pos += num_read; } } finish_data_context( ctx, &dctx ); if ( dctx.rc == 0 ) { dctx.rc = rc; } } KFileRelease( f ); } } return rc; }
static rc_t KLoaderFile_Open(KLoaderFile* self) { rc_t rc = 0; DBG(("%s opening %s\n", __func__, self->realname)); if( (rc = KDirectoryOpenFileRead(self->dir, &self->kfile, "%s", self->realname)) == 0 ) { if( self->has_md5 ) { const KFile *md5File = NULL; DBG(("%s opening as md5 wrapped %s\n", __func__, self->realname)); if( (rc = KFileMakeMD5Read(&md5File, self->file, self->md5_digest)) == 0) { self->file = md5File; } } if( rc == 0 ) { const KFile *z = NULL; switch(self->compress_type) { case compress_none: break; case compress_gzip: DBG(("%s opening as gzip wrapped %s\n", __func__, self->realname)); if( (rc = KFileMakeGzipForRead(&z, self->file)) == 0 ) { KFileRelease(self->file); self->file = z; } break; case compress_bzip2: DBG(("%s opening as bzip2 wrapped %s\n", __func__, self->realname)); if( (rc = KFileMakeBzip2ForRead(&z, self->file)) == 0 ) { KFileRelease(self->file); self->file = z; } break; default: rc = RC(rcApp, rcFile, rcOpening, rcType, rcUnexpected); break; } #if ! WINDOWS if( rc == 0 && self->ahead ) { const KFile *z = NULL; if( (rc = KQueueFileMakeRead(&z, self->pos, self->file, self->buffer_size * 10, self->buffer_size, 0)) == 0 ) { KFileRelease(self->file); self->file = z; } } #endif } } if( rc != 0 ) { PLOGERR(klogErr, (klogErr, rc, "opening $(file)", PLOG_S(file), self->filename)); KFileRelease(self->file); } return rc; }
static rc_t TarNode_Touch(const TarNode* cself) { rc_t rc = 0; if( cself->xml_path != NULL ) { KDirectory* dir = NULL; if( (rc = KDirectoryNativeDir(&dir)) == 0 ) { KTime_t dt; if( (rc = KDirectoryDate(dir, &dt, "%s", cself->xml_path)) == 0 ) { if( dt != cself->mtime ) { const KFile* kfile = NULL; DEBUG_MSG(8, ("%s: updating tar %s\n", __func__, cself->xml_path)); if( (rc = KDirectoryOpenFileRead(dir, &kfile, "%s", cself->xml_path)) == 0 ) { const KXMLMgr* xmlmgr; if( (rc = XML_MgrGet(&xmlmgr)) == 0 ) { const KXMLDoc* xmldoc = NULL; if( (rc = KXMLMgrMakeDocRead(xmlmgr, &xmldoc, kfile)) == 0 ) { const KXMLNodeset* ns = NULL; if( (rc = KXMLDocOpenNodesetRead(xmldoc, &ns, "/TAR")) == 0 ) { uint32_t count = 0; if( (rc = KXMLNodesetCount(ns, &count)) == 0 ) { if( count != 1 ) { rc = RC(rcExe, rcDoc, rcValidating, rcData, rcInvalid); } else { const KXMLNode* n = NULL; if( (rc = KXMLNodesetGetNodeRead(ns, &n, 0)) == 0 ) { char errmsg[4096]; const TarFileList* new_files; if( (rc = TarNode_MakeFileList(n, &new_files, errmsg, cself->rel_path, cself->node.name)) != 0 ) { LOGERR(klogErr, rc, errmsg); } else { TarFileList_Release(cself->files); ((TarNode*)cself)->files = new_files; ((TarNode*)cself)->mtime = dt; } ReleaseComplain(KXMLNodeRelease, n); } } } ReleaseComplain(KXMLNodesetRelease, ns); } ReleaseComplain(KXMLDocRelease, xmldoc); } } ReleaseComplain(KFileRelease, kfile); } } } ReleaseComplain(KDirectoryRelease, dir); } } return rc; }
static rc_t make_local_file( const KFile ** f, const char * path ) { KDirectory * dir; rc_t rc = KDirectoryNativeDir( &dir ); *f = NULL; if ( rc == 0 ) { rc = KDirectoryOpenFileRead( dir, f, "%s", path ); KDirectoryRelease( dir ); } return rc; }
static rc_t OpenFile(KFile const **kf, char const path[], char const base[]) { char fname[4096]; rc_t rc = PathWithBasePath(fname, sizeof(fname), path, base); if (rc == 0) { KDirectory *dir; rc = KDirectoryNativeDir(&dir); if (rc == 0) { rc = KDirectoryOpenFileRead(dir, kf, fname); KDirectoryRelease(dir); } } return rc; }
static rc_t XmlMetaInit(rc_t rc, XmlMeta* meta, const AppCtx* ctx, const char* xml) { const KFile* f = NULL; const KXMLDoc* doc = NULL; const KXMLNodeset* ns = NULL; const KXMLNode* node = NULL; const char path[] = "/Run"; memset(meta, 0, sizeof *meta); if (rc) { return rc; } if (rc == 0) { rc = KDirectoryOpenFileRead(ctx->dir, &f, xml); DISP_RC(rc, xml); } if (rc == 0) { rc = KXMLMgrMakeDocRead(ctx->x_mgr, &doc, f); DISP_RC(rc, xml); } if (rc == 0) { rc = KXMLDocOpenNodesetRead(doc, &ns, path); DISP_RC(rc, path); } if (rc == 0) { rc = KXMLNodesetGetNodeRead(ns, &node, 0); DISP_RC(rc, path); } if (rc == 0) { rc = ParseSraMetaNode(node, path, NULL, &meta->root); } if (rc == 0 && !meta->root.found) { meta->tr = calloc(1, sizeof *meta->tr); if (meta->tr == NULL) { rc = RC(rcExe, rcStorage, rcAllocating, rcMemory, rcExhausted); } else { BSTreeInit(meta->tr); rc = XmlMetaInitMembers(meta->tr, node); } } RELEASE(KXMLNode, node); RELEASE(KXMLNodeset, ns); RELEASE(KXMLDoc, doc); RELEASE(KFile, f); return rc; }
static uint64_t get_file_size( const char * path ) { uint64_t res = 0; KDirectory * dir; rc_t rc = KDirectoryNativeDir( &dir ); if ( rc == 0 ) { const KFile * f; rc = KDirectoryOpenFileRead( dir, &f, "%s", path ); if ( rc == 0 ) { KFileSize ( f, &res ); KFileRelease( f ); } KDirectoryRelease( dir ); } return res; }
rc_t OpenDatabase(KKeyRing* self) { rc_t rc; const KFile* enc_infile; assert(self); rc = KDirectoryOpenFileRead(self->wd, &enc_infile, "%s", self->path); if ( rc == 0) { rc_t rc2; const KFile* infile; KKey key; rc = KKeyInitRead(&key, kkeyAES256, self->passwd, string_measure(self->passwd, NULL)); if ( rc == 0 ) { rc = KEncFileMakeRead (&infile, enc_infile, &key); if (rc == 0) { const KDirectory* arc; rc = KDirectoryOpenArcDirRead_silent_preopened(self->wd, &arc, true, "/keyring", tocKFile, (void*)infile, KArcParseSRA, NULL, NULL); if (rc == 0) { /* Hack: we violate the KDirectory object interface in order for VDBManagerMakeUpdate to succeed, since it would refuse to open a read-only dir (i.e. archive); We will only read from the object, though. */ ((KDirectory*)arc)->read_only = false; rc = KeyRingDatabaseLoad(self->data, arc, "/keyring"); rc2 = KDirectoryRelease(arc); if (rc == 0) rc = rc2; } rc2 = KFileRelease(infile); if (rc == 0) rc = rc2; } } rc2 = KFileRelease(enc_infile); if (rc == 0) rc = rc2; } return rc; }
static rc_t CC _IsPathCart ( const struct KDirectory * Directory, const char * Path, const char * Name, char * BF, size_t BFS, bool * IsCart ) { rc_t RCt; size_t nwr; const struct KFile * File; const char * Sg = "ncbikart"; size_t SgLen; char SF [ 64 ]; RCt = 0; nwr = 0; File = NULL; SgLen = sizeof ( Sg ); * SF = 0; * BF = 0; * IsCart = false; RCt = string_printf ( BF, BFS, & nwr, "%s/%s", Path, Name ); if ( RCt == 0 ) { RCt = KDirectoryOpenFileRead ( Directory, & File, BF ); if ( RCt == 0 ) { RCt = KFileRead ( File, 0, SF, SgLen, & nwr ); if ( RCt == 0 ) { * IsCart = ! string_cmp ( SF, SgLen, Sg, SgLen, SgLen ); } KFileRelease ( File ); } } return RCt; } /* _IsPathCart () */
static rc_t SpotIteratorInit(struct SpotIterator* self, const SRATable* tbl, const char* redactFileName) { rc_t rc = 0; assert(self && tbl && redactFileName); memset(self, 0, sizeof *self); self->m_crnSpotId = 1; rc = SRATableMaxSpotId(tbl, &self->m_maxSpotId); if (rc != 0) { logerr(klogErr, rc, "while calling SRATableMaxSpotId"); } else { plogmsg(klogInfo, "MaxSpotId = $(spot)", PLOG_U32(spot), self->m_maxSpotId); } if (rc == 0) { rc = SpotIteratorInitDirectory(); } if (rc == 0) { self->m_filename = redactFileName; plogmsg(klogInfo, "Opening '$(path)'", "path=%s", self->m_filename); rc = KDirectoryOpenFileRead( __SpotIteratorDirectory, &self->m_file, "%s", self->m_filename); if (rc != 0) { plogerr(klogErr, rc, "while opening file '$(path)'", "path=%s", self->m_filename); } } if (rc == 0) { rc = SpotIteratorReadSpotToRedact(self); } return rc; }
rc_t CC TOpenPtFile ( struct KDirectory * current_dir, const char * path, TFileOpenMode mode, struct KFile ** out_file ) { rc_t rc; struct KFile * file; rc = 0; file = NULL; assert ( path ); assert ( out_file ); * out_file = NULL; switch (mode) { case TFileOpenMode_Read: rc = KDirectoryOpenFileRead ( current_dir, (const struct KFile **)& file, path ); break; case TFileOpenMode_Write: rc = KDirectoryOpenFileWrite ( current_dir, & file, false, path ); break; case TFileOpenMode_ReadWrite: rc = KDirectoryOpenFileWrite ( current_dir, & file, true, path ); break; default: assert(false); } if (rc == 0) { * out_file = file; } return rc; } /* TOpenPtFile () */
static rc_t perform_test( const KDirectory *dir, const char * filename, const uint64_t pos, const uint64_t pos2, const uint64_t count, const size_t queue_size, const size_t block_size, const size_t chunk_size ) { const KFile * f; rc_t rc = KDirectoryOpenFileRead ( dir, &f, filename ); if ( rc != 0 ) { OUTMSG(( "KDirectoryOpenFileRead( '%s' ) failed: %R\n", filename, rc )); } else { rc = perform_qtest( f, filename, pos, count, queue_size, block_size, chunk_size ); if ( rc == 0 && pos2 > 0 ) { rc = perform_qtest( f, filename, pos2, count, queue_size, block_size, chunk_size ); } KFileRelease( f ); } return rc; }
static rc_t SpotIteratorInit(const char* redactFileName, const Db* db, SpotIterator* self) { rc_t rc = 0; int64_t first = 0; assert(self && db); memset(self, 0, sizeof *self); self->crnSpotId = 1; rc = VCursorIdRange (db->rCursor, db->rFilterIdx, &first, &self->maxSpotId); DISP_RC(rc, "while calling VCursorIdRange"); self->spotToReduct = first - 1; if (rc == 0) { rc = SpotIteratorInitDirectory(); } if (rc == 0) { self->filename = redactFileName; rc = KDirectoryOpenFileRead (__SpotIteratorDirectory, &self->file, "%s", self->filename); if (rc != 0) { PLOGERR(klogErr, (klogErr, rc, "while opening file '$(path)'", "path=%s", self->filename)); } } if (rc == 0) { rc = SpotIteratorReadSpotToRedact(self); } return rc; }
static rc_t md5(const char* path, uint8_t digest[16], const KDirectory* dir) { const KFile* kf = NULL; rc_t rc = KDirectoryOpenFileRead(dir, &kf, "%s", path); if (rc == 0) { KFile* fnull = NULL; rc = KFileMakeNullUpdate(&fnull); if (rc == 0) { KMD5SumFmt* fmt = NULL; rc = KMD5SumFmtMakeUpdate(&fmt, fnull); if (rc == 0) { const KFile* md5 = NULL; rc = KFileMakeNewMD5Read(&md5, kf, fmt, path); if (rc == 0) { uint64_t ps = 0; char buffer[512]; size_t read = 0; do { rc = KFileRead(md5, ps, buffer, sizeof buffer, &read); if (rc == 0) { ps += read; } } while (rc == 0 && read > 0); if (rc == 0) { bool bin; rc = KMD5SumFmtFind(fmt, path, digest, &bin); } } RELEASE(KFile, md5); } RELEASE(KMD5SumFmt, fmt); } /* RELEASE(KFile, fnull); fnull is released by KMD5SumFmt* fmt */ } /* RELEASE(KFile, kf); kf is released by KFile* md5 */ return rc; }
static rc_t run() { KDirectory * pwd; rc_t rc; STSMSG (1, ("Open file system\n")); rc = KDirectoryNativeDir (&pwd); if (rc) LOGERR (klogErr, rc, "Failed to open filesystem"); else { const KFile * unencrypt; STSMSG (1, ("Open unencryptd file %s\n", UNENCRYPT)); rc = KDirectoryOpenFileRead (pwd, &unencrypt, UNENCRYPT); if (rc) LOGERR (klogErr, rc, "failed to open unencryptd file"); else { const KFile * encrypt; STSMSG (1, ("Open encryptd file %s\n", ENCRYPT)); rc = KDirectoryOpenFileRead (pwd, &encrypt, ENCRYPT); if (rc) LOGERR (klogErr, rc, "Failed to open encryptd file"); else { const KFile * decrypt; STSMSG (1, ("Open decrypt file\n")); rc = KFileMakeWGAEncRead (&decrypt, encrypt, WGA_KEY, 1); if (rc) LOGERR (klogErr, rc, "Failed to open decrypter"); else { size_t tot_readu; size_t tot_readd; size_t num_read; uint8_t * decoded = NULL; uint8_t * unencoded = NULL; int comp; decoded = calloc ( 64 * 1024, 24 ); unencoded = calloc ( 64 * 1024, 24 ); for (tot_readu = 0; rc == 0 && tot_readu < sizeof unencoded; tot_readu += num_read) { STSMSG (5, ("Read unencrypted '%u' @ %lu\n", sizeof unencoded - tot_readu, tot_readu)); rc = KFileRead (unencrypt, tot_readu, unencoded + tot_readu, sizeof unencoded - tot_readu, &num_read); if (num_read == 0) break; } if (rc == 0) { for (tot_readd = 0; rc == 0 && tot_readd < sizeof decoded; tot_readd += num_read) { STSMSG (5, ("Read decrypted '%u' @ %lu\n", sizeof decoded - tot_readd, tot_readd)); rc = KFileRead (decrypt, tot_readd, decoded + tot_readd, sizeof decoded - tot_readd, &num_read); if (num_read == 0) break; } comp = memcmp(decoded,unencoded, sizeof decoded); STSMSG (1, ("Read u '%zu' d '%zu' cmp '%d'", tot_readu, tot_readd, comp)); if (comp != 0) { rc = RC (rcExe, rcNoTarg, rcValidating, rcFile, rcInconsistent); LOGERR (klogErr, rc, "Unencryptfailed"); { size_t ix; size_t limit; size_t matched = 0; size_t mismatched = 0; limit = tot_readu; if (limit < tot_readd) limit = tot_readd; for (ix = 0; ix < limit; ++ix) { if (decoded[ix] != unencoded[ix]) { ++mismatched; STSMSG (2, ("%5.5zu: D %c %2.2X U %c %2.2X\n", ix, decoded[ix]?decoded[ix]:'?',decoded[ix], unencoded[ix]?unencoded[ix]:'?',unencoded[ix])); } else ++matched; } STSMSG (2, ("matched %zu mismatched %zu",matched,mismatched)); } } } free (decoded); free (unencoded); } KFileRelease (decrypt); } KFileRelease (encrypt); } KFileRelease (unencrypt); } KDirectoryRelease (pwd); return rc; }
rc_t open (param_block * pb) { rc_t rc; rc = KDirectoryNativeDir (&pb->pwd); if (rc) LOGERR (klogFatal, rc, "Failed to open file system"); else { rc = KDirectoryOpenFileRead (pb->pwd, &pb->file, "%s", pb->file_path); if (rc) LOGERR (klogFatal, rc, "Failed to open input file"); else { rc = KMMapMakeRead (&pb->mmap, pb->file); if (rc) LOGERR (klogFatal, rc, "unable to map file"); else { rc = VDBManagerMakeUpdate (&pb->mgr, pb->pwd); if (rc) LOGERR (klogFatal, rc, "Failed to open DB Manager"); else { rc = VDBManagerMakeSchema (pb->mgr, &pb->schema); if (rc) LOGERR (klogFatal, rc, "Failed to create a schema object"); else { VSchemaAddIncludePath (pb->schema, "interfaces"); rc = VSchemaParseFile (pb->schema, "%s", pb->schema_path); if (rc) LOGERR (klogFatal, rc, "Failed to parse schema"); else { rc = VDBManagerCreateTable (pb->mgr, &pb->table, pb->schema, TYPESPEC, kcmCreate, "%s", pb->table_path); if (rc) LOGERR (klogFatal, rc, "Failed to create table"); else { rc = VTableCreateCursorWrite (pb->table, &pb->cursor, kcmCreate); if (rc) LOGERR (klogFatal, rc, "Failed to create cursor"); else { rc = VCursorAddColumn (pb->cursor, &pb->idx, "READ"); if (rc) LOGERR (klogFatal, rc, "Failed to add READ to cursor"); else { rc = VCursorOpen (pb->cursor); if (rc) LOGERR (klogFatal, rc, "Failed to open cursor"); else { rc = write_rows (pb); if (rc == 0) VCursorCommit (pb->cursor); } } VCursorRelease (pb->cursor); } VTableRelease (pb->table); } } VSchemaRelease (pb->schema); } VDBManagerRelease (pb->mgr); } KMMapRelease (pb->mmap); } KFileRelease (pb->file); } KDirectoryRelease (pb->pwd); } return rc; }
rc_t KRngSysEntropy (KRng * self, uint8_t * buffer, size_t buff_size) { MD5State state; KDirectory * dir; const KFile * file; uint64_t file_pos; rc_t rc; if (self == NULL) return RC (rcKrypto, rcRng, rcWriting, rcSelf, rcNull); if (buffer == NULL) return RC (rcKrypto, rcRng, rcWriting, rcParam, rcNull); rc = KDirectoryNativeDir (&dir); if (rc == 0) { rc = KDirectoryOpenFileRead (dir, &file, "/dev/random"); if (rc) file = NULL; } file_pos = 0; MD5StateInit (&state); while (buff_size > 0) { if (file) { size_t to_read; size_t num_read; to_read = (buff_size > 16) ? 16 : buff_size; rc = KFileRead (file, file_pos, buffer, to_read, &num_read); if (rc == 0) { buffer += num_read; buff_size -= num_read; file_pos += num_read; /* superfluous? */ } } if (buff_size > 0) { uint8_t digest [16]; char buff [1024]; size_t ii; string_printf (buff, sizeof (buff), &ii, "%p%zu%lu%lu%lu%lu%s%s%lu", buffer, buff_size, (uint64_t)clock(), (uint64_t)time(NULL), (uint64_t)getpid(), (uint64_t)getuid(), getlogin(), ttyname(0), (uint64_t)getgid()); MD5StateAppend (&state, buff, sizeof buff); MD5StateFinish (&state, digest); ii = (buff_size < sizeof (digest)) ? buff_size : sizeof (digest); memcpy (buffer, digest, ii); buff_size -= ii; buffer += ii; } } KFileRelease (file); KDirectoryRelease (dir); return 0; }
static rc_t VDBDependenciesReportDepend1(const VDBDependencies *self, const ReportFuncs *f, uint32_t count, uint32_t indent, bool toreport, uint32_t *missing) { KDirectory *dir = NULL; rc_t rc = 0; uint32_t i = ~0; assert(missing); *missing = 0; for (i = 0; i < count; ++i) { bool isMissing = false; bool local = false; const char* seq_id = ""; const char* path = NULL; rc = VDBDependenciesSeqId(self, &seq_id, i); if (rc != 0 && toreport) reportErrorStrInt(indent, rc, "VDBDependenciesSeqId", "origin", "VDatabaseListDependencies", "idx", i); if (rc == 0) { rc = VDBDependenciesLocal(self, &local, i); if (rc != 0 && toreport) { reportErrorStrInt(indent, rc, "VDBDependenciesLocal", "origin", "VDatabaseListDependencies", "idx", i); } } if (rc == 0) { rc = VDBDependenciesPath(self, &path, i); if (rc != 0 && toreport) { reportErrorStrInt(indent, rc, "VDBDependenciesPath", "origin", "VDatabaseListDependencies", "idx", i); } } if (rc == 0) { if (!local && (path == NULL || path[0] == '\0')) { isMissing = true; ++*missing; } if (toreport) { bool reported = false; if (!isMissing && !local) { rc_t rc = 0; bool readable = false; uint64_t size = ~0; KTime_t date = 0; bool ready = false; if (dir == NULL) { rc = KDirectoryNativeDir(&dir); } assert(!local && path && path[0]); if (dir != NULL) { rc = KDirectoryFileSize(dir, &size, "%s", path); if (rc == 0) { rc = KDirectoryDate(dir, &date, "%s", path); } if (rc == 0) { const KFile* f = NULL; rc = KDirectoryOpenFileRead(dir, &f, "%s", path); if (rc == 0) { char buffer[1024]; size_t num_read = 0; size_t bsize = size > sizeof buffer ? sizeof buffer : size; rc = KFileReadAll(f, 0, buffer, bsize, &num_read); if (rc == 0 && num_read != bsize) { rc = RC(rcVDB, rcFile, rcReading, rcBuffer, rcExcessive); } if (rc == 0) { readable = true; } } KFileRelease(f); } if (rc == 0) { ready = true; } } if (ready) { KTime kt; memset(&kt, 0, sizeof kt); KTimeLocal(&kt, date); report(indent, "Dependency", 7, "index", 'd', i, "seq_id", 's', seq_id, "local", 's', local ? "true" : "false", "path", 's', path, "size", 'u', size, "date", 'T', &kt, "readable", 's', readable ? "true" : "false"); reported = true; } else { report(indent, "Dependency", 5, "index", 'd', i, "seq_id", 's', seq_id, "local", 's', local ? "true" : "false", "path", 's', path, "error", 'R', rc); reported = true; } } if (!reported) { report(indent, "Dependency", 4, "index", 'd', i, "seq_id", 's', seq_id, "local", 's', local ? "true" : "false", "path", 's', path == NULL ? "" : path); } } } if (rc != 0 && !toreport) { break; } } RELEASE(KDirectory, dir); return rc; }
static rc_t aws_find_nodes ( KConfigNode *aws_node, const char *aws_path ) { KDirectory *wd; rc_t rc = KDirectoryNativeDir ( &wd ); if ( rc == 0 ) { char *buffer; size_t num_read; uint64_t buf_size; const KFile *credentials, *config; rc = KDirectoryOpenFileRead ( wd, &credentials, "%s%s", aws_path, "/credentials" ); if ( rc == 0 ) { rc = KFileSize ( credentials, &buf_size ); if ( rc == 0 ) { buffer = malloc ( buf_size ); if ( buffer != NULL ) { rc = KFileReadAll ( credentials, 0, buffer, ( size_t ) buf_size, &num_read ); if ( rc == 0 ) aws_parse_file ( credentials, aws_node, buffer, num_read, true ); free ( buffer ); } } KFileRelease ( credentials ); } rc = KDirectoryOpenFileRead ( wd, &config, "%s%s", aws_path, "/config" ); if ( rc == 0 ) { rc = KFileSize ( config, &buf_size ); if ( rc == 0 ) { buffer = malloc ( buf_size ); if ( buffer != NULL ) { rc = KFileReadAll ( config, 0, buffer, ( size_t ) buf_size, &num_read ); if ( rc == 0 ) aws_parse_file ( config, aws_node, buffer, num_read, false ); free ( buffer ); } } KFileRelease ( config ); } KDirectoryRelease ( wd ); } return rc; }
static rc_t run() { KDirectory * pwd; const KDirectory * xfs; const KFile * xml; rc_t rc; STSMSG (1, ("Open file system\n")); rc = KDirectoryNativeDir (&pwd); if (rc) LOGERR (klogErr, rc, "Failed to open filesystem"); else { STSMSG (1, ("Open file %s\n", XML)); rc = KDirectoryOpenFileRead (pwd, &xml, XML); if (rc) LOGERR (klogErr, rc, "failed to open xml file"); else { STSMSG (1, ("Open chrooted as XML TOC %s\n", BASE)); rc = KDirectoryOpenXTocDirRead (pwd, &xfs, true, xml, BASE); if (rc) LOGERR (klogErr, rc, "Failed to open xfs directory"); else { size_t pz; char path [8192]; rc = KDirectoryResolvePath (xfs, false, path, sizeof path, INNER_FILE1); if (rc == 0) { OUTMSG (("path is '%s'\n", path)); rc = KDirectoryResolvePath (xfs, false, path, sizeof path, INNER_FILE2); if (rc == 0) { OUTMSG (("path is '%s'\n", path)); rc = KDirectoryResolvePath (xfs, true, path, sizeof path, INNER_FILE1); if (rc == 0) { OUTMSG (("path is '%s'\n", path)); rc = KDirectoryResolvePath (xfs, true, path, sizeof path, INNER_FILE2); if (rc == 0) { OUTMSG (("path is '%s'\n", path)); } } } } KDirectoryRelease (xfs); } if (rc == 0) { STSMSG (1, ("Open as XML TOC %s\n", BASE)); rc = KDirectoryOpenXTocDirRead (pwd, &xfs, false, xml, BASE); if (rc) LOGERR (klogErr, rc, "Failed to open xfs directory"); else { size_t pz; char path [8192]; rc = KDirectoryResolvePath (xfs, false, path, sizeof path, INNER_FILE1); if (rc == 0) { OUTMSG (("path is '%s'\n", path)); rc = KDirectoryResolvePath (xfs, false, path, sizeof path, INNER_FILE2); if (rc == 0) { OUTMSG (("path is '%s'\n", path)); rc = KDirectoryResolvePath (xfs, true, path, sizeof path, INNER_FILE1); if (rc == 0) { OUTMSG (("path is '%s'\n", path)); rc = KDirectoryResolvePath (xfs, true, path, sizeof path, INNER_FILE2); if (rc == 0) { OUTMSG (("path is '%s'\n", path)); } } } } KDirectoryRelease (xfs); } } KFileRelease (xml); } KDirectoryRelease (pwd); } return rc; }
static rc_t CC _TaskRead ( const struct XTask * self ) { rc_t RCt; const char * Name; uint64_t Offset; size_t Size, NumRead; struct KDirectory * Directory; const struct KFile * File; uint64_t Time; RCt = 0; Name = NULL; Offset = 0; Size = NumRead = 0; Directory = NULL; File = NULL; Time = 0; if ( self == NULL ) { return RC ( rcExe, rcNoTarg, rcProcessing, rcParam, rcNull ); } if ( self -> tasker == NULL ) { return RC ( rcExe, rcNoTarg, rcProcessing, rcParam, rcInvalid ); } RCt = XTaskerNewJob ( self -> tasker, & Name, & Offset, & Size ); if ( RCt == 0 ) { if ( Name != NULL ) { RCt = _TaskRealloc ( self, Size ); if ( RCt == 0 ) { RCt = KDirectoryNativeDir ( & Directory ); if ( RCt == 0 ) { RCt = KDirectoryOpenFileRead ( Directory, & File, Name ); if ( RCt == 0 ) { Time = XTmNow (); RCt = KFileReadAll ( File, Offset, self -> buffer, Size, & NumRead ); if ( RCt == 0 ) { XStatsGood ( self -> tasker -> stat, NumRead, ( XTmNow () - Time ) ); } else { XStatsBad ( self -> tasker -> stat ); } KFileRelease ( File ); } KDirectoryRelease ( Directory ); } } } } return RCt; } /* _TaskRead () */
static rc_t FileInPlace (KDirectory * cwd, const char * leaf, bool try_rename) { rc_t rc; bool is_tmp; STSMSG (1, ("%scrypting file in place %s",De,leaf)); rc = 0; is_tmp = IsTmpFile (leaf); if (is_tmp) { STSMSG (1, ("%s is a vdb-decrypt/vdb-encrypt temporary file and will " "be ignored", leaf)); TmpFoundFlag = true; if (ForceFlag) ; /* LOG OVERWRITE */ else ; /* LOG TMP */ } if (!is_tmp || ForceFlag) { char temp [MY_MAX_PATH]; rc = KDirectoryResolvePath (cwd, false, temp, sizeof temp, ".%s%s", leaf, TmpExt); if (rc) PLOGERR (klogErr, (klogErr, rc, "unable to resolve '.$(S)$(E)'", "S=%s,E=%s",leaf,TmpExt)); else { KPathType kpt; uint32_t kcm; kcm = kcmCreate|kcmParents; kpt = KDirectoryPathType (cwd, temp); if (kpt != kptNotFound) { /* log busy */ if (ForceFlag) { kcm = kcmInit|kcmParents; /* log force */ kpt = kptNotFound; } } if (kpt == kptNotFound) { const KFile * infile; rc = KDirectoryOpenFileRead (cwd, &infile, "%s", leaf); if (rc) PLOGERR (klogErr, (klogErr, rc, "Unable to resolve '$(F)'", "F=%s",leaf)); else { EncScheme scheme; rc = EncryptionTypeCheck (infile, leaf, &scheme); if (rc == 0) { ArcScheme ascheme; bool changed; bool do_this_file; char new_name [MY_MAX_PATH + sizeof EncExt]; do_this_file = DoThisFile (infile, scheme, &ascheme); strcpy (new_name, leaf); if (try_rename) changed = NameFixUp (new_name); else changed = false; /* KOutMsg ("### %d \n", changed); */ if (!do_this_file) { if (changed) { STSMSG (1, ("renaming %s to %s", leaf, new_name)); rc = KDirectoryRename (cwd, false, leaf, new_name); } else STSMSG (1, ("skipping %s",leaf)); } else { KFile * outfile; rc = KDirectoryCreateExclusiveAccessFile (cwd, &outfile, false, 0600, kcm, temp); if (rc) ; else { const KFile * Infile; KFile * Outfile; rc = CryptFile (infile, &Infile, outfile, &Outfile, scheme); if (rc == 0) { STSMSG (1, ("copying %s to %s", leaf, temp)); rc = CopyFile (Infile, Outfile, leaf, temp); if (rc == 0) { uint32_t access; KTime_t date; rc = KDirectoryAccess (cwd, &access, "%s", leaf); if (rc == 0) rc = KDirectoryDate (cwd, &date, "%s", leaf); KFileRelease (infile); KFileRelease (outfile); KFileRelease (Infile); KFileRelease (Outfile); if (rc == 0) { STSMSG (1, ("renaming %s to %s", temp, new_name)); rc = KDirectoryRename (cwd, true, temp, new_name); if (rc) LOGERR (klogErr, rc, "error renaming"); else { if (changed) KDirectoryRemove (cwd, false, "%s", leaf); /*rc =*/ KDirectorySetAccess (cwd, false, access, 0777, "%s", new_name); KDirectorySetDate (cwd, false, date, "%s", new_name); /* gonna ignore an error here I think */ return rc; } } } } KFileRelease (outfile); } } } KFileRelease (infile); } } } } return rc; }
static rc_t FileToFile (const KDirectory * sd, const char * source, KDirectory *dd, const char * dest_, bool try_rename, char * base) { const KFile * infile; rc_t rc; uint32_t access; KTime_t date; bool is_tmp; char dest [MY_MAX_PATH + sizeof EncExt]; strcpy (dest, dest_); if (try_rename) NameFixUp (dest); if ((sd == dd) && (strcmp (source, dest) == 0)) return FileInPlace (dd, dest, try_rename); if (base == NULL) STSMSG (1, ("%scrypting file %s to %s", De, source, dest)); else STSMSG (1, ("%scrypting file %s to %s/%s", De, source, base, dest)); /* * A Hack to make stdin/stout work within KFS */ if (UseStdin) { const KFile * iinfile; rc = KFileMakeStdIn (&iinfile); if (rc == 0) { rc = KBufReadFileMakeRead (&infile, iinfile, 64 * 1024); KFileRelease (iinfile); if (rc == 0) { access = 0640; date = 0; goto stdin_shortcut; } LOGERR (klogErr, rc, "error wrapping stdin"); return rc; } } rc = 0; is_tmp = IsTmpFile (source); if (is_tmp) { TmpFoundFlag = true; if (ForceFlag) ; /* LOG OVERWRITE */ else ; /* LOG TMP */ } if (!is_tmp || ForceFlag) { rc = KDirectoryAccess (sd, &access, "%s", source); if (rc) LOGERR (klogErr, rc, "Error check permission of source"); else { rc = KDirectoryDate (sd, &date, "%s", source); if (rc) LOGERR (klogErr, rc, "Error check date of source"); else { rc = KDirectoryOpenFileRead (sd, &infile, "%s", source); if (rc) PLOGERR (klogErr, (klogErr, rc, "Error opening source file '$(S)'", "S=%s", source)); else { EncScheme scheme; stdin_shortcut: rc = EncryptionTypeCheck (infile, source, &scheme); if (rc == 0) { KFile * outfile; uint32_t kcm; /* * Hack to support stdout before VFS is complete enough to use here */ if (UseStdout) { rc = KFileMakeStdOut (&outfile); if (rc) LOGERR (klogErr, rc, "error wrapping stdout"); } else { kcm = ForceFlag ? kcmInit|kcmParents : kcmCreate|kcmParents; rc = KDirectoryCreateFile (dd, &outfile, false, 0600, kcm, "%s", dest); if (rc) PLOGERR (klogErr,(klogErr, rc, "error opening output '$(O)'", "O=%s", dest)); } if (rc == 0) { const KFile * Infile; KFile * Outfile; rc = CryptFile (infile, &Infile, outfile, &Outfile, scheme); if (rc == 0) { rc = CopyFile (Infile, Outfile, source, dest); if (rc == 0) { if (UseStdin || UseStdout) ; else { rc = KDirectorySetAccess (dd, false, access, 0777, "%s", dest); if (rc == 0 && date != 0) rc = KDirectorySetDate (dd, false, date, "%s", dest); } } KFileRelease (Infile); KFileRelease (Outfile); } KFileRelease (outfile); } } KFileRelease (infile); } } } } return rc; }
static rc_t CC _ReadAll ( const char * Path, char ** Buf, size_t * BufSize ) { rc_t RCt; const struct KFile * File; struct KDirectory * Dir; char * RetBuf; size_t Size, NumRead; RCt = 0; File = NULL; Dir = NULL; RetBuf = NULL; Size = 0; NumRead = 0; RCt = KDirectoryNativeDir ( & Dir ); if ( RCt == 0 ) { RCt = KDirectoryFileSize ( Dir, & Size, Path ); if ( RCt == 0 ) { if ( Size == 0 ) { return RC ( rcExe, rcNoTarg, rcProcessing, rcParam, rcInvalid ); } else { RetBuf = calloc ( Size, sizeof ( char ) ); if ( RetBuf == NULL ) { return RC ( rcExe, rcNoTarg, rcProcessing, rcParam, rcExhausted ); } else { RCt = KDirectoryOpenFileRead ( Dir, & File, Path ); if ( RCt == 0 ) { RCt = KFileReadAll ( File, 0, RetBuf, Size, & NumRead ); if ( RCt == 0 ) { * Buf = RetBuf; * BufSize = Size; } } } } } KDirectoryRelease ( Dir ); } if ( RCt != 0 ) { Buf = NULL; BufSize = 0; if ( RetBuf != NULL ) { free ( RetBuf ); } } return RCt; } /* _ReadAll () */
static rc_t print_headers_from_file( const samdump_opts * opts ) { KDirectory * dir; rc_t rc = KDirectoryNativeDir ( &dir ); if ( rc != 0 ) { (void)PLOGERR( klogErr, ( klogErr, rc, "cant created native directory for file '$(t)'", "t=%s", opts->header_file ) ); } else { const struct KFile * f; rc = KDirectoryOpenFileRead ( dir, &f, "%s", opts->header_file ); if ( rc != 0 ) { (void)PLOGERR( klogErr, ( klogErr, rc, "cant open file '$(t)'", "t=%s", opts->header_file ) ); } else { VNamelist * headers; rc = VNamelistMake ( &headers, 25 ); if ( rc != 0 ) { (void)PLOGERR( klogErr, ( klogErr, rc, "cant create container for file '$(t)'", "t=%s", opts->header_file ) ); } else { rc = LoadKFileToNameList( f, headers ); if ( rc != 0 ) { (void)PLOGERR( klogErr, ( klogErr, rc, "cant load file '$(t)' into container", "t=%s", opts->header_file ) ); } else { uint32_t count; rc = VNameListCount ( headers, &count ); if ( rc != 0 ) { (void)PLOGERR( klogErr, ( klogErr, rc, "cant get count for container of '$(t)'", "t=%s", opts->header_file ) ); } else { uint32_t i; for ( i = 0; i < count && rc == 0; ++i ) { const char * line; rc = VNameListGet ( headers, i, &line ); if ( rc != 0 ) { (void)PLOGERR( klogErr, ( klogErr, rc, "cant get line #$(t) from container", "t=%u", i ) ); } else { rc = KOutMsg( "%s\n", line ); } } } } VNamelistRelease ( headers ); } KFileRelease ( f ); } KDirectoryRelease ( dir ); } return rc; }