Target directory point inside catalog.
authorFrantišek Dvořák <valtri@civ.zcu.cz>
Sun, 13 Oct 2013 20:55:59 +0000 (22:55 +0200)
committerFrantišek Dvořák <valtri@civ.zcu.cz>
Sun, 13 Oct 2013 21:45:07 +0000 (23:45 +0200)
scan.cpp
walk.cpp
walk.h [new file with mode: 0644]

index d6143c2..e01ee81 100644 (file)
--- a/scan.cpp
+++ b/scan.cpp
@@ -7,6 +7,8 @@
 #include <dmlite/cpp/catalog.h>
 #include <dmlite/cpp/poolmanager.h>
 
+#include "walk.h"
+
 
 
 #define DMLITE_CONFIG_DEFAULT "/etc/dmlite.conf"
@@ -23,18 +25,15 @@ const char *optstring = "c:h";
 
 
 
-void walk(dmlite::Catalog *catalog, const char *path) throw(dmlite::DmException);
-
-
-
 static void usage(const char *prg) {
-       printf("Usage: %s [OPTIONS] DIRECTORY\n\n", prg);
+       printf("Usage: %s [OPTIONS] DIRECTORY [TARGET]\n\n", prg);
        printf("OPTIONS are:\n");
        printf("  -h,--help ...... usage information\n");
        printf("  -a,--address ... host address (for credentials) [%s]\n", ADDRESS_DEFAULT);
-       printf("  -c,--config .... dmlite config file [/etc/dmlite.conf,~/.dmlite.conf]\n");
+       printf("  -c,--config .... dmlite config file [/etc/dmlite.conf]\n");
        printf("  -u,--user ...... user name (for credentials) [%s]\n", USER_DEFAULT);
        printf("DIRECTORY ........ path to browse and import\n");
+       printf("TARGET... ........ target directory in the catalog [=DIRECTORY]\n");
 }
 
 
@@ -88,7 +87,7 @@ int main(int argc, char *argv[]) {
        dmlite::Catalog* catalog = NULL;
        //dmlite::PoolManager* poolManager = NULL;
        dmlite::SecurityCredentials creds;
-       char *dmlite_config = NULL, *user = NULL, *address = NULL;
+       char *dmlite_config = NULL, *user = NULL, *address = NULL, *path, *target;
        int opt;
        bool safetyCheck = true;
 
@@ -165,7 +164,9 @@ int main(int argc, char *argv[]) {
 
                // now we can walk safely
                try {
-                       walk(catalog, argv[optind]);
+                       path = argv[optind];
+                       target = (optind + 1 < argc) ? argv[optind + 1] : path;
+                       walk(catalog, path, target);
                } catch (dmlite::DmException& e) {
                        std::cout << "Could not walk." << std::endl
                                << "Reason: " << e.what() << std::endl;
index a2e8b70..293a154 100644 (file)
--- a/walk.cpp
+++ b/walk.cpp
@@ -9,9 +9,11 @@
 #include <dmlite/cpp/dmlite.h>
 #include <dmlite/cpp/catalog.h>
 
+#include "walk.h"
 
 static dmlite::Catalog *catalog;
-static const char *prefix;
+static std::string localPrefix;
+static std::string nsPrefix;
 static struct utimbuf utimbuf;
 static struct dirent *entry;
 static std::string lpath;
@@ -34,19 +36,19 @@ static void walk_recursive(const char *path) throw(dmlite::DmException) {
        time_t diratime, dirmtime;
        DIR *dir = NULL;
 
-       lpath = std::string(prefix) + path;
+       lpath = localPrefix + path;
 
-//printf("[level %d] '%s'/'%s'\n", level, prefix, path);
+//printf("[level %d] '%s'/'%s'\n", level, localPrefix.c_str(), path);
        wrapCall(lstat(lpath.c_str(), &pathstat), "can't lstat() '" + lpath + "'");
        if (S_ISREG(pathstat.st_mode)) {
-               catalog->create(path, pathstat.st_mode);
+               catalog->create(nsPrefix + path, pathstat.st_mode);
                utimbuf.actime = pathstat.st_atime;
                utimbuf.modtime = pathstat.st_mtime;
-               catalog->utime(path, &utimbuf);
+               catalog->utime(nsPrefix + path, &utimbuf);
                //catalog->setSize(path, stat.st_size);
        } else if (S_ISDIR(pathstat.st_mode)) {
                // create under permissive modes
-               if (path[0]) catalog->makeDir(path, 0775);
+               if (path[0]) catalog->makeDir(nsPrefix + path, 0775);
 
                // remember for later
                dirmode = pathstat.st_mode;
@@ -57,7 +59,7 @@ static void walk_recursive(const char *path) throw(dmlite::DmException) {
                wrapCall(dir = opendir(lpath.c_str()), "can't open directory '" + lpath + "'");
                level++;
                try {
-                       if (level > 10) throw dmlite::DmException(E2BIG, "[level %d] maximul level reached at '%s/%s'", level, prefix, path);
+                       if (level > 10) throw dmlite::DmException(E2BIG, "[level %d] maximul level reached at '%s/%s'", level, localPrefix.c_str(), path);
                        while ((entry = readdir(dir)) != NULL) {
                                if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
                                        walk_recursive((std::string(path) + "/" + entry->d_name).c_str());
@@ -70,21 +72,23 @@ static void walk_recursive(const char *path) throw(dmlite::DmException) {
                level--;
                closedir(dir);
 
-//printf("[level %d] finalize '%s'/'%s'\n", level, prefix, path);
+//printf("[level %d] finalize '%s'/'%s'\n", level, localPrefix.c_str(), path);
                // set remebered values
                if (path[0]) {
                        utimbuf.actime = diratime;
                        utimbuf.modtime = dirmtime;
-                       catalog->utime(path, &utimbuf);
-                       catalog->setMode(path, dirmode);
+                       catalog->utime(nsPrefix + path, &utimbuf);
+                       catalog->setMode(nsPrefix + path, dirmode);
                }
        }
 }
 
 
 
-void walk(dmlite::Catalog *acatalog, const char *path) throw(dmlite::DmException) {
-       prefix = path;
+void walk(dmlite::Catalog *acatalog, const char *path, const char *target) throw(dmlite::DmException) {
+       localPrefix = path;
+       nsPrefix = target;
+       if (nsPrefix == "/") nsPrefix = "";
        level = 0;
        catalog = acatalog;
        acatalog->changeDir("/");
diff --git a/walk.h b/walk.h
new file mode 100644 (file)
index 0000000..3cb94ad
--- /dev/null
+++ b/walk.h
@@ -0,0 +1,9 @@
+#ifndef WALK_H
+#define WALK_H
+
+#include <dmlite/cpp/dmlite.h>
+#include <dmlite/cpp/catalog.h>
+
+void walk(dmlite::Catalog *acatalog, const char *path, const char *target) throw(dmlite::DmException);
+
+#endif