wmii/cmd/wmifs.c

403 lines
7.8 KiB
C
Raw Normal View History

2005-11-18 18:54:58 +03:00
/*
* (C)opyright MMV Anselm R. Garbe <garbeam at gmail dot com>
* See LICENSE file for license details.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "wmii.h"
#include <cext.h>
/* array indexes for file pointers */
typedef enum {
F_CTL,
F_LAST
2005-12-05 01:45:59 +03:00
} FsIndexes;
2005-11-18 18:54:58 +03:00
typedef struct Route Route;
struct Route {
2005-12-05 01:45:59 +03:00
File dest;
int src;
2005-11-18 18:54:58 +03:00
};
typedef struct Bind Bind;
struct Bind {
2005-12-05 01:45:59 +03:00
IXPClient *client;
Route route[MAX_CONN * MAX_OPEN_FILES];
File *mount;
char *prefix;
2005-11-18 18:54:58 +03:00
};
2005-12-05 01:45:59 +03:00
static Bind zero_bind = { 0 };
2005-11-18 18:54:58 +03:00
static Display *dpy;
static IXPServer *ixps;
2005-12-05 01:45:59 +03:00
static char *sockfile = 0;
static File *files[F_LAST];
static Bind **bindings = 0;
2005-11-18 18:54:58 +03:00
2005-12-05 01:45:59 +03:00
static void quit(void *obj, char *arg);
static void bind(void *obj, char *arg);
static void unbind(void *obj, char *arg);
static Bind *path_to_bind(char *path);
2005-11-18 18:54:58 +03:00
2005-12-05 01:45:59 +03:00
static Action acttbl[] = {
2005-11-18 18:54:58 +03:00
{"quit", quit},
{"unbind", unbind},
{"bind", bind},
{0, 0}
};
2005-12-05 01:45:59 +03:00
static char *version[] = {
2005-11-18 18:54:58 +03:00
"wmifs - window manager improved filesystem - " VERSION "\n"
2005-12-05 01:45:59 +03:00
" (C)opyright MMV Anselm R. Garbe\n", 0
2005-11-18 18:54:58 +03:00
};
2005-12-05 01:45:59 +03:00
static void usage()
2005-11-18 18:54:58 +03:00
{
fprintf(stderr,
2005-12-05 01:45:59 +03:00
"usage: wmifs -s <socket file> [-v]\n"
" -s socket file\n" " -v version info\n");
2005-11-18 18:54:58 +03:00
exit(1);
}
2005-12-05 01:45:59 +03:00
static void quit(void *obj, char *arg)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
int i;
2005-11-18 18:54:58 +03:00
for (i = 0; bindings && bindings[i]; i++) {
if (bindings[i]->mount) {
bindings[i]->mount->content = 0;
ixp_remove(ixps, bindings[i]->prefix);
if (ixps->errstr)
fprintf(stderr, "wmifs: error on quit: remove %s: %s\n",
2005-12-05 01:45:59 +03:00
bindings[i]->prefix, ixps->errstr);
2005-11-18 18:54:58 +03:00
}
/* free stuff */
if (bindings[i]->prefix)
free(bindings[i]->prefix);
free(bindings[i]);
}
free(bindings);
bindings = 0;
ixps->runlevel = SHUTDOWN;
}
2005-12-05 01:45:59 +03:00
static void _unbind(Bind * b)
2005-11-18 18:54:58 +03:00
{
bindings =
2005-12-05 01:45:59 +03:00
(Bind **) detach_item((void **) bindings, b, sizeof(Bind *));
2005-11-18 18:54:58 +03:00
if (b->mount) {
b->mount->content = 0;
ixp_remove(ixps, b->prefix);
if (ixps->errstr)
fprintf(stderr, "wmifs: error on _unbind: remove %s: %s\n",
2005-12-05 01:45:59 +03:00
b->prefix, ixps->errstr);
2005-11-18 18:54:58 +03:00
}
/* free stuff */
deinit_client(b->client);
free(b->prefix);
free(b);
}
2005-12-05 01:45:59 +03:00
static void unbind(void *obj, char *arg)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
Bind *b = path_to_bind(arg);
2005-11-18 18:54:58 +03:00
if (!b) {
fprintf(stderr, "wmifs: unbind: '%s' no such path\n", arg);
return;
}
_unbind(b);
}
2005-12-05 01:45:59 +03:00
static void bind(void *obj, char *arg)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
Bind *b = 0;
char cmd[1024];
char *sfile;
2005-11-18 18:54:58 +03:00
if (!arg)
return;
_strlcpy(cmd, arg, sizeof(cmd));
sfile = strchr(cmd, ' ');
if (!sfile) {
fprintf(stderr,
2005-12-05 01:45:59 +03:00
"wmifs: bind: '%s' without socket argument, ignoring\n",
arg);
return; /* shortcut with empty argument */
2005-11-18 18:54:58 +03:00
}
*sfile = '\0';
sfile++;
if (*sfile == '\0') {
fprintf(stderr,
2005-12-05 01:45:59 +03:00
"wmifs: bind: '%s' without socket argument, ignoring\n",
arg);
return; /* shortcut with empty argument */
2005-11-18 18:54:58 +03:00
}
b = emalloc(sizeof(Bind));
*b = zero_bind;
2005-12-05 22:38:03 +03:00
b->client = init_ixp_client(sfile);
2005-11-18 18:54:58 +03:00
if (!b->client) {
fprintf(stderr,
2005-12-05 01:45:59 +03:00
"wmifs: bind: cannot connect to server '%s', ignoring\n",
sfile);
2005-11-18 18:54:58 +03:00
free(b);
return;
}
b->prefix = strdup(cmd);
b->mount = ixp_create(ixps, b->prefix);
b->mount->content = b->mount; /* shall be a directory */
bindings =
(Bind **) attach_item_end((void **) bindings, b, sizeof(Bind *));
}
2005-12-05 01:45:59 +03:00
static void handle_after_write(IXPServer * s, File * f)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
int i;
size_t len;
2005-11-18 18:54:58 +03:00
for (i = 0; acttbl[i].name; i++) {
len = strlen(acttbl[i].name);
if (!strncmp(acttbl[i].name, (char *) f->content, len)) {
if (strlen(f->content) > len) {
acttbl[i].func(0, &((char *) f->content)[len + 1]);
} else {
acttbl[i].func(0, 0);
}
break;
}
}
}
2005-12-05 01:45:59 +03:00
static Bind *path_to_bind(char *path)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
int i;
2005-11-18 18:54:58 +03:00
for (i = 0; bindings && bindings[i]; i++)
if (!strncmp
2005-12-05 01:45:59 +03:00
(bindings[i]->prefix, path, strlen(bindings[i]->prefix)))
2005-11-18 18:54:58 +03:00
return bindings[i];
return 0;
}
2005-12-05 01:45:59 +03:00
static Bind *fd_to_bind(int fd, int *client_fd)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
File *f = fd_to_file(ixps, fd);
int i, j;
2005-11-18 18:54:58 +03:00
if (!f)
return 0;
for (i = 0; bindings && bindings[i]; i++) {
for (j = 0; j < MAX_CONN * MAX_OPEN_FILES; j++) {
if (&bindings[i]->route[j].dest == f) {
*client_fd = bindings[i]->route[j].src;
return bindings[i];
}
}
}
return 0;
}
2005-12-05 01:45:59 +03:00
static File *fixp_create(IXPServer * s, char *path)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
Bind *b = path_to_bind(path);
size_t len;
2005-11-18 18:54:58 +03:00
if (!b) {
2005-12-05 01:45:59 +03:00
File *f = ixp_create(s, path);
2005-11-18 18:54:58 +03:00
return f;
}
/* route to b */
len = strlen(b->prefix);
b->client->create(b->client, path[len] == '\0' ? "/" : &path[len]);
if (b->client->errstr) {
if (!strcmp(b->client->errstr, DEAD_SERVER))
_unbind(b);
}
return 0;
}
2005-12-05 01:45:59 +03:00
static File *fixp_open(IXPServer * s, char *path)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
Bind *b = path_to_bind(path);
int fd;
size_t len;
2005-11-18 18:54:58 +03:00
if (!b) {
2005-12-05 01:45:59 +03:00
File *f = ixp_open(s, path);
2005-11-18 18:54:58 +03:00
return f;
}
/* route to b */
len = strlen(b->prefix);
fd = b->client->open(b->client, path[len] == '\0' ? "/" : &path[len]);
if (b->client->errstr) {
set_error(s, b->client->errstr);
if (!strcmp(b->client->errstr, DEAD_SERVER))
_unbind(b);
return 0;
}
b->route[fd].src = fd;
return &b->route[fd].dest;
}
2005-12-05 01:45:59 +03:00
static size_t
2005-11-18 18:54:58 +03:00
fixp_read(IXPServer * s, int fd, size_t offset, void *out_buf,
2005-12-05 01:45:59 +03:00
size_t out_buf_len)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
int cfd;
Bind *b = fd_to_bind(fd, &cfd);
size_t result;
2005-11-18 18:54:58 +03:00
if (!b) {
result = ixp_read(s, fd, offset, out_buf, out_buf_len);
return result;
}
/* route to b */
result = seek_read(b->client, cfd, offset, out_buf, out_buf_len);
if (b->client->errstr) {
set_error(s, b->client->errstr);
if (!strcmp(b->client->errstr, DEAD_SERVER))
_unbind(b);
}
return result;
}
static void
fixp_write(IXPServer * s, int fd, size_t offset, void *content,
2005-12-05 01:45:59 +03:00
size_t in_len)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
int cfd;
Bind *b = fd_to_bind(fd, &cfd);
2005-11-18 18:54:58 +03:00
if (!b) {
ixp_write(s, fd, offset, content, in_len);
return;
}
/* route to b */
seek_write(b->client, cfd, offset, content, in_len);
if (b->client->errstr) {
set_error(s, b->client->errstr);
if (!strcmp(b->client->errstr, DEAD_SERVER))
_unbind(b);
}
}
2005-12-05 01:45:59 +03:00
static void fixp_close(IXPServer * s, int fd)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
int cfd;
Bind *b = fd_to_bind(fd, &cfd);
2005-11-18 18:54:58 +03:00
if (!b) {
ixp_close(s, fd);
return;
}
/* route to b */
b->client->close(b->client, cfd);
if (b->client->errstr) {
set_error(s, b->client->errstr);
if (!strcmp(b->client->errstr, DEAD_SERVER))
_unbind(b);
}
}
2005-12-05 01:45:59 +03:00
static void fixp_remove(IXPServer * s, char *path)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
Bind *b = path_to_bind(path);
size_t len;
2005-11-18 18:54:58 +03:00
if (!b) {
ixp_remove(s, path);
return;
}
/* route to b */
len = strlen(b->prefix);
b->client->remove(b->client, path[len] == '\0' ? "/" : &path[len]);
if (b->client->errstr) {
set_error(s, b->client->errstr);
if (!strcmp(b->client->errstr, DEAD_SERVER))
_unbind(b);
}
}
2005-12-05 01:45:59 +03:00
static void check_event(Connection * e)
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
XEvent ev;
2005-11-18 18:54:58 +03:00
while (XPending(dpy)) {
/*
* wmifs isn't interested in any X events, so just drop them
* all
*/
XNextEvent(dpy, &ev);
}
/* why check them? because X won't kill wmifs when X dies */
}
2005-12-05 01:45:59 +03:00
static void run()
2005-11-18 18:54:58 +03:00
{
if (!(files[F_CTL] = ixp_create(ixps, "/ctl"))) {
perror("wmifs: cannot connect IXP server");
exit(1);
}
files[F_CTL]->after_write = handle_after_write;
/* routing functions */
ixps->create = fixp_create;
ixps->remove = fixp_remove;
ixps->open = fixp_open;
ixps->close = fixp_close;
ixps->read = fixp_read;
ixps->write = fixp_write;
/* main event loop */
run_server_with_fd_support(ixps, ConnectionNumber(dpy),
2005-12-05 01:45:59 +03:00
check_event, 0);
2005-11-18 18:54:58 +03:00
deinit_server(ixps);
}
2005-12-05 01:45:59 +03:00
int main(int argc, char *argv[])
2005-11-18 18:54:58 +03:00
{
2005-12-05 01:45:59 +03:00
int i;
2005-11-18 18:54:58 +03:00
/* command line args */
for (i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
switch (argv[i][1]) {
case 'v':
fprintf(stdout, "%s", version[0]);
exit(0);
break;
case 's':
if (i + 1 < argc)
sockfile = argv[++i];
else
usage();
break;
default:
usage();
break;
}
}
if (!getenv("HOME")) {
fprintf(stderr, "%s",
2005-12-05 01:45:59 +03:00
"wmifs: $HOME environment variable is not set\n");
2005-11-18 18:54:58 +03:00
usage();
}
/* just for the case X crashes/gets quit */
dpy = XOpenDisplay(0);
if (!dpy) {
fprintf(stderr, "%s", "wmifs: cannot open display\n");
exit(1);
}
ixps = wmii_setup_server(sockfile);
run();
return 0;
}