blob: 4d20e98568964b3ca72f1546971623a09ec36ab4 [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 Afanasyevf2890632013-01-02 13:40:02 -080034
35void
36usage ()
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 Afanasyeva199f972013-01-02 19:37:26 -080048
Alexander Afanasyevf2890632013-01-02 13:40:02 -080049int
50main (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 Afanasyeva199f972013-01-02 19:37:26 -080065
Alexander Afanasyev5e9e46e2013-01-02 14:12:50 -080066 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 Afanasyeva199f972013-01-02 19:37:26 -080083
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 Afanasyeva199f972013-01-02 19:37:26 -080096 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 Afanasyev433ecda2013-01-02 22:13:45 -0800102 fileStats.st_atime, fileStats.st_mtime, fileStats.st_ctime,
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800103 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 Afanasyevee7e6132013-01-03 20:03:14 -0800130 else
131 {
132 cerr << "ERROR: Unknown command " << cmd << endl;
133 usage ();
134 }
Alexander Afanasyev5e9e46e2013-01-02 14:12:50 -0800135 }
136 else
137 {
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800138 cerr << "ERROR: Cannot connect to the daemon\n";
Alexander Afanasyev5e9e46e2013-01-02 14:12:50 -0800139 status = 1;
140 }
141 }
142 catch (const Ice::Exception& ex)
143 {
144 cerr << ex << endl;
145 status = 1;
146 }
Alexander Afanasyevf2890632013-01-02 13:40:02 -0800147
Alexander Afanasyev5e9e46e2013-01-02 14:12:50 -0800148 if (ic)
149 ic->destroy();
150 return status;
Alexander Afanasyevf2890632013-01-02 13:40:02 -0800151}