2005-11-18 18:54:58 +03:00
|
|
|
/*
|
2006-01-20 17:20:24 +03:00
|
|
|
* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
|
2005-11-18 18:54:58 +03:00
|
|
|
* See LICENSE file for license details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2006-01-30 21:08:58 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
2006-02-08 03:05:19 +03:00
|
|
|
#include <time.h>
|
2005-11-18 18:54:58 +03:00
|
|
|
|
2006-02-02 15:20:07 +03:00
|
|
|
#include <ixp.h>
|
2006-01-30 21:08:58 +03:00
|
|
|
|
|
|
|
static IXPClient c = { 0 };
|
2005-11-18 18:54:58 +03:00
|
|
|
|
2006-02-04 17:31:52 +03:00
|
|
|
static char version[] = "wmiir - " VERSION ", (C)opyright MMIV-MMVI Anselm R. Garbe\n";
|
2005-11-18 18:54:58 +03:00
|
|
|
|
2005-12-21 18:18:11 +03:00
|
|
|
static void
|
|
|
|
usage()
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
2006-03-23 00:16:26 +03:00
|
|
|
fprintf(stderr, "%s", "usage: wmiir [-a <address>] [-v] create | read | remove | write <file>\n");
|
|
|
|
exit(1);
|
2005-11-18 18:54:58 +03:00
|
|
|
}
|
|
|
|
|
2005-12-21 18:18:11 +03:00
|
|
|
static void
|
2006-01-30 21:08:58 +03:00
|
|
|
write_data(unsigned int fid)
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
2006-01-30 21:08:58 +03:00
|
|
|
void *data = cext_emallocz(c.fcall.iounit);
|
|
|
|
unsigned long long offset = 0;
|
2006-03-01 13:55:46 +03:00
|
|
|
unsigned int len = 0;
|
2006-01-30 21:08:58 +03:00
|
|
|
|
|
|
|
while((len = read(0, data, c.fcall.iounit)) > 0) {
|
2006-03-23 00:16:26 +03:00
|
|
|
if(ixp_client_write(&c, fid, offset, len, data) != len) {
|
|
|
|
fprintf(stderr, "wmiir: cannot write file: %s\n", c.errstr);
|
|
|
|
break;
|
|
|
|
}
|
2006-01-30 21:08:58 +03:00
|
|
|
offset += len;
|
2006-03-23 00:16:26 +03:00
|
|
|
}
|
2006-02-28 13:51:45 +03:00
|
|
|
if(offset == 0) /* do an explicit empty write when no writing has been done yet */
|
|
|
|
if(ixp_client_write(&c, fid, offset, 0, 0) != 0)
|
2006-03-23 18:12:06 +03:00
|
|
|
fprintf(stderr, "wmiir: cannot write file: %s\n", c.errstr);
|
2006-01-30 21:08:58 +03:00
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
xcreate(char *file)
|
|
|
|
{
|
2006-03-23 00:16:26 +03:00
|
|
|
unsigned int fid;
|
|
|
|
char *p = strrchr(file, '/');
|
2006-01-30 21:08:58 +03:00
|
|
|
|
2006-03-23 00:16:26 +03:00
|
|
|
fid = c.root_fid << 2;
|
|
|
|
/* walk to bottom-most directory */
|
|
|
|
*p = 0;
|
|
|
|
if(ixp_client_walk(&c, fid, file) == -1) {
|
|
|
|
fprintf(stderr, "wmiir: cannot walk to '%s': %s\n", file, c.errstr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
if(ixp_client_create(&c, fid, p, DMWRITE, IXP_OWRITE) == -1) {
|
|
|
|
fprintf(stderr, "wmiir: cannot create file '%s': %s\n", p, c.errstr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return ixp_client_close(&c, fid);
|
2006-01-30 21:08:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2006-03-06 22:15:43 +03:00
|
|
|
xwrite(char *file, unsigned char mode)
|
2006-01-30 21:08:58 +03:00
|
|
|
{
|
2006-03-23 00:16:26 +03:00
|
|
|
/* open */
|
|
|
|
unsigned int fid = c.root_fid << 2;
|
|
|
|
if(ixp_client_open(&c, fid, file, mode) == -1) {
|
|
|
|
fprintf(stderr, "wmiir: cannot open file '%s': %s\n", file, c.errstr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
write_data(fid);
|
|
|
|
return ixp_client_close(&c, fid);
|
2006-01-30 21:08:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2006-02-08 03:05:19 +03:00
|
|
|
comp_stat(const void *s1, const void *s2)
|
2006-01-30 21:08:58 +03:00
|
|
|
{
|
2006-02-08 03:05:19 +03:00
|
|
|
Stat *st1 = (Stat *)s1;
|
|
|
|
Stat *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 *
|
2006-03-01 09:51:04 +03:00
|
|
|
mode2str(unsigned int mode)
|
2006-02-08 03:05:19 +03:00
|
|
|
{
|
|
|
|
static char buf[16];
|
|
|
|
|
|
|
|
if(mode & DMDIR)
|
|
|
|
buf[0]='d';
|
|
|
|
else
|
|
|
|
buf[0]='-';
|
|
|
|
|
|
|
|
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 *
|
2006-03-01 09:51:04 +03:00
|
|
|
time2str(unsigned int t)
|
2006-02-08 03:05:19 +03:00
|
|
|
{
|
|
|
|
static char buf[32];
|
|
|
|
cext_strlcpy(buf, ctime((time_t *)&t), sizeof(buf));
|
|
|
|
buf[strlen(buf) - 1] = 0;
|
|
|
|
return buf;
|
2006-01-30 21:08:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
xls(void *result, unsigned int msize)
|
|
|
|
{
|
2006-03-01 13:55:46 +03:00
|
|
|
unsigned int n = 0, i = 0;
|
2006-03-23 00:16:26 +03:00
|
|
|
void *p = result;
|
2006-02-08 03:05:19 +03:00
|
|
|
Stat *dir;
|
2006-03-23 00:16:26 +03:00
|
|
|
static Stat stat;
|
|
|
|
|
|
|
|
do {
|
|
|
|
p = ixp_dec_stat(p, &stat);
|
2006-01-30 21:08:58 +03:00
|
|
|
n++;
|
2006-03-23 00:16:26 +03:00
|
|
|
}
|
|
|
|
while(p - result < msize);
|
2006-02-08 03:05:19 +03:00
|
|
|
dir = (Stat *)cext_emallocz(sizeof(Stat) * n);
|
2006-01-30 21:08:58 +03:00
|
|
|
p = result;
|
|
|
|
do {
|
2006-03-23 00:16:26 +03:00
|
|
|
p = ixp_dec_stat(p, &dir[i++]);
|
|
|
|
}
|
|
|
|
while(p - result < msize);
|
2006-02-08 03:05:19 +03:00
|
|
|
qsort(dir, n, sizeof(Stat), comp_stat);
|
|
|
|
for(i = 0; i < n; i++) {
|
2006-03-23 00:16:26 +03:00
|
|
|
fprintf(stdout, "%s %s %s %5llu %s %s\n", mode2str(dir[i].mode),
|
|
|
|
dir[i].uid, dir[i].gid, dir[i].length,
|
|
|
|
time2str(dir[i].mtime), dir[i].name);
|
2006-01-30 21:08:58 +03:00
|
|
|
}
|
|
|
|
free(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
xread(char *file)
|
|
|
|
{
|
2006-03-23 00:16:26 +03:00
|
|
|
unsigned int fid = c.root_fid << 2;
|
|
|
|
int count, is_directory = 0;
|
|
|
|
static unsigned char result[IXP_MAX_MSG];
|
2006-02-06 11:20:23 +03:00
|
|
|
void *dircontent = nil;
|
2006-03-01 13:55:46 +03:00
|
|
|
unsigned int dircontentsz = 0;
|
|
|
|
unsigned int ndircontent = 0;
|
2006-01-30 21:08:58 +03:00
|
|
|
unsigned long long offset = 0;
|
|
|
|
|
2006-03-23 00:16:26 +03:00
|
|
|
if(ixp_client_open(&c, fid, file, IXP_OREAD) == -1) {
|
|
|
|
fprintf(stderr, "wmiir: cannot open file '%s': %s\n", file, c.errstr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
is_directory = !c.fcall.nwqid || (c.fcall.qid.type == IXP_QTDIR);
|
2006-01-30 21:08:58 +03:00
|
|
|
|
|
|
|
while((count = ixp_client_read(&c, fid, offset, result, IXP_MAX_MSG)) > 0) {
|
2006-02-06 11:20:23 +03:00
|
|
|
if(is_directory) {
|
|
|
|
if(ndircontent + count > dircontentsz) {
|
|
|
|
void *tmp = dircontent;
|
|
|
|
if(!dircontentsz)
|
|
|
|
dircontentsz = IXP_MAX_MSG;
|
|
|
|
else
|
|
|
|
dircontentsz *= 2;
|
|
|
|
dircontent = cext_emallocz(dircontentsz);
|
|
|
|
if(tmp) {
|
|
|
|
memcpy(dircontent, tmp, ndircontent);
|
|
|
|
free(tmp);
|
|
|
|
}
|
|
|
|
}
|
2006-03-23 18:12:06 +03:00
|
|
|
memcpy(dircontent + ndircontent, result, count);
|
2006-02-06 11:20:23 +03:00
|
|
|
ndircontent += count;
|
|
|
|
}
|
2006-01-30 21:08:58 +03:00
|
|
|
else {
|
|
|
|
unsigned int i;
|
|
|
|
for(i = 0; i < count; i++)
|
|
|
|
fputc(result[i], stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
offset += count;
|
|
|
|
}
|
2006-03-23 00:16:26 +03:00
|
|
|
if(count == -1) {
|
|
|
|
fprintf(stderr, "wmiir: cannot read file/directory '%s': %s\n", file, c.errstr);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-02-07 19:55:33 +03:00
|
|
|
if(is_directory && ndircontent)
|
2006-02-06 11:20:23 +03:00
|
|
|
xls(dircontent, ndircontent);
|
2006-03-23 00:16:26 +03:00
|
|
|
return ixp_client_close(&c, fid);
|
2006-01-30 21:08:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
xremove(char *file)
|
|
|
|
{
|
2006-03-23 00:16:26 +03:00
|
|
|
unsigned int fid;
|
2006-01-30 21:08:58 +03:00
|
|
|
|
2006-03-23 00:16:26 +03:00
|
|
|
fid = c.root_fid << 2;
|
|
|
|
if(ixp_client_remove(&c, fid, file) == -1) {
|
|
|
|
fprintf(stderr, "wmiir: cannot remove file '%s': %s\n", file, c.errstr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
2005-11-18 18:54:58 +03:00
|
|
|
}
|
|
|
|
|
2005-12-21 18:18:11 +03:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
2006-03-23 00:16:26 +03:00
|
|
|
int ret = 0, i = 0;
|
|
|
|
char *cmd, *file, *address = getenv("WMII_ADDRESS");
|
2005-11-18 18:54:58 +03:00
|
|
|
|
2006-03-23 00:16:26 +03:00
|
|
|
/* command line args */
|
2006-01-30 21:08:58 +03:00
|
|
|
if(argc < 2)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
|
|
|
|
switch (argv[i][1]) {
|
|
|
|
case 'v':
|
2006-03-22 16:25:21 +03:00
|
|
|
fprintf(stdout, "%s", version);
|
2006-01-30 21:08:58 +03:00
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
if(i + 1 < argc)
|
2006-02-01 16:03:45 +03:00
|
|
|
address = argv[++i];
|
2006-01-30 21:08:58 +03:00
|
|
|
else
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cmd = argv[argc - 2];
|
|
|
|
file = argv[argc - 1];
|
|
|
|
|
2006-03-23 00:16:26 +03:00
|
|
|
if(!address) {
|
|
|
|
fprintf(stderr, "%s", "wmiir: error: $WMII_ADDRESS not set\n");
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ixp_client_dial(&c, address, getpid()) == -1) {
|
|
|
|
fprintf(stderr, "wmiir: %s\n", c.errstr);
|
|
|
|
exit(1);
|
|
|
|
}
|
2005-11-18 18:54:58 +03:00
|
|
|
|
2006-03-06 23:17:31 +03:00
|
|
|
if(!strncmp(cmd, "create", 7))
|
2006-02-24 19:20:25 +03:00
|
|
|
ret = xcreate(file);
|
2006-01-30 21:08:58 +03:00
|
|
|
else if(!strncmp(cmd, "read", 5))
|
2006-02-24 19:20:25 +03:00
|
|
|
ret = xread(file);
|
2006-01-30 21:08:58 +03:00
|
|
|
else if(!strncmp(cmd, "remove", 7))
|
2006-02-24 19:20:25 +03:00
|
|
|
ret = xremove(file);
|
2006-03-06 22:15:43 +03:00
|
|
|
else if(!strncmp(cmd, "write", 6))
|
|
|
|
ret = xwrite(file, IXP_OWRITE);
|
2006-01-30 21:08:58 +03:00
|
|
|
else
|
|
|
|
usage();
|
|
|
|
|
2006-03-23 00:16:26 +03:00
|
|
|
/* close socket */
|
|
|
|
ixp_client_hangup(&c);
|
2006-03-23 18:12:06 +03:00
|
|
|
|
2006-03-23 00:16:26 +03:00
|
|
|
return ret;
|
2005-11-18 18:54:58 +03:00
|
|
|
}
|