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 | f289063 | 2013-01-02 13:40:02 -0800 | [diff] [blame] | 34 | |
| 35 | void |
| 36 | usage () |
| 37 | { |
| 38 | cerr << "Usage: chronoshare <cmd> [<options>]\n" |
| 39 | << "\n" |
| 40 | << " <cmd> is one of:\n" |
| 41 | << " version\n" |
| 42 | << " update <filename>\n" |
| 43 | << " delete <filename>\n" |
| 44 | << " move <filename> <filename>\n"; |
| 45 | exit (1); |
| 46 | } |
| 47 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 48 | |
Alexander Afanasyev | f289063 | 2013-01-02 13:40:02 -0800 | [diff] [blame] | 49 | int |
| 50 | main (int argc, char **argv) |
| 51 | { |
| 52 | if (argc < 2) |
| 53 | { |
| 54 | usage (); |
| 55 | } |
| 56 | |
| 57 | string cmd = argv[1]; |
| 58 | algorithm::to_lower (cmd); |
| 59 | |
| 60 | if (cmd == "version") |
| 61 | { |
| 62 | cout << "chronoshare version " << CHRONOSHARE_VERSION << endl; |
| 63 | exit (0); |
| 64 | } |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 65 | |
Alexander Afanasyev | 5e9e46e | 2013-01-02 14:12:50 -0800 | [diff] [blame] | 66 | int status = 0; |
| 67 | Ice::CommunicatorPtr ic; |
| 68 | try |
| 69 | { |
| 70 | // Create a communicator |
| 71 | // |
| 72 | ic = Ice::initialize (argc, argv); |
| 73 | |
| 74 | Ice::ObjectPrx base = ic->stringToProxy("NotifyDaemon:default -p 55436"); |
| 75 | if (!base) |
| 76 | { |
| 77 | throw "Could not create proxy"; |
| 78 | } |
| 79 | |
| 80 | NotifyPrx notify = NotifyPrx::checkedCast (base); |
| 81 | if (notify) |
| 82 | { |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 83 | |
| 84 | if (cmd == "update") |
| 85 | { |
| 86 | if (argc != 3) |
| 87 | { |
| 88 | usage (); |
| 89 | } |
| 90 | |
| 91 | struct stat fileStats; |
| 92 | int ok = stat (argv[2], &fileStats); |
| 93 | if (ok == 0) |
| 94 | { |
| 95 | // Alex: the following code is platform specific :( |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 96 | HashPtr fileHash = Hash::FromFileContent (argv[2]); |
| 97 | |
| 98 | notify->updateFile (argv[2], |
| 99 | make_pair(reinterpret_cast<const ::Ice::Byte*> (fileHash->GetHash ()), |
| 100 | reinterpret_cast<const ::Ice::Byte*> (fileHash->GetHash ()) + |
| 101 | fileHash->GetHashBytes ()), |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 102 | fileStats.st_atime, fileStats.st_mtime, fileStats.st_ctime, |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 103 | fileStats.st_mode); |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | cerr << "File [" << argv[2] << "] doesn't exist or is not accessible" << endl; |
| 108 | return 1; |
| 109 | } |
| 110 | } |
| 111 | else if (cmd == "delete") |
| 112 | { |
| 113 | if (argc != 3) |
| 114 | { |
| 115 | usage (); |
| 116 | } |
| 117 | |
| 118 | notify->deleteFile (argv[2]); |
| 119 | } |
| 120 | else if (cmd == "move") |
| 121 | { |
| 122 | if (argc != 4) |
| 123 | { |
| 124 | usage (); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | notify->moveFile (argv[2], argv[3]); |
| 129 | } |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 130 | else |
| 131 | { |
| 132 | cerr << "ERROR: Unknown command " << cmd << endl; |
| 133 | usage (); |
| 134 | } |
Alexander Afanasyev | 5e9e46e | 2013-01-02 14:12:50 -0800 | [diff] [blame] | 135 | } |
| 136 | else |
| 137 | { |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 138 | cerr << "ERROR: Cannot connect to the daemon\n"; |
Alexander Afanasyev | 5e9e46e | 2013-01-02 14:12:50 -0800 | [diff] [blame] | 139 | status = 1; |
| 140 | } |
| 141 | } |
| 142 | catch (const Ice::Exception& ex) |
| 143 | { |
| 144 | cerr << ex << endl; |
| 145 | status = 1; |
| 146 | } |
Alexander Afanasyev | f289063 | 2013-01-02 13:40:02 -0800 | [diff] [blame] | 147 | |
Alexander Afanasyev | 5e9e46e | 2013-01-02 14:12:50 -0800 | [diff] [blame] | 148 | if (ic) |
| 149 | ic->destroy(); |
| 150 | return status; |
Alexander Afanasyev | f289063 | 2013-01-02 13:40:02 -0800 | [diff] [blame] | 151 | } |