From: Eric Bollengier Date: Thu, 6 Aug 2009 19:17:39 +0000 (+0200) Subject: add user handler to print directory X-Git-Tag: Release-5.0.0~336^2~1 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=200faf7fb91ed91ea3cca258e9ac1dc79a1c22f3;p=bacula%2Fbacula add user handler to print directory add -j, -p to bvfs_test tool --- diff --git a/bacula/src/cats/bvfs.c b/bacula/src/cats/bvfs.c index b5996cffb8..690102cd07 100644 --- a/bacula/src/cats/bvfs.c +++ b/bacula/src/cats/bvfs.c @@ -36,6 +36,46 @@ #define dbglevel 10 #define dbglevel_sql 15 +static int result_handler(void *ctx, int fields, char **row) +{ + if (fields == 4) { + Pmsg4(0, "%s\t%s\t%s\t%s\n", + row[0], row[1], row[2], row[3]); + } else if (fields == 5) { + Pmsg5(0, "%s\t%s\t%s\t%s\t%s\n", + row[0], row[1], row[2], row[3], row[4]); + } else if (fields == 6) { + Pmsg6(0, "%s\t%s\t%s\t%s\t%s\t%s\n", + row[0], row[1], row[2], row[3], row[4], row[5]); + } else if (fields == 7) { + Pmsg7(0, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n", + row[0], row[1], row[2], row[3], row[4], row[5], row[6]); + } + return 0; +} + +Bvfs::Bvfs(JCR *j, B_DB *mdb) { + jcr = j; + jcr->inc_use_count(); + db = mdb; /* need to inc ref count */ + jobids = get_pool_memory(PM_NAME); + pattern = get_pool_memory(PM_NAME); + *pattern = *jobids = 0; + dir_filenameid = pwd_id = offset = 0; + see_copies = see_all_version = false; + limit = 1000; + attr = new_attr(jcr); + list_entries = result_handler; + user_data = this; +} + +Bvfs::~Bvfs() { + free_pool_memory(jobids); + free_pool_memory(pattern); + free_attr(attr); + jcr->dec_use_count(); +} + /* * TODO: Find a way to let the user choose how he wants to display * files and directories @@ -116,9 +156,7 @@ char *bvfs_parent_dir(char *path) return path; } - - -/* Return the basename of the with the trailing / (update the given string) +/* Return the basename of the with the trailing / * TODO: see in the rest of bacula if we don't have * this function already */ @@ -302,7 +340,6 @@ bail_out: db_unlock(mdb); } - /* * Find an store the filename descriptor for empty directories Filename.Name='' */ @@ -388,33 +425,6 @@ void Bvfs::update_cache() bvfs_update_path_hierarchy_cache(jcr, db, jobids); } -static int result_handler(void *ctx, int fields, char **row) -{ - if (fields == 4) { - Dmsg4(0, "%s\t%s\t%s\t%s\n", - row[0], row[1], row[2], row[3]); - } else if (fields == 5) { - Dmsg5(0, "%s\t%s\t%s\t%s\t%s\n", - row[0], row[1], row[2], row[3], row[4]); - } else if (fields == 6) { - Dmsg6(0, "%s\t%s\t%s\t%s\t%s\t%s\n", - row[0], row[1], row[2], row[3], row[4], row[5]); - } else if (fields == 7) { - Dmsg7(0, "%s\t%s\t%s\t%s\t%s\t%s\t%s\n", - row[0], row[1], row[2], row[3], row[4], row[5], row[6]); - } - return 0; -} - -static int result_path_handler(void *ctx, int fields, char **row) -{ - if (fields == 4) { - Dmsg4(0, "%s\t%s\t%s\t%s\n", - row[0], bvfs_basename_dir(row[1]), row[2], row[3]); - } - return 0; -} - /* Change the current directory, returns true if the path exists */ bool Bvfs::ch_dir(char *path) { @@ -458,7 +468,7 @@ void Bvfs::get_all_file_versions(DBId_t pathid, DBId_t fnid, char *client) ,edit_uint64(fnid, ed1), edit_uint64(pathid, ed2), client, q.c_str(), limit, offset); - db_sql_query(db, query.c_str(), result_handler, this); + db_sql_query(db, query.c_str(), list_entries, user_data); } DBId_t Bvfs::get_root() @@ -492,7 +502,7 @@ void Bvfs::ls_special_dirs() POOL_MEM query2; Mmsg(query2, -"SELECT tmp.PathId, tmp.Path, LStat, JobId " +"SELECT tmp.PathId, tmp.Path, JobId, LStat " "FROM %s AS tmp LEFT JOIN ( " // get attributes if any "SELECT File1.PathId AS PathId, File1.JobId AS JobId, " "File1.LStat AS LStat FROM File AS File1 " @@ -503,7 +513,7 @@ void Bvfs::ls_special_dirs() query.c_str(), edit_uint64(dir_filenameid, ed2), jobids); Dmsg1(dbglevel_sql, "q=%s\n", query.c_str()); - db_sql_query(db, query2.c_str(), result_handler, this); + db_sql_query(db, query2.c_str(), list_entries, user_data); } void Bvfs::ls_dirs() @@ -530,7 +540,8 @@ void Bvfs::ls_dirs() */ /* Then we get all the dir entries from File ... */ POOL_MEM query; - Mmsg(query, + Mmsg(query, +// 0 1 2 3 "SELECT PathId, Path, JobId, LStat FROM ( " "SELECT Path1.PathId AS PathId, Path1.Path AS Path, " "lower(Path1.Path) AS lpath, " @@ -563,7 +574,7 @@ void Bvfs::ls_dirs() limit, offset); Dmsg1(dbglevel_sql, "q=%s\n", query.c_str()); - db_sql_query(db, query.c_str(), result_path_handler, this); + db_sql_query(db, query.c_str(), list_entries, user_data); } void Bvfs::ls_files() @@ -584,8 +595,8 @@ void Bvfs::ls_files() } POOL_MEM query; - Mmsg(query, // 0 1 2 3 4 -"SELECT File.FilenameId, listfiles.id, listfiles.Name, File.LStat, File.JobId " + Mmsg(query, // 0 1 2 3 4 +"SELECT File.FilenameId, listfiles.Name, File.JobId, File.LStat, listfiles.id " "FROM File, ( " "SELECT Filename.Name as Name, max(File.FileId) as id " "FROM File, Filename " @@ -604,5 +615,5 @@ void Bvfs::ls_files() limit, offset); Dmsg1(dbglevel_sql, "q=%s\n", query.c_str()); - db_sql_query(db, query.c_str(), result_handler, this); + db_sql_query(db, query.c_str(), list_entries, user_data); } diff --git a/bacula/src/cats/bvfs.h b/bacula/src/cats/bvfs.h index 53b65203cd..f4531b378a 100644 --- a/bacula/src/cats/bvfs.h +++ b/bacula/src/cats/bvfs.h @@ -42,26 +42,27 @@ * fs.ls_files(); */ +/* Helper for result handler */ +typedef enum { + BVFS_FILE_RECORD = 5, + BVFS_DIR_RECORD = 4, + BVFS_FILE_VERSION = 6 +} bvfs_handler_type; + +typedef enum { + BVFS_Id = 0, + BVFS_Name = 1, + BVFS_JobId = 2, + BVFS_LStat = 3, + + BVFS_FileId = 4, /* Only if File record */ +} bvfs_row_index; + class Bvfs { public: - Bvfs(JCR *j, B_DB *mdb) { - jcr = j; - jcr->inc_use_count(); - db = mdb; /* need to inc ref count */ - jobids = get_pool_memory(PM_NAME); - pattern = get_pool_memory(PM_NAME); - *pattern = *jobids = 0; - dir_filenameid = pwd_id = offset = 0; - see_copies = see_all_version = false; - limit = 1000; - } - - virtual ~Bvfs() { - free_pool_memory(jobids); - free_pool_memory(pattern); - jcr->dec_use_count(); - } + Bvfs(JCR *j, B_DB *mdb); + virtual ~Bvfs(); void set_jobid(JobId_t id) { Mmsg(jobids, "%lld", (uint64_t)id); @@ -115,6 +116,19 @@ public: see_copies = val; } + void set_handler(DB_RESULT_HANDLER *h, void *ctx) { + list_entries = h; + user_data = ctx; + } + + ATTR *get_attr() { + return attr; + } + + JCR *get_jcr() { + return jcr; + } + private: JCR *jcr; B_DB *db; @@ -124,11 +138,15 @@ private: POOLMEM *pattern; DBId_t pwd_id; DBId_t dir_filenameid; + ATTR *attr; bool see_all_version; bool see_copies; DBId_t get_dir_filenameid(); + + DB_RESULT_HANDLER *list_entries; + void *user_data; }; void bvfs_update_path_hierarchy_cache(JCR *jcr, B_DB *mdb, char *jobids); diff --git a/bacula/src/tools/Makefile.in b/bacula/src/tools/Makefile.in index 85b2d3beab..121808a9d9 100644 --- a/bacula/src/tools/Makefile.in +++ b/bacula/src/tools/Makefile.in @@ -103,9 +103,9 @@ bbatch: Makefile ../lib/libbac$(DEFAULT_ARCHIVE_TYPE) ../cats/libbacsql$(DEFAULT $(LIBTOOL_LINK) $(CXX) -g $(LDFLAGS) -L../cats -L. -L../lib -o $@ bbatch.o \ -lbacsql -lbac -lm $(DB_LIBS) $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS) -bvfs_test: Makefile ../lib/libbac$(DEFAULT_ARCHIVE_TYPE) ../cats/libbacsql$(DEFAULT_ARCHIVE_TYPE) bvfs_test.o - $(LIBTOOL_LINK) $(CXX) -g $(LDFLAGS) -L../cats -L. -L../lib -o $@ bvfs_test.o \ - -lbacsql -lbac -lm $(DB_LIBS) $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS) +bvfs_test: Makefile ../findlib/libbacfind$(DEFAULT_ARCHIVE_TYPE) ../lib/libbac$(DEFAULT_ARCHIVE_TYPE) ../cats/libbacsql$(DEFAULT_ARCHIVE_TYPE) bvfs_test.o + $(LIBTOOL_LINK) $(CXX) -g $(LDFLAGS) -L../cats -L. -L../lib -o $@ $(FINDOBJS) bvfs_test.o \ + -lbacsql -lbacfind -lbac -lm $(DB_LIBS) $(LIBS) $(GETTEXT_LIBS) $(OPENSSL_LIBS) gigaslam.o: gigaslam.c $(CC) -c $< diff --git a/bacula/src/tools/bvfs_test.c b/bacula/src/tools/bvfs_test.c index 4c18e79287..4eae62cc01 100644 --- a/bacula/src/tools/bvfs_test.c +++ b/bacula/src/tools/bvfs_test.c @@ -38,6 +38,7 @@ #include "bacula.h" #include "cats/cats.h" #include "cats/bvfs.h" +#include "findlib/find.h" /* Local variables */ static B_DB *db; @@ -59,16 +60,44 @@ PROG_COPYRIGHT " -P specify database host (default NULL)\n" " -w specify working directory\n" +" -j specify jobids\n" +" -p specify path\n" +" -f specify file\n" " -v verbose\n" " -? print this message\n\n"), 2001, VERSION, BDATE); exit(1); } +static int result_handler(void *ctx, int fields, char **row) +{ + Bvfs *vfs = (Bvfs *)ctx; + ATTR *attr = vfs->get_attr(); + char *empty = ""; + + if (fields == BVFS_DIR_RECORD || fields == BVFS_FILE_RECORD) { + decode_stat((row[BVFS_LStat])?row[BVFS_LStat]:empty, + &attr->statp, &attr->LinkFI); + if (fields == BVFS_DIR_RECORD) { + pm_strcpy(attr->ofname, bvfs_basename_dir(row[BVFS_Name])); + } else { + pm_strcpy(attr->ofname, row[BVFS_Name]); + } + print_ls_output(vfs->get_jcr(), attr); + + } else { + Pmsg6(0, "%s\t%s\t%s\t%s\t%s\t%s", + row[0], row[1], row[2], row[3], row[4], row[5]); + } + return 0; +} + + /* number of thread started */ int main (int argc, char *argv[]) { int ch; + char *jobids="1", *path=NULL, *file=NULL; setlocale(LC_ALL, ""); bindtextdomain("bacula", LOCALEDIR); textdomain("bacula"); @@ -81,7 +110,7 @@ int main (int argc, char *argv[]) OSDependentInit(); - while ((ch = getopt(argc, argv, "h:c:d:n:P:Su:vf:w:?")) != -1) { + while ((ch = getopt(argc, argv, "h:c:d:n:P:Su:vf:w:?j:p:f:")) != -1) { switch (ch) { case 'd': /* debug level */ if (*optarg == 't') { @@ -118,6 +147,18 @@ int main (int argc, char *argv[]) verbose++; break; + case 'p': + path = optarg; + break; + + case 'f': + path = optarg; + break; + + case 'j': + jobids = optarg; + break; + case '?': default: usage(); @@ -162,30 +203,44 @@ int main (int argc, char *argv[]) bvfs_update_cache(bjcr, db); Bvfs fs(bjcr, db); + fs.set_handler(result_handler, &fs); - fs.set_jobids("1"); + fs.set_jobids(jobids); fs.update_cache(); - fs.ch_dir(""); - fs.ls_files(); - fs.ls_dirs(); + if (path) { + fs.ch_dir(path); + fs.ls_special_dirs(); + fs.ls_dirs(); + fs.ls_files(); + exit (0); + } + + + Pmsg0(0, "list /\n"); fs.ch_dir("/"); - fs.ls_files(); + fs.ls_special_dirs(); fs.ls_dirs(); + fs.ls_files(); + Pmsg0(0, "list /tmp/\n"); fs.ch_dir("/tmp/"); - fs.ls_files(); + fs.ls_special_dirs(); fs.ls_dirs(); + fs.ls_files(); + Pmsg0(0, "list /tmp/regress/\n"); fs.ch_dir("/tmp/regress/"); + fs.ls_special_dirs(); fs.ls_files(); fs.ls_dirs(); - fs.set_jobid(1); + Pmsg0(0, "list /tmp/regress/build/\n"); fs.ch_dir("/tmp/regress/build/"); - fs.ls_files(); - fs.ls_dirs(); fs.ls_special_dirs(); + fs.ls_dirs(); + fs.ls_files(); + fs.get_all_file_versions(1, 347, "zog4-fd"); char p[200];