2007-03-26 08:48:00 +04:00
|
|
|
/* Copyright ©2007 Kris Maglione <fbsdaemon@gmail.com>
|
|
|
|
* See LICENSE file for license details.
|
|
|
|
*/
|
2007-07-01 15:08:30 +04:00
|
|
|
#define IXP_NO_P9_
|
|
|
|
#define IXP_P9_STRUCTS
|
2007-03-26 08:48:00 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <ixp.h>
|
|
|
|
#include <util.h>
|
2007-07-01 15:08:30 +04:00
|
|
|
#include <fmt.h>
|
2007-03-26 08:48:00 +04:00
|
|
|
|
|
|
|
static IxpClient *client;
|
|
|
|
|
|
|
|
static void
|
2007-05-25 01:42:33 +04:00
|
|
|
usage(void) {
|
2007-07-17 00:52:35 +04:00
|
|
|
fprint(1,
|
2008-01-14 23:42:21 +03:00
|
|
|
"usage: %s [-a <address>] {create | read | ls [-ld] | remove | rm | write} <file>\n"
|
|
|
|
" %s [-a <address>] xwrite <file> <data>\n"
|
|
|
|
" %s -v\n", argv0, argv0, argv0);
|
2007-03-26 08:48:00 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2007-07-01 15:08:30 +04:00
|
|
|
static int
|
|
|
|
errfmt(Fmt *f) {
|
|
|
|
return fmtstrcpy(f, ixp_errbuf());
|
|
|
|
}
|
|
|
|
|
2007-03-26 08:48:00 +04:00
|
|
|
/* Utility Functions */
|
|
|
|
static void
|
2007-03-27 01:21:05 +04:00
|
|
|
write_data(IxpCFid *fid, char *name) {
|
2007-03-26 08:48:00 +04:00
|
|
|
void *buf;
|
2007-05-25 00:49:45 +04:00
|
|
|
int len;
|
2007-03-26 08:48:00 +04:00
|
|
|
|
2007-07-01 15:08:30 +04:00
|
|
|
buf = emalloc(fid->iounit);;
|
2007-03-28 01:39:00 +04:00
|
|
|
do {
|
|
|
|
len = read(0, buf, fid->iounit);
|
2007-05-25 00:03:15 +04:00
|
|
|
if(len > 0 && ixp_write(fid, buf, len) != len)
|
2007-07-01 15:08:30 +04:00
|
|
|
fatal("cannot write file '%s': %r\n", name);
|
2007-03-28 01:39:00 +04:00
|
|
|
} while(len > 0);
|
|
|
|
|
2007-03-26 08:48:00 +04:00
|
|
|
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 *
|
2007-07-17 00:52:35 +04:00
|
|
|
modestr(uint mode) {
|
2007-03-26 08:48:00 +04:00
|
|
|
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 *
|
2007-07-17 00:52:35 +04:00
|
|
|
timestr(uint val) {
|
2007-03-26 08:48:00 +04:00
|
|
|
static char buf[32];
|
|
|
|
|
|
|
|
ctime_r((time_t*)&val, buf);
|
|
|
|
buf[strlen(buf) - 1] = '\0';
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-04-06 23:29:24 +04:00
|
|
|
print_stat(Stat *s, int lflag) {
|
|
|
|
if(lflag)
|
2007-07-17 00:52:35 +04:00
|
|
|
print("%s %s %s %5llud %s %s\n",
|
|
|
|
modestr(s->mode), s->uid, s->gid, s->length,
|
|
|
|
timestr(s->mtime), s->name);
|
2007-03-26 08:48:00 +04:00
|
|
|
else {
|
|
|
|
if((s->mode&P9_DMDIR) && strcmp(s->name, "/"))
|
2007-07-17 00:52:35 +04:00
|
|
|
print("%s/\n", s->name);
|
2007-03-26 08:48:00 +04:00
|
|
|
else
|
2007-07-17 00:52:35 +04:00
|
|
|
print("%s\n", s->name);
|
2007-03-26 08:48:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Service Functions */
|
|
|
|
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)
|
2007-07-01 15:08:30 +04:00
|
|
|
fatal("Can't open file '%s': %r\n", file);
|
2007-03-26 08:48:00 +04:00
|
|
|
|
2007-03-27 01:21:05 +04:00
|
|
|
write_data(fid, file);
|
2007-07-03 18:16:14 +04:00
|
|
|
ixp_close(fid);
|
2007-03-26 08:48:00 +04:00
|
|
|
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)
|
2007-07-01 15:08:30 +04:00
|
|
|
fatal("Can't open file '%s': %r\n", file);
|
2007-03-26 08:48:00 +04:00
|
|
|
|
|
|
|
nbuf = 0;
|
|
|
|
mbuf = 128;
|
2007-07-01 15:08:30 +04:00
|
|
|
buf = emalloc(mbuf);
|
2007-03-26 08:48:00 +04:00
|
|
|
while(argc) {
|
|
|
|
arg = ARGF();
|
|
|
|
len = strlen(arg);
|
2007-07-03 18:16:14 +04:00
|
|
|
if(nbuf + len + 1 > mbuf) {
|
2007-03-26 08:48:00 +04:00
|
|
|
mbuf <<= 1;
|
2007-07-01 15:08:30 +04:00
|
|
|
buf = erealloc(buf, mbuf);
|
2007-03-26 08:48:00 +04:00
|
|
|
}
|
|
|
|
memcpy(buf+nbuf, arg, len);
|
|
|
|
nbuf += len;
|
|
|
|
if(argc)
|
|
|
|
buf[nbuf++] = ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ixp_write(fid, buf, nbuf) == -1)
|
2007-07-01 15:08:30 +04:00
|
|
|
fatal("cannot write file '%s': %r\n", file);
|
2007-07-03 18:16:14 +04:00
|
|
|
ixp_close(fid);
|
|
|
|
free(buf);
|
2007-03-26 08:48:00 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
xcreate(int argc, char *argv[]) {
|
|
|
|
IxpCFid *fid;
|
|
|
|
char *file;
|
|
|
|
|
|
|
|
ARGBEGIN{
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}ARGEND;
|
|
|
|
|
|
|
|
file = EARGF(usage());
|
2007-03-27 01:21:05 +04:00
|
|
|
fid = ixp_create(client, file, 0777, P9_OWRITE);
|
2007-03-26 08:48:00 +04:00
|
|
|
if(fid == nil)
|
2007-07-01 15:08:30 +04:00
|
|
|
fatal("Can't create file '%s': %r\n", file);
|
2007-03-26 08:48:00 +04:00
|
|
|
|
|
|
|
if((fid->qid.type&P9_DMDIR) == 0)
|
2007-03-27 01:21:05 +04:00
|
|
|
write_data(fid, file);
|
2007-07-03 18:16:14 +04:00
|
|
|
ixp_close(fid);
|
2007-03-26 08:48:00 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
xremove(int argc, char *argv[]) {
|
|
|
|
char *file;
|
2008-01-14 23:42:21 +03:00
|
|
|
int n;
|
2007-03-26 08:48:00 +04:00
|
|
|
|
|
|
|
ARGBEGIN{
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}ARGEND;
|
|
|
|
|
|
|
|
file = EARGF(usage());
|
2008-01-14 23:42:21 +03:00
|
|
|
do {
|
|
|
|
if(ixp_remove(client, file) == 0) {
|
|
|
|
fprint(2, "%s: Can't remove file '%s': %r\n", argv0, file);
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}while((file = ARGF()));
|
2007-03-26 08:48:00 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
xread(int argc, char *argv[]) {
|
|
|
|
IxpCFid *fid;
|
|
|
|
char *file, *buf;
|
|
|
|
int count;
|
|
|
|
|
|
|
|
ARGBEGIN{
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}ARGEND;
|
|
|
|
|
2008-01-14 23:42:21 +03:00
|
|
|
if(argc == 0)
|
|
|
|
usage();
|
2007-03-26 08:48:00 +04:00
|
|
|
file = EARGF(usage());
|
2008-01-14 23:42:21 +03:00
|
|
|
do {
|
|
|
|
fid = ixp_open(client, file, P9_OREAD);
|
|
|
|
if(fid == nil)
|
|
|
|
fatal("Can't open file '%s': %r\n", file);
|
2007-03-26 08:48:00 +04:00
|
|
|
|
2008-01-14 23:42:21 +03:00
|
|
|
buf = emalloc(fid->iounit);
|
|
|
|
while((count = ixp_read(fid, buf, fid->iounit)) > 0)
|
|
|
|
write(1, buf, count);
|
|
|
|
ixp_close(fid);
|
2007-03-26 08:48:00 +04:00
|
|
|
|
2008-01-14 23:42:21 +03:00
|
|
|
if(count == -1)
|
|
|
|
fprint(2, "%s: cannot read file/directory '%s': %r\n", argv0, file);
|
|
|
|
}while((file = ARGF()));
|
2007-03-26 08:48:00 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
xls(int argc, char *argv[]) {
|
2007-07-01 15:08:30 +04:00
|
|
|
IxpMsg m;
|
2007-03-26 08:48:00 +04:00
|
|
|
Stat *stat;
|
|
|
|
IxpCFid *fid;
|
2007-04-22 08:07:23 +04:00
|
|
|
char *file;
|
2008-01-18 01:50:35 +03:00
|
|
|
char *buf;
|
2007-03-26 08:48:00 +04:00
|
|
|
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)
|
2007-07-01 15:08:30 +04:00
|
|
|
fatal("cannot stat file '%s': %r\n", file);
|
2007-03-26 08:48:00 +04:00
|
|
|
|
|
|
|
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)
|
2007-07-01 15:08:30 +04:00
|
|
|
fatal("Can't open file '%s': %r\n", file);
|
2007-03-26 08:48:00 +04:00
|
|
|
|
|
|
|
nstat = 0;
|
|
|
|
mstat = 16;
|
2007-07-01 15:08:30 +04:00
|
|
|
stat = emalloc(sizeof(*stat) * mstat);
|
|
|
|
buf = emalloc(fid->iounit);
|
2007-03-26 08:48:00 +04:00
|
|
|
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;
|
2007-05-25 14:25:41 +04:00
|
|
|
stat = erealloc(stat, sizeof(*stat) * mstat);
|
2007-03-26 08:48:00 +04:00
|
|
|
}
|
|
|
|
ixp_pstat(&m, &stat[nstat++]);
|
|
|
|
}
|
|
|
|
}
|
2007-07-03 18:16:14 +04:00
|
|
|
ixp_close(fid);
|
2007-03-26 08:48:00 +04:00
|
|
|
|
|
|
|
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)
|
2007-07-01 15:08:30 +04:00
|
|
|
fatal("cannot read directory '%s': %r\n", file);
|
2007-03-26 08:48:00 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-03-28 01:39:00 +04:00
|
|
|
typedef struct exectab exectab;
|
|
|
|
struct exectab {
|
|
|
|
char *cmd;
|
|
|
|
int (*fn)(int, char**);
|
|
|
|
} etab[] = {
|
|
|
|
{"write", xwrite},
|
|
|
|
{"xwrite", xawrite},
|
|
|
|
{"read", xread},
|
2008-01-14 23:42:21 +03:00
|
|
|
{"cat", xread},
|
2007-03-28 01:39:00 +04:00
|
|
|
{"create", xcreate},
|
|
|
|
{"remove", xremove},
|
2007-05-25 00:03:15 +04:00
|
|
|
{"rm", xremove},
|
2007-03-28 01:39:00 +04:00
|
|
|
{"ls", xls},
|
|
|
|
{0, 0}
|
|
|
|
};
|
|
|
|
|
2007-03-26 08:48:00 +04:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[]) {
|
2008-01-14 23:42:21 +03:00
|
|
|
char *address;
|
2007-03-28 01:39:00 +04:00
|
|
|
exectab *tab;
|
|
|
|
int ret;
|
2007-03-26 08:48:00 +04:00
|
|
|
|
2007-07-01 15:08:30 +04:00
|
|
|
fmtinstall('r', errfmt);
|
|
|
|
|
2007-03-26 08:48:00 +04:00
|
|
|
address = getenv("WMII_ADDRESS");
|
|
|
|
|
|
|
|
ARGBEGIN{
|
|
|
|
case 'v':
|
2007-07-17 00:52:35 +04:00
|
|
|
print("%s-" VERSION ", ©2007 Kris Maglione\n", argv0);
|
2007-03-26 08:48:00 +04:00
|
|
|
exit(0);
|
|
|
|
case 'a':
|
|
|
|
address = EARGF(usage());
|
2007-03-26 23:46:37 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
2007-03-26 08:48:00 +04:00
|
|
|
}ARGEND;
|
|
|
|
|
|
|
|
if(!address)
|
|
|
|
fatal("$WMII_ADDRESS not set\n");
|
|
|
|
|
|
|
|
client = ixp_mount(address);
|
|
|
|
if(client == nil)
|
2007-07-01 15:08:30 +04:00
|
|
|
fatal("can't mount: %r\n");
|
2007-03-26 08:48:00 +04:00
|
|
|
|
2007-03-28 01:39:00 +04:00
|
|
|
for(tab = etab; tab->cmd; tab++)
|
2008-01-14 23:42:21 +03:00
|
|
|
if(strcmp(*argv, tab->cmd) == 0) break;
|
2007-03-28 01:39:00 +04:00
|
|
|
if(tab->cmd == 0)
|
2007-03-26 08:48:00 +04:00
|
|
|
usage();
|
|
|
|
|
2007-03-28 01:39:00 +04:00
|
|
|
ret = tab->fn(argc, argv);
|
|
|
|
|
2007-03-26 08:48:00 +04:00
|
|
|
ixp_unmount(client);
|
|
|
|
return ret;
|
|
|
|
}
|