rc_t CopyDirectoryFiles( const KDirectory *source, KDirectory *dest ) { rc_t rc; KNamelist *list; const char *name; int i; uint32_t count; char resolved[1024]; rc = KDirectoryList( source, &list, PathIsFile, NULL, "."); if (rc != 0) { /* This doesn't do what I thought. */ KDirectoryResolvePath( source, false, resolved, 1024, "."); LOGERR ( klogInt, rc, resolved ); return rc; } KNamelistCount(list, &count); for (i=0; i<count; i++) { KNamelistGet(list, i, &name); if (test) { fprintf(stderr, "Will copy %s\n", name); } else { CopyFileToFile( source, name, dest, (char *)name ); } } return 0; }
static rc_t CC _LoadKartScanPath ( struct XFSKartCollectionNode * Node ) { rc_t RCt; struct KDirectory * Directory; KNamelist * List; const char * Name; uint32_t Idx; uint32_t ListCount; RCt = 0; Directory = NULL; List = NULL; Name = NULL; Idx = 0; ListCount = 0; if ( Node == NULL ) { return XFS_RC ( rcNull ); } if ( Node -> path == NULL ) { return XFS_RC ( rcInvalid ); } RCt = KDirectoryNativeDir ( & Directory ); if ( RCt == 0 ) { RCt = KDirectoryList ( Directory, & List, NULL, NULL, Node -> path ); if ( RCt == 0 ) { RCt = KNamelistCount ( List, & ListCount ); if ( RCt == 0 ) { for ( Idx = 0; Idx < ListCount; Idx ++ ) { RCt = KNamelistGet ( List, Idx, & Name ); if ( RCt == 0 ) { RCt = _LoadKartItem ( Directory, Node, Name ); } if ( RCt != 0 ) { break; } } } KNamelistRelease ( List ); } KDirectoryRelease ( Directory ); } return RCt; } /* _LoadKartScanPath () */
LIB_EXPORT rc_t CC KTableListIdx ( const KTable *self, KNamelist **names ) { if ( self != NULL ) { if ( ! self -> prerelease ) { return KDirectoryList ( self -> dir, names, KTableListIdxFilter, NULL, "idx" ); } return KDirectoryList ( self -> dir, names, KTableListSkeyFilter, NULL, "." ); } if ( names != NULL ) * names = NULL; return RC ( rcDB, rcTable, rcListing, rcSelf, rcNull ); }
LIB_EXPORT rc_t CC KDatabaseListIdx ( struct KDatabase const *self, KNamelist **names ) { if ( self != NULL ) { struct FilterData data; data.mgr = self->mgr; data.type = kptIndex; return KDirectoryList ( self -> dir, names, KDatabaseListFilter, &data, "idx" ); } if ( names != NULL ) * names = NULL; return RC ( rcDB, rcDatabase, rcListing, rcSelf, rcNull ); }
LIB_EXPORT rc_t CC KTableListCol ( const KTable *self, KNamelist **names ) { if ( self != NULL ) { struct FilterData data; data.mgr = self->mgr; data.type = kptColumn; return KDirectoryList ( self -> dir, names, KDatabaseListFilter, &data, "col" ); } if ( names != NULL ) * names = NULL; return RC ( rcDB, rcTable, rcListing, rcSelf, rcNull ); }
rc_t CopyDirectoryDirectories( const KDirectory *source, KDirectory *dest ) { rc_t rc; KNamelist *list; const char *name; int i; uint32_t count; uint32_t mode; uint32_t pathtype; KDirectoryList( source, &list, PathIsDir, NULL, "."); KNamelistCount(list, &count); for (i=0; i<count; i++) { KNamelistGet(list, i, &name); /* fprintf(stderr, "Creating directory %s\n", name); */ mode = DEFAULT_DIR_MODE; rc = KDirectoryAccess( source, &mode, "%s", name); if (rc != 0) { LOGERR ( klogInt, rc, name ); return rc; } pathtype = KDirectoryPathType( dest, "%s", name ); if ((pathtype & ~kptAlias) == kptNotFound) { rc = KDirectoryCreateDir( dest, mode, kcmOpen, "%s", name ); if (rc != 0) { LOGERR ( klogInt, rc, name ); return rc; } } else if ((pathtype & ~kptAlias) == kptDir) { if (clobber_protections) { KDirectorySetAccess( dest, false, mode, 0777, "%s", name); } } CopyDirectoryToExistingDirectory( source, name, dest, (char *)name); } return 0; }
static rc_t DoDir (const KDirectory * sd, KDirectory * dd) { KNamelist * names; rc_t rc; rc = KDirectoryList (sd, &names, NULL, NULL, "."); if (rc) ; else { uint32_t count; rc = KNamelistCount (names, &count); if (rc) ; else { uint32_t idx; for (idx = 0; idx < count; ++idx) { const char * name; rc = KNamelistGet (names, idx, &name); if (rc) ; else { const KDirectory * nsd; KDirectory * ndd; KPathType kpt; kpt = KDirectoryPathType (sd, name); switch (kpt) { default: break; case kptFile: if (sd == dd) rc = FileInPlace (dd, name, true); else rc = FileToFile (sd, name, dd, name, true, NULL); break; case kptDir: if (sd == dd) { rc = KDirectoryOpenDirUpdate (dd, &ndd, false, "%s", name); if (rc) ; else { /* RECURSION */ STSMSG (1, ("%scrypting directory %s", De, name)); rc = DoDir (ndd, ndd); STSMSG (1, ("done with directory %s", name)); KDirectoryRelease (ndd); } } else { rc = KDirectoryOpenDirRead (sd, &nsd, false, name); if (rc) ; else { rc = KDirectoryCreateDir (dd, 0600, kcmOpen, "%s", name); if (rc) ; else { rc = KDirectoryOpenDirUpdate (dd, &ndd, false, "%s", name); if (rc) ; else { /* RECURSION */ STSMSG (1, ("%scrypting directory %s", De, name)); rc = DoDir (nsd, ndd); STSMSG (1, ("done with directory %s", name)); KDirectoryRelease (ndd); } } KDirectoryRelease (nsd); } } break; } } } } KNamelistRelease (names); } return rc; }
/* filter will let us add the add to and extract by name things to kar */ static rc_t step_through_dir (const KDirectory * dir, const char * path, bool ( CC * filter)(const KDirectory *, const char *, void *), void * fdata, rc_t ( CC * action)(const KDirectory *, const char *, void *), void * adata) { rc_t rc; KNamelist * names; STSMSG (4, ("step_through_dir %s\n", path)); rc = KDirectoryList (dir, &names, NULL, NULL, path); if (rc == 0) { uint32_t limit; rc = KNamelistCount (names, &limit); if (rc == 0) { uint32_t idx; size_t pathlen; pathlen = strlen(path); for (idx = 0; idx < limit; idx ++) { const char * name; rc = KNamelistGet (names, idx, &name); if (rc == 0) { char * new_path; size_t namelen; size_t new_pathlen; size_t rsize; namelen = strlen (name); new_pathlen = pathlen + 1 + namelen; rsize = new_pathlen + 1; new_path = malloc (rsize); if (new_path != NULL) { char * recur_path; if (pathlen == 0) { memcpy (new_path, name, namelen); new_path[namelen] = '\0'; } else { memcpy (new_path, path, pathlen); new_path[pathlen] = '/'; memcpy (&new_path[pathlen+1], name, namelen); new_path[new_pathlen] = '\0'; } recur_path = malloc (rsize); if (recur_path != NULL) { rc = KDirectoryVResolvePath (dir, false, recur_path, rsize, new_path, NULL); if (rc == 0) { bool use_name; if (filter != NULL) { use_name = filter (dir, recur_path, fdata); } else { use_name = true; } if (use_name) { rc = action (dir, recur_path, adata); /* if (rc == 0) */ /* { */ /* enum KPathType type; */ /* type = (enum KPathType)KDirectoryPathType (dir, recur_path); */ /* if (type == kptDir) */ /* rc = step_through_dir (dir, recur_path, filter, fdata, action, adata); */ /* } */ } } free (recur_path); } free (new_path); } } if (rc != 0) break; } } KNamelistRelease (names); } return rc; }
/* filter will let us add the add to and extract by name things to kar */ static rc_t step_through_dir (const KDirectory * dir, const char * path, rc_t (*action)(const KDirectory *, const char *, void *), void * adata) { rc_t rc; KNamelist * names; STSMSG (4, ("step_through_dir %s\n", path)); rc = KDirectoryList (dir, &names, NULL, NULL, "%s", path); if (rc == 0) { uint32_t limit; rc = KNamelistCount (names, &limit); if (rc == 0) { uint32_t idx; size_t pathlen; pathlen = strlen(path); for (idx = 0; (rc == 0) && (idx < limit); idx ++) { const char * name; rc = KNamelistGet (names, idx, &name); if (rc == 0) { size_t namelen = strlen (name); size_t new_pathlen = pathlen + 1 + namelen; char * new_path = malloc (new_pathlen + 1); if (new_path != NULL) { char * recur_path; if (pathlen == 0) { memmove (new_path, name, namelen); new_path[namelen] = '\0'; } else { memmove (new_path, path, pathlen); new_path[pathlen] = '/'; memmove (new_path + pathlen + 1, name, namelen); new_path[pathlen+1+namelen] = '\0'; } recur_path = malloc (pathlen + 1 + namelen + 1); if (recur_path != NULL) { rc = KDirectoryResolvePath (dir, false, recur_path, pathlen + 1 + namelen + 1, "%s", new_path); if (rc == 0) rc = action (dir, recur_path, adata); free (recur_path); } free (new_path); } } } } KNamelistRelease (names); } return rc; }
static rc_t foreach_path_obj( visit_ctx * ctx, on_path_t func ) { rc_t rc = 0; ctx->path_type = ( KDirectoryPathType ( ctx->dir, "%s", ctx->path ) & ~ kptAlias ); if ( ctx->path_type == kptDir ) { KNamelist * path_objects; rc = KDirectoryList ( ctx->dir, &path_objects, NULL, NULL, "%s", ctx->path ); if ( rc != 0 ) { PLOGERR( klogErr, ( klogErr, rc, "KDirectoryList( $(path) ) failed in $(func)", "path=%s,func=%s", ctx->path, __func__ ) ); } else { uint32_t idx, count; rc = KNamelistCount ( path_objects, &count ); if ( rc != 0 ) { PLOGERR( klogErr, ( klogErr, rc, "KNamelistCount() failed in $(func)", "func=%s", __func__ ) ); } for ( idx = 0; idx < count && rc == 0 && !ctx->terminate; ++idx ) { const char * obj_name = NULL; rc = KNamelistGet ( path_objects, idx, &obj_name ); if ( rc != 0 ) { PLOGERR( klogErr, ( klogErr, rc, "KNamelistGet( $(idx) ) failed in $(func)", "idx=%d,func=%s", idx, __func__ ) ); } else if ( obj_name != NULL ) { char obj_path[ 4096 ]; rc = KDirectoryResolvePath ( ctx->dir, true, obj_path, sizeof obj_path, "%s/%s", ctx->path, obj_name ); if ( rc != 0 ) { PLOGERR( klogErr, ( klogErr, rc, "KDirectoryResolvePath( $(path) ) failed in $(func)", "path=%s,func=%s", ctx->path, __func__ ) ); } else { visit_ctx octx; octx.dir = ctx->dir; octx.options = ctx->options; octx.path = obj_path; octx.data = ctx->data; octx.path_type = ( KDirectoryPathType ( ctx->dir, "%s", obj_path ) & ~ kptAlias ); if ( octx.path_type == kptDir ) { rc = foreach_path_obj( &octx, func ); /* recursion !!! */ } else if ( octx.path_type == kptFile ) { rc = func( &octx ); } } } } { rc_t rc2 = KNamelistRelease ( path_objects ); if ( rc2 != 0 ) { PLOGERR( klogErr, ( klogErr, rc2, "KNamelistRelease() failed in $(func)", "func=%s", __func__ ) ); } } } } if ( rc == 0 && ( ctx->path_type == kptDir || ctx->path_type == kptFile ) && !ctx->terminate ) { /* at the very end of it, call the function for the path itself */ rc = func( ctx ); } return rc; }