blob: 8ab80139f87fe95fed482cd9672c416f3d7e9d21 [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 :(
96
97 char atimespec[26], mtimespec[26], ctimespec[26];
98 ctime_r (&fileStats.st_atime, atimespec);
99 ctime_r (&fileStats.st_mtime, mtimespec);
100 ctime_r (&fileStats.st_ctime, ctimespec);
101
102 HashPtr fileHash = Hash::FromFileContent (argv[2]);
103
104 notify->updateFile (argv[2],
105 make_pair(reinterpret_cast<const ::Ice::Byte*> (fileHash->GetHash ()),
106 reinterpret_cast<const ::Ice::Byte*> (fileHash->GetHash ()) +
107 fileHash->GetHashBytes ()),
108 atimespec, mtimespec, ctimespec,
109 fileStats.st_mode);
110 }
111 else
112 {
113 cerr << "File [" << argv[2] << "] doesn't exist or is not accessible" << endl;
114 return 1;
115 }
116 }
117 else if (cmd == "delete")
118 {
119 if (argc != 3)
120 {
121 usage ();
122 }
123
124 notify->deleteFile (argv[2]);
125 }
126 else if (cmd == "move")
127 {
128 if (argc != 4)
129 {
130 usage ();
131 }
132
133
134 notify->moveFile (argv[2], argv[3]);
135 }
Alexander Afanasyev5e9e46e2013-01-02 14:12:50 -0800136 }
137 else
138 {
139 cerr << "Cannot connect to the daemon\n";
140 status = 1;
141 }
142 }
143 catch (const Ice::Exception& ex)
144 {
145 cerr << ex << endl;
146 status = 1;
147 }
Alexander Afanasyevf2890632013-01-02 13:40:02 -0800148
Alexander Afanasyev5e9e46e2013-01-02 14:12:50 -0800149 if (ic)
150 ic->destroy();
151 return status;
Alexander Afanasyevf2890632013-01-02 13:40:02 -0800152}