Reshuffle and simplify.
authorFrantišek Dvořák <valtri@civ.zcu.cz>
Wed, 16 Oct 2013 14:05:59 +0000 (16:05 +0200)
committerFrantišek Dvořák <valtri@civ.zcu.cz>
Wed, 16 Oct 2013 17:49:07 +0000 (19:49 +0200)
run.cpp

diff --git a/run.cpp b/run.cpp
index 9f438f4..1cf730a 100644 (file)
--- a/run.cpp
+++ b/run.cpp
 int main(int argc, char *argv[]) {
        dmlite::PluginManager manager;
        std::string conf = DMLITE_DEFAULT_CONF;
+       std::string operation;
+       std::string path = "/tmp/pokus";
+       //std::string path = "/dpm/zcu.cz/home/vo.org/pokus/xMaster_Splinters_Door2.jpg";
+       std::string arg;
 
        if (argc < 3) {
                std::cout << "Usage: " << argv[0] << " [ DMLITE_CONFIGFILE [ s | d | h | (r|w|c) [PATH] | z [PATH [SIZE]] ] ]" << std::endl;
@@ -55,30 +59,28 @@ int main(int argc, char *argv[]) {
 
        if (argc < 3) return 0;
 
-       switch (argv[2][0]) {
-       case 's':
-               try {
-                       dmlite::ExtendedStat xstat = catalog->extendedStat("pokus");
-                       std::cout << "Success!" << std::endl;
-               }
-               catch (dmlite::DmException& e) {
-                       std::cout << "Could not stat the file." << std::endl
-                                 << "Reason: " << e.what() << std::endl;
-                       return e.code();
-               }
-               break;
+       if (argc >= 4) path = argv[3];
+       if (argc >= 5) arg = argv[4];
 
-       case 'd':
+       try {
+               switch (argv[2][0]) {
+               case 's': {
+                       dmlite::ExtendedStat xstat;
 
-               try {
-                       std::string path;
-                       dmlite::Directory* dir;
+                       operation = "stat the file";
 
-                       path="/tmp/pokus";
-                       if (argc >= 4) path = argv[3];
+                       xstat = catalog->extendedStat(path);
+                       std::cout << "Success!" << std::endl;
 
+                       break;
+               }
+
+               case 'd': {
+                       dmlite::Directory* dir;
                        dmlite::ExtendedStat* xstat;
 
+                       operation = "list the directory";
+
                        dir = catalog->openDir(path);
 
                        std::cout << "Content of the directory " << path << std::endl;
@@ -103,18 +105,14 @@ int main(int argc, char *argv[]) {
                        catalog->closeDir(dir);
                        std::cout << "Success!" << std::endl;
 #endif
+                       break;
                }
-               catch (dmlite::DmException& e) {
-                       std::cout << "Could not open the directory." << std::endl
-                                 << "Reason: " << e.what() << std::endl;
-                       return e.code();
-               }
-               break;
 
-       case 'h':
-               try {
+               case 'h': {
                        std::string d;
 
+                       operation = "change the directory";
+
                        d = catalog->getWorkingDir();
                        std::cout << "Current directory before: '" << d << "'" << std::endl;
 
@@ -122,86 +120,68 @@ int main(int argc, char *argv[]) {
 
                        d = catalog->getWorkingDir();
                        std::cout << "Current directory after: '" << d << "'" << std::endl;
-               } catch (dmlite::DmException& e) {
-                       std::cout << "Error." << std::endl
-                                 << "Reason: " << e.what() << std::endl;
-                       return e.code();
+
+                       break;
                }
-               break;
 
-       case 'r':
-               try {
-                       std::string path;
+               case 'r': {
                        dmlite::Location loc;
                        dmlite::Location::const_iterator i;
                        unsigned n;
 
-                       path = "/tmp/pokus";
-                       //path = "/dpm/zcu.cz/home/vo.org/pokus/xMaster_Splinters_Door2.jpg";
-                       if (argc >= 4) path = argv[3];
+                       operation = "read";
+
                        loc = poolManager->whereToRead(path);
 
                        for (i = loc.begin(), n = 0; i != loc.end(); i++, n++) {
                                std::cout << "Chunk " << n << ": " << i->url.toString() << " (" << i->offset << '-' << i->offset + i->size << ")" << std::endl;
                        }
-               } catch (dmlite::DmException& e) {
-                       std::cout << "Could not read." << std::endl
-                                 << "Reason: " << e.what() << std::endl;
-                       return e.code();
+
+                       break;
                }
-               break;
 
-       case 'w':
-               try {
-                       std::string path;
+               case 'w': {
                        dmlite::Location loc;
                        dmlite::Location::const_iterator i;
                        unsigned n;
 
-                       path = "/tmp/pokus";
-                       //path = "/dpm/zcu.cz/home/vo.org/pokus/xMaster_Splinters_Door2.jpg";
-                       if (argc >= 4) path = argv[3];
+                       operation = "write";
+
                        loc = poolManager->whereToWrite(path);
 
                        for (i = loc.begin(), n = 0; i != loc.end(); i++, n++) {
                                std::cout << "Chunk " << n << ": " << i->url.toString() << " (" << i->offset << '-' << i->offset + i->size << ")" << std::endl;
                        }
-               } catch (dmlite::DmException& e) {
-                       std::cout << "Could not write." << std::endl
-                                 << "Reason: " << e.what() << std::endl;
-                       return e.code();
+
+                       break;
                }
-               break;
-
-       case 'c':
-               try {
-                       std::string path;
-
-                       path = "/tmp/pokus";
-                       if (argc >= 4) path = argv[3];
-                       catalog->create(path, 0644);
-               } catch (dmlite::DmException& e) {
-                       std::cout << "Could not create." << std::endl
-                                 << "Reason: " << e.what() << std::endl;
-                       return e.code();
+
+               case 'c': {
+                       mode_t mode = 0644;
+
+                       operation = "create";
+
+                       if (argc >= 5) mode = atoll(arg.c_str());
+                       catalog->create(path, mode);
+
+                       break;
                }
-               break;
 
-       case 'z':
-               try {
-                       std::string path;
+               case 'z': {
                        size_t size;
 
-                       path = "/tmp/pokus";
+                       operation = "set size";
                        size = 20000;
-                       if (argc >= 4) path = argv[3];
-                       if (argc >= 5) size = atoll(argv[4]);
+                       if (argc >= 5) size = strtoull(arg.c_str(), NULL, 8);
                        catalog->setSize(path, size);
-               } catch (dmlite::DmException& e) {
-                       std::cout << "Could not set size." << std::endl
-                                 << "Reason: " << e.what() << std::endl;
-                       return e.code();
+
+                       break;
                }
+               }
+       } catch (dmlite::DmException& e) {
+               std::cout << "Could not " << operation << "." << std::endl
+                         << "Reason: " << e.what() << std::endl;
+               return e.code();
        }
 
 //     delete catalog;