/* CreateFile * opens a file with write access * * "f" [ OUT ] - return parameter for newly opened file * * "update" [ IN ] - if true, open in read/write mode * otherwise, open in write-only mode * * "access" [ IN ] - standard Unix access mode, e.g. 0664 * * "mode" [ IN ] - a creation mode ( see explanation above ). * * "path" [ IN ] - NUL terminated string in directory-native * character set denoting target file */ inline rc_t CreateFile ( struct KFile **f, bool update, uint32_t access, KCreateMode mode, const char *path, ... ) throw () { va_list args; va_start ( args, path ); rc_t rc = KDirectoryVCreateFile ( this, f, update, access, mode, path, args ); va_end ( args ); return rc; }
/* this could be modified to accept stdout */ static rc_t open_out_file (const char * path, KFile ** fout) { rc_t rc; KPathType kpt; KCreateMode mode; rc = 0; mode = kcmParents|kcmCreate; kpt = KDirectoryPathType (kdir, path); switch (kpt & ~kptAlias) { case kptDir: /* ----- * this version will fail but perhaps with directory * take last facet <F> from input path and use that as a name * to be <F>.sra in this directory */ default: rc = RC (rcExe, rcNoTarg, rcParsing, rcParam, rcInvalid); PLOGERR (klogFatal, (klogFatal, rc, "archive [$(A)] must be a file", PLOG_S(A), path)); break; case kptFile: if (force) mode = kcmParents|kcmInit; else { rc = RC (rcExe, rcNoTarg, rcParsing, rcFile, rcExists); PLOGERR (klogFatal, (klogFatal, rc, "archive [$(A)] already exists", PLOG_S(A), path)); } break; case kptNotFound: break; } if (rc == 0) { rc = KDirectoryVCreateFile (kdir, fout, false, 0664, mode, path, NULL); if (rc) PLOGERR (klogFatal, (klogFatal, rc, "unable to create archive [$(A)]", PLOG_S(A), path)); } return rc; }
rc_t make_lookup_writer( KDirectory *dir, struct index_writer * idx, struct lookup_writer ** writer, size_t buf_size, const char * fmt, ... ) { rc_t rc; struct KFile * f; va_list args; va_start ( args, fmt ); rc = KDirectoryVCreateFile( dir, &f, false, 0664, kcmInit, fmt, args ); if ( rc != 0 ) ErrMsg( "KDirectoryVCreateFile() -> %R", rc ); else { if ( buf_size > 0 ) { struct KFile * temp_file; rc = KBufFileMakeWrite( &temp_file, f, false, buf_size ); if ( rc != 0 ) ErrMsg( "KBufFileMakeWrite() -> %R", rc ); else { KFileRelease( f ); f = temp_file; } } if ( rc == 0 ) { rc = make_lookup_writer_obj( writer, idx, f ); if ( rc != 0 ) KFileRelease( f ); } } va_end ( args ); return rc; }
static rc_t CC extract_action (const KDirectory * dir, const char * path, void * _adata) { rc_t rc; extract_adata * adata; KPathType type; char link [2 * 4096]; /* we'll truncate? */ uint32_t access; rc = 0; adata = _adata; STSMSG (1, ("extract_action: %s\n", path)); type = KDirectoryPathType (dir, path); if (type & kptAlias) { rc = KDirectoryVResolveAlias (dir, false, link, sizeof (link), path, NULL); if (rc == 0) { rc = KDirectoryVAccess (dir, &access, path, NULL); if (rc == 0) { rc = KDirectoryCreateAlias (adata->dir, access, kcmCreate|kcmParents, link, path); } } } else { switch (type & ~kptAlias) { case kptNotFound: rc = RC (rcExe, rcDirectory, rcAccessing, rcPath, rcNotFound); KOutMsg ("%s: %s type kptNotFouns %R\n", __func__, path, rc); break; case kptBadPath: rc = RC (rcExe, rcDirectory, rcAccessing, rcPath, rcInvalid); break; case kptFile: rc = KDirectoryVAccess (dir, &access, path, NULL); if (rc == 0) { const KFile * fin; KFile * fout; rc = KDirectoryVCreateFile (adata->dir, &fout, false, access, kcmCreate|kcmParents, path, NULL); if (rc == 0) { rc = KDirectoryVOpenFileRead (dir, &fin, path, NULL); if (rc == 0) { #if USE_SKEY_MD5_FIX /* KLUDGE!!!! */ size_t pathz, skey_md5z; static const char skey_md5[] = "skey.md5"; pathz = string_size (path); skey_md5z = string_size(skey_md5); if ( pathz >= skey_md5z && strcmp ( & path [ pathz - skey_md5z ], skey_md5 ) == 0 ) rc = copy_file_skey_md5_kludge (fin, fout); else #endif rc = copy_file (fin, fout); KFileRelease (fin); } KFileRelease (fout); } } break; case kptDir: rc = KDirectoryVAccess (dir, &access, path, NULL); if (rc == 0) { rc = KDirectoryCreateDir (adata->dir, 0700, kcmCreate|kcmParents, path, NULL); if (rc == 0) { rc = step_through_dir (dir, path, adata->filter, adata->fdata, extract_action, adata); if (rc == 0) rc = KDirectoryVSetAccess (adata->dir, false, access, 0777, path, NULL); } } break; case kptCharDev: case kptBlockDev: case kptFIFO: /* shouldn't get here */ return 0; } } return rc; }
static rc_t CCCopyDoFile (CCCopy * self) { const KFile * original; rc_t rc = 0; enum KCreateMode mode; PLOGMSG (klogDebug9, "CCCopyDoFile $(f)", PLOG_S(f), self->path); if (! self->force) { /* if not forced replace mode we fail on existing file */ mode = kcmCreate; } else { uint32_t tt; tt = KDirectoryPathType (self->out, self->path); switch (tt) { default: PLOGMSG (klogWarn, "File exists and will be replaced in output directory $(f)", PLOG_S(f), self->path); break; /* if the path to the file or the file do not exist no warning */ case kptNotFound: case kptBadPath: break; } tt = KDirectoryPathType (self->xml, self->path); switch (tt) { default: PLOGMSG (klogWarn, "File exists and might be replaced in xml directory $(f)", PLOG_S(f), self->path); break; /* if the path to the file or the file do not exist no warning */ case kptNotFound: case kptBadPath: break; } /* forced mode we create with init instead of create forcing a delete/create effect */ mode = kcmInit; } /* open original source for read */ rc = KDirectoryVOpenFileRead (self->in, &original, self->path, NULL); if (rc == 0) { KFile * copy; /* create copy output for write */ rc = KDirectoryVCreateFile (self->out, ©, false, 0644, mode|kcmParents, self->path, NULL); if (rc == 0) { KFile * fm; /* create parallel <path>.md5 */ rc = KDirectoryCreateFile (self->out, &fm, true, 0644, mode, "%s.md5", self->path); if (rc == 0) { KMD5SumFmt * md5f; /* make the .md5 an MD5 sum format file */ rc = KMD5SumFmtMakeUpdate (&md5f, fm); if (rc == 0) { union u { KFile * kf; KMD5File * mf; } outf; /* combine the copy and MD5 file into our special KFile */ rc = KMD5FileMakeWrite (&outf.mf, copy, md5f, self->path); if (rc == 0) { const KFile * inf; /* release this outside reference to the MD5SumFMT leaving * only the one internal to the KMD5File */ KMD5SumFmtRelease (md5f); /* create the KTeeFile that copies reads from the * original as writes to the copy. Reads will be * made by the cataloging process */ rc = KFileMakeTeeRead (&inf, original, outf.kf); if (rc == 0) { CCCat * po; KTime_t mtime; /* try to get a modification time for this pathname */ rc = KDirectoryVDate (self->in, &mtime, self->path, NULL); if (rc != 0) mtime = 0; /* default to ? 0? */ /* create the cataloger giving it the infile which * is the KTeeFile, Indirectory, the XML directory, * and the original path for the file */ rc = CCCatMake (&po, self->in, self->xml, inf, self->md5, self->ff, mtime, self->tree, false, 0, self->path); if (rc == 0) { /* do the catalog (and thus copy) */ rc = CCCatDo(po); /* release the cataloger object */ CCCatRelease (po); } else pLOGERR (klogDebug6, rc, "failure in CCCatMake $(P)", PLOG_S(P), self->path); /* release the infile which will complete a copy * regardless of the state of the cataloger */ KFileRelease (inf); /* return rc; */ } else { KFileRelease (outf.kf); KFileRelease (original); pLOGERR (klogDebug4, rc, "failure with kfilemaketeeread $(P)", PLOG_S(P), self->path); } /* rc = KFileMakeTeeRead (&inf, original, outf.kf);*/ } else { KFileRelease (copy); KMD5SumFmtRelease (md5f); pLOGERR (klogDebug4, rc, "failure with KMD5FileMakeWrite $(P)", PLOG_S(P), self->path); } /* KMD5FileMakeWrite (&outf.mf, copy, md5f, self->path); */ } /* KDirectoryCreateFile (self->out, &fm, true, 0644, mode, "%s.md5", */ else pLOGERR (klogDebug4, rc, "failure with KMD5SumFmtMakeUpdate $(P)", PLOG_S(P), self->path); KFileRelease (fm); } /* KDirectoryCreateFile (self->out, &fm, true, 0644, mode, "%s.md5", */ else pLOGERR (klogDebug4, rc, "failure with KDirectoryCreateFile $(P).md5", PLOG_S(P), self->path); KFileRelease (copy); } /* rc = KDirectoryVCreateFile (self->out, ©, false, 0644, mode|kcmParents, */ else pLOGERR (klogDebug4, rc, "failure with KDirectoryVCreateFile $(P)", PLOG_S(P), self->path); KFileRelease (original); } /* rc = KDirectoryVOpenFileRead (self->in, &original, self->path, NULL); */ else pLOGERR (klogDebug4, rc, "failure with KDirectoryVOpenFileRead $(pP)", PLOG_S(P), self->path); return rc; }
inline rc_t CreateFile ( struct KFile **f, bool update, uint32_t access, KCreateMode mode, const char *path, va_list args ) throw () { return KDirectoryVCreateFile ( this, f, update, access, mode, path, args ); }