wmii/cmd/wmiifs.c

479 lines
9.7 KiB
C
Raw Normal View History

2005-11-18 17:54:58 +02:00
/*
2006-01-20 16:20:24 +02:00
* (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
2005-11-18 17:54:58 +02:00
* See LICENSE file for license details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
2005-11-18 17:54:58 +02:00
#include "wmii.h"
/*
* filesystem specification
* / Droot
* /ctl Fctl command interface
* /foobar Dmount
*/
2005-11-18 17:54:58 +02:00
/* 8-bit qid.path.type */
enum {
Droot,
Dmount,
Fctl
2005-11-18 17:54:58 +02:00
};
typedef struct Mount Mount;
struct Mount {
unsigned short id;
char wname[256];
char address[256];
2005-11-18 17:54:58 +02:00
};
Qid root_qid;
2005-11-18 17:54:58 +02:00
static Display *dpy;
static IXPServer srv;
static Mount **mount = nil;
static size_t mountsz = 0;
static size_t nmount = 0;
2005-11-18 17:54:58 +02:00
static char version[] = "wmiifs - " VERSION ", (C)opyright MMIV-MMVI Anselm R. Garbe\n";
2005-11-18 17:54:58 +02:00
2005-12-21 17:18:11 +02:00
static void
usage()
2005-11-18 17:54:58 +02:00
{
fprintf(stderr, "usage: wmiifs -a <address> [-v]\n");
2005-12-21 17:18:11 +02:00
exit(1);
2005-11-18 17:54:58 +02:00
}
static Mount *
mount_of_name(char *name)
{
size_t i;
for(i = 0; i < nmount; i++)
if(!strncmp(mount[i]->wname, name, sizeof(mount[i]->wname)))
return mount[i];
return nil;
}
static int
index_of_id(unsigned short id)
{
int i;
for(i = 0; i < nmount; i++)
if(mount[i]->id == id)
return i;
return -1;
}
/* IXP stuff */
2005-12-21 17:18:11 +02:00
static unsigned long long
mkqpath(unsigned char type, unsigned short id)
{
return ((unsigned long long) id << 8) | (unsigned long long) type;
2005-11-18 17:54:58 +02:00
}
static unsigned char
qpath_type(unsigned long long path)
2005-11-18 17:54:58 +02:00
{
return path & 0xff;
2005-11-18 17:54:58 +02:00
}
static unsigned short
qpath_id(unsigned long long path)
2005-11-18 17:54:58 +02:00
{
return (path >> 8) & 0xffff;
2005-11-18 17:54:58 +02:00
}
static char *
qid_to_name(Qid *qid)
2005-11-18 17:54:58 +02:00
{
unsigned char type = qpath_type(qid->path);
unsigned short id = qpath_id(qid->path);
int i;
if(id && ((i = index_of_id(id)) == -1))
return nil;
switch(type) {
case Droot: return "/"; break;
case Dmount: return mount[i]->wname; break;
case Fctl: return "ctl"; break;
default: return nil; break;
}
2005-11-18 17:54:58 +02:00
}
static int
name_to_type(char *name)
2005-11-18 17:54:58 +02:00
{
if(!name || !name[0] || !strncmp(name, "/", 2) || !strncmp(name, "..", 3))
return Droot;
if(!strncmp(name, "ctl", 4))
return Fctl;
if(mount_of_name(name))
return Dmount;
return -1;
}
2005-12-21 17:18:11 +02:00
static int
mkqid(Qid *dir, char *wname, Qid *new, Bool iswalk)
{
Mount *mnt;
int type = name_to_type(wname);
if((dir->type != IXP_QTDIR) || (type == -1))
return -1;
new->dtype = qpath_type(dir->path);
new->version = 0;
switch(type) {
case Droot:
*new = root_qid;
break;
case Dmount:
if(!(mnt = mount_of_name(wname)))
return -1;
new->type = IXP_QTDIR;
new->path = mkqpath(type, mnt->id);
break;
default:
new->type = IXP_QTFILE;
new->path = mkqpath(type, 0);
break;
}
return 0;
}
2005-12-21 17:18:11 +02:00
static char *
xwalk(IXPConn *c, Fcall *fcall)
{
unsigned short nwqid = 0;
Qid dir = root_qid;
IXPMap *m;
if(!(m = ixp_server_fid2map(c, fcall->fid)))
return Enofid;
if(fcall->fid != fcall->newfid && (ixp_server_fid2map(c, fcall->newfid)))
return Enofid;
if(fcall->nwname) {
dir = m->qid;
for(nwqid = 0; (nwqid < fcall->nwname)
&& !mkqid(&dir, fcall->wname[nwqid], &fcall->wqid[nwqid], True); nwqid++) {
/*fprintf(stderr, "wname=%s nwqid=%d\n", fcall->wname[nwqid], nwqid);*/
dir = fcall->wqid[nwqid];
}
if(!nwqid)
return Enofile;
}
/* a fid will only be valid, if the walk was complete */
if(nwqid == fcall->nwname) {
if(fcall->fid != fcall->newfid) {
m = cext_emallocz(sizeof(IXPMap));
c->map = (IXPMap **)cext_array_attach((void **)c->map,
m, sizeof(IXPMap *), &c->mapsz);
}
m->qid = dir;
m->fid = fcall->newfid;
}
fcall->id = RWALK;
fcall->nwqid = nwqid;
ixp_server_respond_fcall(c, fcall);
return nil;
2005-11-18 17:54:58 +02:00
}
static char *
xopen(IXPConn *c, Fcall *fcall)
2005-11-18 17:54:58 +02:00
{
IXPMap *m = ixp_server_fid2map(c, fcall->fid);
if(!m)
return Enofid;
if(!(fcall->mode | IXP_OREAD) && !(fcall->mode | IXP_OWRITE))
return Enomode;
fcall->id = ROPEN;
fcall->qid = m->qid;
fcall->iounit = 2048;
ixp_server_respond_fcall(c, fcall);
return nil;
}
2005-12-21 17:18:11 +02:00
static unsigned int
mkstat(Stat *stat, Qid *dir, char *name, unsigned long long length, unsigned int mode)
{
stat->mode = mode;
stat->atime = stat->mtime = time(0);
cext_strlcpy(stat->uid, getenv("USER"), sizeof(stat->uid));
cext_strlcpy(stat->gid, getenv("USER"), sizeof(stat->gid));
cext_strlcpy(stat->muid, getenv("USER"), sizeof(stat->muid));
cext_strlcpy(stat->name, name, sizeof(stat->name));
stat->length = length;
mkqid(dir, name, &stat->qid, False);
return ixp_sizeof_stat(stat);
}
2005-12-21 17:18:11 +02:00
static unsigned int
type_to_stat(Stat *stat, char *name, Qid *dir)
{
Mount *mnt;
int type = name_to_type(name);
switch (type) {
case Droot:
return mkstat(stat, dir, name, 0, DMDIR | DMREAD | DMEXEC);
break;
case Fctl:
return mkstat(stat, dir, name, 0, DMWRITE);
break;
case Dmount:
if(!(mnt = mount_of_name(name)))
return -1;
return mkstat(stat, dir, name, 0, DMREAD | DMWRITE);
break;
}
return 0;
2005-11-18 17:54:58 +02:00
}
static char *
xremove(IXPConn *c, Fcall *fcall)
2005-11-18 17:54:58 +02:00
{
IXPMap *m = ixp_server_fid2map(c, fcall->fid);
unsigned short id = qpath_id(m->qid.path);
int i;
if(!m)
return Enofid;
if(id && ((i = qpath_id(id)) == -1))
return Enofile;
if((qpath_type(m->qid.path) == Dmount) && (i < nmount)) {
/* TODO: umount */
fcall->id = RREMOVE;
ixp_server_respond_fcall(c, fcall);
return nil;
}
return Enoperm;
}
2005-12-21 17:18:11 +02:00
static char *
xread(IXPConn *c, Fcall *fcall)
{
Stat stat;
IXPMap *m = ixp_server_fid2map(c, fcall->fid);
unsigned short id;
unsigned char *p = fcall->data;
unsigned int len = 0;
int i;
if(!m)
return Enofid;
id = qpath_id(m->qid.path);
if(id && ((i = index_of_id(id)) == -1))
return Enofile;
fcall->count = 0;
if(fcall->offset) {
switch (qpath_type(m->qid.path)) {
case Droot:
/* jump to offset */
len = type_to_stat(&stat, "ctl", &m->qid);
for(i = 0; i < nmount; i++) {
len += type_to_stat(&stat, mount[i]->wname, &m->qid);
if(len <= fcall->offset)
continue;
break;
}
/* offset found, proceeding */
for(; i < nmount; i++) {
len = type_to_stat(&stat, mount[i]->wname, &m->qid);
if(fcall->count + len > fcall->iounit)
break;
fcall->count += len;
p = ixp_enc_stat(p, &stat);
}
break;
default:
break;
}
}
else {
switch (qpath_type(m->qid.path)) {
case Droot:
fcall->count = type_to_stat(&stat, "ctl", &m->qid);
p = ixp_enc_stat(p, &stat);
for(i = 0; i < nmount; i++) {
len = type_to_stat(&stat, mount[i]->wname, &m->qid);
if(fcall->count + len > fcall->iounit)
break;
fcall->count += len;
p = ixp_enc_stat(p, &stat);
}
break;
case Dmount:
/* TODO: */
break;
case Fctl:
return Enoperm;
break;
default:
return "invalid read";
break;
}
}
fcall->id = RREAD;
ixp_server_respond_fcall(c, fcall);
return nil;
2005-11-18 17:54:58 +02:00
}
static char *
xstat(IXPConn *c, Fcall *fcall)
2005-11-18 17:54:58 +02:00
{
IXPMap *m = ixp_server_fid2map(c, fcall->fid);
char *name;
if(!m)
return Enofid;
name = qid_to_name(&m->qid);
/*fprintf(stderr, "xstat: name=%s\n", name);*/
if(!type_to_stat(&fcall->stat, name, &m->qid))
return Enofile;
fcall->id = RSTAT;
ixp_server_respond_fcall(c, fcall);
return nil;
}
2005-12-21 17:18:11 +02:00
static char *
xwrite(IXPConn *c, Fcall *fcall)
{
char buf[256];
IXPMap *m = ixp_server_fid2map(c, fcall->fid);
if(!m)
return Enofid;
switch (qpath_type(m->qid.path)) {
case Fctl:
if(fcall->count == 4) {
memcpy(buf, fcall->data, 4);
buf[4] = 0;
if(!strncmp(buf, "quit", 5)) {
srv.running = 0;
break;
}
}
return Enocommand;
break;
default:
return "invalid write";
break;
}
fcall->id = RWRITE;
ixp_server_respond_fcall(c, fcall);
return nil;
2005-11-18 17:54:58 +02:00
}
2005-12-21 17:18:11 +02:00
static void
do_fcall(IXPConn *c)
2005-11-18 17:54:58 +02:00
{
static Fcall fcall;
unsigned int msize;
char *errstr;
if((msize = ixp_server_receive_fcall(c, &fcall))) {
/*fprintf(stderr, "fcall=%d\n", fcall.id);*/
switch(fcall.id) {
case TVERSION: errstr = wmii_ixp_version(c, &fcall); break;
case TATTACH: errstr = wmii_ixp_attach(c, &fcall); break;
case TWALK: errstr = xwalk(c, &fcall); break;
case TREMOVE: errstr = xremove(c, &fcall); break;
case TOPEN: errstr = xopen(c, &fcall); break;
case TREAD: errstr = xread(c, &fcall); break;
case TWRITE: errstr = xwrite(c, &fcall); break;
case TCLUNK: errstr = wmii_ixp_clunk(c, &fcall); break;
case TSTAT: errstr = xstat(c, &fcall); break;
default: errstr = Enofunc; break;
}
if(errstr)
ixp_server_respond_error(c, &fcall, errstr);
}
2005-11-18 17:54:58 +02:00
}
2005-12-21 17:18:11 +02:00
static void
new_ixp_conn(IXPConn *c)
2005-11-18 17:54:58 +02:00
{
int fd = ixp_accept_sock(c->fd);
if(fd >= 0)
ixp_server_open_conn(c->srv, fd, do_fcall, ixp_server_close_conn);
2005-11-18 17:54:58 +02:00
}
2005-12-21 17:18:11 +02:00
static void
check_x_event(IXPConn *c)
2005-11-18 17:54:58 +02:00
{
2005-12-21 17:18:11 +02:00
XEvent ev;
while(XPending(dpy))
2005-12-21 17:18:11 +02:00
XNextEvent(dpy, &ev);
/* why check them? because X won't kill wmiifs when X dies */
2005-11-18 17:54:58 +02:00
}
/* main */
2005-11-18 17:54:58 +02:00
2005-12-21 17:18:11 +02:00
int
main(int argc, char *argv[])
2005-11-18 17:54:58 +02:00
{
2005-12-21 17:18:11 +02:00
int i;
char *errstr;
char *address = nil;
2005-12-21 17:18:11 +02:00
/* command line args */
for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
switch (argv[i][1]) {
case 'v':
fprintf(stdout, "%s", version);
2005-12-21 17:18:11 +02:00
exit(0);
break;
case 'a':
2005-12-21 17:18:11 +02:00
if(i + 1 < argc)
address = argv[++i];
2005-12-21 17:18:11 +02:00
else
usage();
break;
default:
usage();
break;
}
}
/* just for the case X crashes/gets quit */
dpy = XOpenDisplay(0);
if(!dpy) {
fprintf(stderr, "%s", "wmiifs: cannot open display\n");
exit(1);
}
i = ixp_create_sock(address, &errstr);
if(i < 0) {
fprintf(stderr, "wmiifs: fatal: %s\n", errstr);
exit(1);
}
/* IXP server */
ixp_server_open_conn(&srv, i, new_ixp_conn, ixp_server_close_conn);
root_qid.type = IXP_QTDIR;
root_qid.version = 0;
root_qid.path = mkqpath(Droot, 0);
/* X server */
ixp_server_open_conn(&srv, ConnectionNumber(dpy), check_x_event, nil);
/* main loop */
errstr = ixp_server_loop(&srv);
if(errstr)
fprintf(stderr, "wmiibar: fatal: %s\n", errstr);
/* cleanup */
ixp_server_close(&srv);
XCloseDisplay(dpy);
2005-12-21 17:18:11 +02:00
return errstr ? 1 : 0;
2005-11-18 17:54:58 +02:00
}