Exemple #1
0
int main(int argc, char** argv)
{
   if (argc < 3)
   {
      help();
      return -1;
   }

   CmdLineParser clp;
   if (clp.parse(argc, argv) < 0)
   {
      help();
      return -1;
   }

   if (clp.m_vParams.size() < 2)
   {
      help();
      return -1;
   }

   int replica_num = 1;
   string ip = "";
   string cluster = "";
   int parallel = 1;  // concurrent uploading multiple files

   bool encryption = false;
   bool smart = false;

   for (map<string, string>::const_iterator i = clp.m_mDFlags.begin(); i != clp.m_mDFlags.end(); ++ i)
   {
      if (i->first == "n")
         replica_num = atoi(i->second.c_str());
      else if (i->first == "a")
         ip = i->second;
      else if (i->first == "c")
         cluster = i->second;
      else if (i->first == "p")
         parallel = atoi(i->second.c_str());
      else
      {
         help();
         return -1;
      }
   }

   for (vector<string>::const_iterator i = clp.m_vSFlags.begin(); i != clp.m_vSFlags.end(); ++ i)
   {
      if (*i == "e")
         encryption = true;
      else if (*i == "smart" )
         smart = true;
      else
      {
         help();
         return -1;
      }
   }

   Sector client;
   if (Utility::login(client) < 0)
      return -1;

   string dstdir = *clp.m_vParams.rbegin();
   clp.m_vParams.pop_back();
   
   SNode attr;
   int r = client.stat(dstdir, attr);
   if ((r < 0) || (!attr.m_bIsDir))
   {
      cerr << "destination directory on Sector does not exist.\n";
      Utility::logout(client);
      return -1;
   }

   bool success = true;

   // upload multiple files/dirs
   for (vector<string>::const_iterator param = clp.m_vParams.begin(); param != clp.m_vParams.end(); ++ param)
   {
      string prefix = "";

      vector<string> fl;
      bool wc = WildCard::isWildCard(*param);
      if (!wc)
      {
         SNode s;
         if (LocalFS::stat(*param, s) < 0)
         {
            cerr << "ERROR: source file does not exist.\n";
            return -1;
         }

         if (s.m_bIsDir)
            prefix = *param;
         else
         {
            size_t pos = param->rfind('/');
            if (pos != string::npos)
               prefix = param->substr(0, pos);
         }

         getFileList(*param, fl);
      }
      else
      {
         size_t pos = param->rfind('/');
         if (pos != string::npos)
            prefix = param->substr(0, pos);

         string path = *param;
         string orig = path;
         size_t p = path.rfind('/');
         if (p == string::npos)
         {
            path = ".";
         }
         else
         {
            path = path.substr(0, p);
            orig = orig.substr(p + 1, orig.length() - p);
         }

         // as this is a wildcard, list all files in the current dir, choose those matched ones
         vector<SNode> curr_fl;
         if (LocalFS::list_dir(path, curr_fl) < 0)
            return -1;

         for (vector<SNode>::iterator s = curr_fl.begin(); s != curr_fl.end(); ++ s)
         {
            // skip "." and ".."
            if ((s->m_strName == ".") || (s->m_strName == ".."))
               continue;

            if (WildCard::match(orig, s->m_strName))
            {
               if (path == ".")
                  getFileList(s->m_strName, fl);
               else
                  getFileList(path + "/" + s->m_strName, fl);
            }
         }
      }

      // upload all files in the file list
      for (vector<string>::const_iterator i = fl.begin(); i != fl.end(); ++ i)
      {
         // process directory name change: /src/mydata -> /dst/mydata
         string dst = *i;
         if (prefix.length() > 0)
            dst.replace(0, prefix.length(), dstdir + "/");
         else
            dst = dstdir + "/" + dst;

         SNode s;
         if (LocalFS::stat(*i, s) < 0)
            continue;

         if (s.m_bIsDir)
            client.mkdir(dst);
         else
         {
   
            int result = upload(i->c_str(), dst.c_str(), client, replica_num, ip, cluster, encryption, smart);
            if ((result == SectorError::E_CONNECTION) ||
                (result == SectorError::E_BROKENPIPE))
            {
               // connection fail, retry once.
               result = upload(i->c_str(), dst.c_str(), client, replica_num, ip, cluster, encryption, smart);
            }
 
            if (result < 0)
            {
               // failed, remove the file in Sector.
               client.remove(dst);
               success = false;
               Utility::logout(client);
               return -1;
            }
         }
      }
   }

   Utility::logout(client);
   return success ? 0 : -1;
}
Exemple #2
0
int main(int argc, char** argv)
{
   if (argc != 2)
   {
      cout << "USAGE: rm <dir>\n";
      return -1;
   }

   Sector client;

   Session s;
   s.loadInfo("../conf/client.conf");

   if (client.init(s.m_ClientConf.m_strMasterIP, s.m_ClientConf.m_iMasterPort) < 0)
      return -1;
   if (client.login(s.m_ClientConf.m_strUserName, s.m_ClientConf.m_strPassword, s.m_ClientConf.m_strCertificate.c_str()) < 0)
      return -1;

   string path = argv[1];
   bool wc = WildCard::isWildCard(path);

   if (!wc)
   {
      int r = client.remove(path);

      if (r == SectorError::E_NOEMPTY)
      {
         if (isRecursive(path))
            client.rmr(path);
      }
      else if (r < 0)
         cout << "ERROR: " << r << " " << SectorError::getErrorMsg(r) << endl;
   }
   else
   {
      string orig = path;
      size_t p = path.rfind('/');
      if (p == string::npos)
         path = "/";
      else
      {
         path = path.substr(0, p);
         orig = orig.substr(p + 1, orig.length() - p);
      }

      vector<SNode> filelist;
      int r = client.list(path, filelist);
      if (r < 0)
         cout << "ERROR: " << r << " " << SectorError::getErrorMsg(r) << endl;

      bool recursive = false;

      vector<string> filtered;
      for (vector<SNode>::iterator i = filelist.begin(); i != filelist.end(); ++ i)
      {
         if (WildCard::match(orig, i->m_strName))
         {
            if (recursive)
               client.rmr(path + "/" + i->m_strName);
            else
            {
               r = client.remove(path + "/" + i->m_strName);

               if (r == SectorError::E_NOEMPTY)
               {
                  recursive = isRecursive(path + "/" + i->m_strName);
                  if (recursive)
                     client.rmr(path + "/" + i->m_strName);
               }
               else if (r < 0)
                  cout << "ERROR: " << r << " " << SectorError::getErrorMsg(r) << endl;
            }
         }
      }
   }

   client.logout();
   client.close();

   return 1;
}