Alexander Afanasyev | f289063 | 2013-01-02 13:40:02 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | * Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 20 | */ |
| 21 | |
| 22 | #include <iostream> |
| 23 | #include <boost/algorithm/string.hpp> |
| 24 | #include <config.h> |
Alexander Afanasyev | 5e9e46e | 2013-01-02 14:12:50 -0800 | [diff] [blame] | 25 | #include <Ice/Ice.h> |
| 26 | #include <chronoshare-client.ice.h> |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 27 | #include <sys/stat.h> |
| 28 | #include <hash-helper.h> |
| 29 | |
Alexander Afanasyev | f289063 | 2013-01-02 13:40:02 -0800 | [diff] [blame] | 30 | |
| 31 | using namespace std; |
| 32 | using namespace boost; |
Alexander Afanasyev | 5e9e46e | 2013-01-02 14:12:50 -0800 | [diff] [blame] | 33 | using namespace ChronoshareClient; |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 34 | namespace fs = boost::filesystem; |
Alexander Afanasyev | f289063 | 2013-01-02 13:40:02 -0800 | [diff] [blame] | 35 | |
| 36 | void |
| 37 | usage () |
| 38 | { |
| 39 | cerr << "Usage: chronoshare <cmd> [<options>]\n" |
| 40 | << "\n" |
| 41 | << " <cmd> is one of:\n" |
| 42 | << " version\n" |
| 43 | << " update <filename>\n" |
| 44 | << " delete <filename>\n" |
| 45 | << " move <filename> <filename>\n"; |
| 46 | exit (1); |
| 47 | } |
| 48 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 49 | |
Alexander Afanasyev | f289063 | 2013-01-02 13:40:02 -0800 | [diff] [blame] | 50 | int |
| 51 | main (int argc, char **argv) |
| 52 | { |
| 53 | if (argc < 2) |
| 54 | { |
| 55 | usage (); |
| 56 | } |
| 57 | |
| 58 | string cmd = argv[1]; |
| 59 | algorithm::to_lower (cmd); |
| 60 | |
| 61 | if (cmd == "version") |
| 62 | { |
| 63 | cout << "chronoshare version " << CHRONOSHARE_VERSION << endl; |
| 64 | exit (0); |
| 65 | } |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 66 | |
Alexander Afanasyev | 5e9e46e | 2013-01-02 14:12:50 -0800 | [diff] [blame] | 67 | int status = 0; |
| 68 | Ice::CommunicatorPtr ic; |
| 69 | try |
| 70 | { |
| 71 | // Create a communicator |
| 72 | // |
| 73 | ic = Ice::initialize (argc, argv); |
| 74 | |
| 75 | Ice::ObjectPrx base = ic->stringToProxy("NotifyDaemon:default -p 55436"); |
| 76 | if (!base) |
| 77 | { |
| 78 | throw "Could not create proxy"; |
| 79 | } |
| 80 | |
| 81 | NotifyPrx notify = NotifyPrx::checkedCast (base); |
| 82 | if (notify) |
| 83 | { |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 84 | |
| 85 | if (cmd == "update") |
| 86 | { |
| 87 | if (argc != 3) |
| 88 | { |
| 89 | usage (); |
| 90 | } |
| 91 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 92 | fs::path file (argv[2]); |
| 93 | fs::file_status fileStatus = fs::status (file); |
| 94 | if (is_regular_file (fileStatus)) |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 95 | { |
| 96 | // Alex: the following code is platform specific :( |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 97 | HashPtr fileHash = Hash::FromFileContent (file); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 98 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 99 | notify->updateFile (file.generic_string (), |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 100 | make_pair(reinterpret_cast<const ::Ice::Byte*> (fileHash->GetHash ()), |
| 101 | reinterpret_cast<const ::Ice::Byte*> (fileHash->GetHash ()) + |
| 102 | fileHash->GetHashBytes ()), |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 103 | fs::last_write_time (file), |
| 104 | // fileStats.st_atime, fileStats.st_mtime, fileStats.st_ctime, |
| 105 | fileStatus.permissions ()); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 106 | } |
| 107 | else |
| 108 | { |
| 109 | cerr << "File [" << argv[2] << "] doesn't exist or is not accessible" << endl; |
| 110 | return 1; |
| 111 | } |
| 112 | } |
| 113 | else if (cmd == "delete") |
| 114 | { |
| 115 | if (argc != 3) |
| 116 | { |
| 117 | usage (); |
| 118 | } |
| 119 | |
| 120 | notify->deleteFile (argv[2]); |
| 121 | } |
| 122 | else if (cmd == "move") |
| 123 | { |
| 124 | if (argc != 4) |
| 125 | { |
| 126 | usage (); |
| 127 | } |
| 128 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 129 | fs::path srcFile (argv[2]); |
| 130 | fs::path dstFile (argv[3]); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 131 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 132 | notify->moveFile (srcFile.generic_string (), dstFile.generic_string ()); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 133 | } |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 134 | else |
| 135 | { |
| 136 | cerr << "ERROR: Unknown command " << cmd << endl; |
| 137 | usage (); |
| 138 | } |
Alexander Afanasyev | 5e9e46e | 2013-01-02 14:12:50 -0800 | [diff] [blame] | 139 | } |
| 140 | else |
| 141 | { |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 142 | cerr << "ERROR: Cannot connect to the daemon\n"; |
Alexander Afanasyev | 5e9e46e | 2013-01-02 14:12:50 -0800 | [diff] [blame] | 143 | status = 1; |
| 144 | } |
| 145 | } |
| 146 | catch (const Ice::Exception& ex) |
| 147 | { |
| 148 | cerr << ex << endl; |
| 149 | status = 1; |
| 150 | } |
Alexander Afanasyev | f289063 | 2013-01-02 13:40:02 -0800 | [diff] [blame] | 151 | |
Alexander Afanasyev | 5e9e46e | 2013-01-02 14:12:50 -0800 | [diff] [blame] | 152 | if (ic) |
| 153 | ic->destroy(); |
| 154 | return status; |
Alexander Afanasyev | f289063 | 2013-01-02 13:40:02 -0800 | [diff] [blame] | 155 | } |