1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
|
/* Copyright ©2007-2010 Kris Maglione <maglione.k at Gmail>
* See LICENSE file for license details.
*/
#define IXP_NO_P9_
#define IXP_P9_STRUCTS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <ixp_local.h>
/* Temporary */
#define fatal(...) ixp_eprint("ixpc: fatal: " __VA_ARGS__); \
static IxpClient *client;
static void
usage(void) {
fprintf(stderr,
"usage: %1$s [-a <address>] {create | read | ls [-ld] | remove | write | append} <file>\n"
" %1$s [-a <address>] xwrite <file> <data>\n"
" %1$s -v\n", argv0);
exit(1);
}
/* Utility Functions */
static void
write_data(IxpCFid *fid, char *name) {
void *buf;
long len;
buf = emalloc(fid->iounit);;
do {
len = read(0, buf, fid->iounit);
if(len >= 0 && ixp_write(fid, buf, len) != len)
fatal("cannot write file '%s': %s\n", name, ixp_errbuf());
} while(len > 0);
free(buf);
}
static int
comp_stat(const void *s1, const void *s2) {
Stat *st1, *st2;
st1 = (Stat*)s1;
st2 = (Stat*)s2;
return strcmp(st1->name, st2->name);
}
static void
setrwx(long m, char *s) {
static char *modes[] = {
"---", "--x", "-w-",
"-wx", "r--", "r-x",
"rw-", "rwx",
};
strncpy(s, modes[m], 3);
}
static char *
str_of_mode(uint mode) {
static char buf[16];
buf[0]='-';
if(mode & P9_DMDIR)
buf[0]='d';
buf[1]='-';
setrwx((mode >> 6) & 7, &buf[2]);
setrwx((mode >> 3) & 7, &buf[5]);
setrwx((mode >> 0) & 7, &buf[8]);
buf[11] = 0;
return buf;
}
static char *
str_of_time(uint val) {
static char buf[32];
ctime_r((time_t*)&val, buf);
buf[strlen(buf) - 1] = '\0';
return buf;
}
static void
print_stat(Stat *s, int details) {
if(details)
fprintf(stdout, "%s %s %s %5llu %s %s\n", str_of_mode(s->mode),
s->uid, s->gid, s->length, str_of_time(s->mtime), s->name);
else {
if((s->mode&P9_DMDIR) && strcmp(s->name, "/"))
fprintf(stdout, "%s/\n", s->name);
else
fprintf(stdout, "%s\n", s->name);
}
}
/* Service Functions */
static int
xappend(int argc, char *argv[]) {
IxpCFid *fid;
IxpStat *stat;
char *file;
ARGBEGIN{
default:
usage();
}ARGEND;
file = EARGF(usage());
fid = ixp_open(client, file, P9_OWRITE);
if(fid == nil)
fatal("Can't open file '%s': %s\n", file, ixp_errbuf());
stat = ixp_stat(client, file);
fid->offset = stat->length;
ixp_freestat(stat);
free(stat);
write_data(fid, file);
return 0;
}
static int
xwrite(int argc, char *argv[]) {
IxpCFid *fid;
char *file;
ARGBEGIN{
default:
usage();
}ARGEND;
file = EARGF(usage());
fid = ixp_open(client, file, P9_OWRITE);
if(fid == nil)
fatal("Can't open file '%s': %s\n", file, ixp_errbuf());
write_data(fid, file);
return 0;
}
static int
xawrite(int argc, char *argv[]) {
IxpCFid *fid;
char *file, *buf, *arg;
int nbuf, mbuf, len;
ARGBEGIN{
default:
usage();
}ARGEND;
file = EARGF(usage());
fid = ixp_open(client, file, P9_OWRITE);
if(fid == nil)
fatal("Can't open file '%s': %s\n", file, ixp_errbuf());
nbuf = 0;
mbuf = 128;
buf = emalloc(mbuf);
while(argc) {
arg = ARGF();
len = strlen(arg);
if(nbuf + len > mbuf) {
mbuf <<= 1;
buf = ixp_erealloc(buf, mbuf);
}
memcpy(buf+nbuf, arg, len);
nbuf += len;
if(argc)
buf[nbuf++] = ' ';
}
if(ixp_write(fid, buf, nbuf) == -1)
fatal("cannot write file '%s': %s\n", file, ixp_errbuf());
return 0;
}
static int
xcreate(int argc, char *argv[]) {
IxpCFid *fid;
char *file;
ARGBEGIN{
default:
usage();
}ARGEND;
file = EARGF(usage());
fid = ixp_create(client, file, 0777, P9_OWRITE);
if(fid == nil)
fatal("Can't create file '%s': %s\n", file, ixp_errbuf());
if((fid->qid.type&P9_DMDIR) == 0)
write_data(fid, file);
return 0;
}
static int
xremove(int argc, char *argv[]) {
char *file;
ARGBEGIN{
default:
usage();
}ARGEND;
file = EARGF(usage());
if(ixp_remove(client, file) == 0)
fatal("Can't remove file '%s': %s\n", file, ixp_errbuf());
return 0;
}
static int
xread(int argc, char *argv[]) {
IxpCFid *fid;
char *file, *buf;
int count;
ARGBEGIN{
default:
usage();
}ARGEND;
file = EARGF(usage());
fid = ixp_open(client, file, P9_OREAD);
if(fid == nil)
fatal("Can't open file '%s': %s\n", file, ixp_errbuf());
buf = emalloc(fid->iounit);
while((count = ixp_read(fid, buf, fid->iounit)) > 0)
write(1, buf, count);
if(count == -1)
fatal("cannot read file/directory '%s': %s\n", file, ixp_errbuf());
return 0;
}
static int
xls(int argc, char *argv[]) {
IxpMsg m;
Stat *stat;
IxpCFid *fid;
char *file, *buf;
int lflag, dflag, count, nstat, mstat, i;
lflag = dflag = 0;
ARGBEGIN{
case 'l':
lflag++;
break;
case 'd':
dflag++;
break;
default:
usage();
}ARGEND;
file = EARGF(usage());
stat = ixp_stat(client, file);
if(stat == nil)
fatal("cannot stat file '%s': %s\n", file, ixp_errbuf());
if(dflag || (stat->mode&P9_DMDIR) == 0) {
print_stat(stat, lflag);
ixp_freestat(stat);
return 0;
}
ixp_freestat(stat);
fid = ixp_open(client, file, P9_OREAD);
if(fid == nil)
fatal("Can't open file '%s': %s\n", file, ixp_errbuf());
nstat = 0;
mstat = 16;
stat = emalloc(sizeof(*stat) * mstat);
buf = emalloc(fid->iounit);
while((count = ixp_read(fid, buf, fid->iounit)) > 0) {
m = ixp_message(buf, count, MsgUnpack);
while(m.pos < m.end) {
if(nstat == mstat) {
mstat <<= 1;
stat = ixp_erealloc(stat, sizeof(*stat) * mstat);
}
ixp_pstat(&m, &stat[nstat++]);
}
}
qsort(stat, nstat, sizeof(*stat), comp_stat);
for(i = 0; i < nstat; i++) {
print_stat(&stat[i], lflag);
ixp_freestat(&stat[i]);
}
free(stat);
if(count == -1)
fatal("cannot read directory '%s': %s\n", file, ixp_errbuf());
return 0;
}
typedef struct exectab exectab;
struct exectab {
char *cmd;
int (*fn)(int, char**);
} etab[] = {
{"append", xappend},
{"write", xwrite},
{"xwrite", xawrite},
{"read", xread},
{"create", xcreate},
{"remove", xremove},
{"ls", xls},
{0, 0}
};
int
main(int argc, char *argv[]) {
char *cmd, *address;
exectab *tab;
int ret;
address = getenv("IXP_ADDRESS");
ARGBEGIN{
case 'v':
printf("%s-" VERSION ", ©2007 Kris Maglione\n", argv0);
exit(0);
case 'a':
address = EARGF(usage());
break;
default:
usage();
}ARGEND;
cmd = EARGF(usage());
if(!address)
fatal("$IXP_ADDRESS not set\n");
client = ixp_mount(address);
if(client == nil)
fatal("%s\n", ixp_errbuf());
for(tab = etab; tab->cmd; tab++)
if(strcmp(cmd, tab->cmd) == 0) break;
if(tab->cmd == 0)
usage();
ret = tab->fn(argc, argv);
ixp_unmount(client);
return ret;
}
|