blob: 4f61d525551419ebb6036f21d955776fcdbd38cc [file] [log] [blame]
Alexander Afanasyevf2890632013-01-02 13:40:02 -08001/* -*- 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 Afanasyev5e9e46e2013-01-02 14:12:50 -080025#include <Ice/Ice.h>
26#include <chronoshare-client.ice.h>
Alexander Afanasyeva199f972013-01-02 19:37:26 -080027#include <sys/stat.h>
28#include <hash-helper.h>
29
Alexander Afanasyevf2890632013-01-02 13:40:02 -080030
31using namespace std;
32using namespace boost;
Alexander Afanasyev5e9e46e2013-01-02 14:12:50 -080033using namespace ChronoshareClient;
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080034namespace fs = boost::filesystem;
Alexander Afanasyevf2890632013-01-02 13:40:02 -080035
36void
37usage ()
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 Afanasyeva199f972013-01-02 19:37:26 -080049
Alexander Afanasyevf2890632013-01-02 13:40:02 -080050int
51main (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 Afanasyeva199f972013-01-02 19:37:26 -080066
Alexander Afanasyev5e9e46e2013-01-02 14:12:50 -080067 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 Afanasyeva199f972013-01-02 19:37:26 -080084
85 if (cmd == "update")
86 {
87 if (argc != 3)
88 {
89 usage ();
90 }
91
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080092 fs::path file (argv[2]);
93 fs::file_status fileStatus = fs::status (file);
94 if (is_regular_file (fileStatus))
Alexander Afanasyeva199f972013-01-02 19:37:26 -080095 {
96 // Alex: the following code is platform specific :(
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080097 HashPtr fileHash = Hash::FromFileContent (file);
Alexander Afanasyeva199f972013-01-02 19:37:26 -080098
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080099 notify->updateFile (file.generic_string (),
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800100 make_pair(reinterpret_cast<const ::Ice::Byte*> (fileHash->GetHash ()),
101 reinterpret_cast<const ::Ice::Byte*> (fileHash->GetHash ()) +
102 fileHash->GetHashBytes ()),
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800103 fs::last_write_time (file),
104 // fileStats.st_atime, fileStats.st_mtime, fileStats.st_ctime,
105 fileStatus.permissions ());
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800106 }
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 Afanasyev68f2a952013-01-08 14:34:16 -0800129 fs::path srcFile (argv[2]);
130 fs::path dstFile (argv[3]);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800131
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800132 notify->moveFile (srcFile.generic_string (), dstFile.generic_string ());
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800133 }
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800134 else
135 {
136 cerr << "ERROR: Unknown command " << cmd << endl;
137 usage ();
138 }
Alexander Afanasyev5e9e46e2013-01-02 14:12:50 -0800139 }
140 else
141 {
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800142 cerr << "ERROR: Cannot connect to the daemon\n";
Alexander Afanasyev5e9e46e2013-01-02 14:12:50 -0800143 status = 1;
144 }
145 }
146 catch (const Ice::Exception& ex)
147 {
148 cerr << ex << endl;
149 status = 1;
150 }
Alexander Afanasyevf2890632013-01-02 13:40:02 -0800151
Alexander Afanasyev5e9e46e2013-01-02 14:12:50 -0800152 if (ic)
153 ic->destroy();
154 return status;
Alexander Afanasyevf2890632013-01-02 13:40:02 -0800155}