2005-11-18 18:54:58 +03:00
|
|
|
/*
|
|
|
|
* (C)opyright MMIV-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 "ixp.h"
|
|
|
|
|
|
|
|
#include <cext.h>
|
|
|
|
|
2005-12-05 01:45:59 +03:00
|
|
|
int is_directory(File * f)
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
|
|
|
return !f->size && f->content;
|
|
|
|
}
|
|
|
|
|
2005-12-05 01:45:59 +03:00
|
|
|
File *ixp_create(IXPServer * s, char *path)
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
2005-12-05 01:45:59 +03:00
|
|
|
File *f, *p;
|
|
|
|
char *buf = strdup(path);
|
|
|
|
char *tok, *tok_ptr;
|
2005-11-18 18:54:58 +03:00
|
|
|
|
|
|
|
if (!path)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* cannot create a directory with empty name */
|
|
|
|
if (buf[strlen(buf) - 1] == '/')
|
|
|
|
return 0;
|
|
|
|
tok = strtok_r(buf, "/", &tok_ptr);
|
|
|
|
p = s->root;
|
|
|
|
f = p->content;
|
|
|
|
/* first determine the path as it exists already */
|
|
|
|
while (f && tok) {
|
|
|
|
if (!strcmp(f->name, tok)) {
|
|
|
|
tok = strtok_r(0, "/", &tok_ptr);
|
|
|
|
p = f;
|
|
|
|
if (tok && is_directory(f))
|
|
|
|
f = f->content;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
f = f->next;
|
|
|
|
}
|
|
|
|
if (p->size && tok) {
|
|
|
|
free(buf);
|
2005-12-05 01:45:59 +03:00
|
|
|
return 0; /* cannot create subdirectory, if file has
|
|
|
|
* content */
|
2005-11-18 18:54:58 +03:00
|
|
|
}
|
|
|
|
/* only create missing parts, if file is directory */
|
|
|
|
while (tok) {
|
2005-12-08 03:31:23 +03:00
|
|
|
f = (File *) cext_emallocz(sizeof(File));
|
2005-11-18 18:54:58 +03:00
|
|
|
f->name = strdup(tok);
|
|
|
|
f->parent = p;
|
|
|
|
if (p->content) {
|
|
|
|
p = p->content;
|
|
|
|
while (p->next) {
|
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
p->next = f;
|
|
|
|
} else {
|
|
|
|
p->content = f;
|
|
|
|
}
|
|
|
|
p = f;
|
|
|
|
tok = strtok_r(0, "/", &tok_ptr);
|
|
|
|
}
|
|
|
|
free(buf);
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2005-12-05 01:45:59 +03:00
|
|
|
static int comp_file_name(const void *f1, const void *f2)
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
2005-12-05 01:45:59 +03:00
|
|
|
File *f = (File *) f1;
|
|
|
|
File *p = (File *) f2;
|
2005-11-18 18:54:58 +03:00
|
|
|
return strcmp(*(char **) f->name, *(char **) p->name);
|
|
|
|
}
|
|
|
|
|
2005-12-05 01:45:59 +03:00
|
|
|
static char *_ls(File * f)
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
2005-12-05 01:45:59 +03:00
|
|
|
File *p;
|
|
|
|
char *result = 0;
|
|
|
|
size_t size = 1; /* for \n */
|
|
|
|
int num = 0, i;
|
|
|
|
File **tmp;
|
2005-11-18 18:54:58 +03:00
|
|
|
|
|
|
|
for (p = f; p; p = p->next)
|
|
|
|
num++;
|
2005-12-08 03:31:23 +03:00
|
|
|
tmp = cext_emallocz(sizeof(File *) * num);
|
2005-11-18 18:54:58 +03:00
|
|
|
i = 0;
|
|
|
|
for (p = f; p; p = p->next) {
|
|
|
|
size += strlen(p->name) + 1;
|
|
|
|
if (is_directory(p))
|
|
|
|
size++;
|
|
|
|
tmp[i++] = p;
|
|
|
|
}
|
|
|
|
qsort(tmp, num, sizeof(char *), comp_file_name);
|
2005-12-08 03:31:23 +03:00
|
|
|
result = cext_emallocz(size);
|
2005-11-18 18:54:58 +03:00
|
|
|
for (i = 0; i < num; i++) {
|
|
|
|
strncat(result, tmp[i]->name, size);
|
|
|
|
if (is_directory(tmp[i]))
|
|
|
|
strncat(result, "/\n", size);
|
|
|
|
else
|
|
|
|
strncat(result, "\n", size);
|
|
|
|
}
|
|
|
|
free(tmp);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-12-05 01:45:59 +03:00
|
|
|
File *ixp_open(IXPServer * s, char *path)
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
2005-12-05 01:45:59 +03:00
|
|
|
File *f;
|
2005-11-18 18:54:58 +03:00
|
|
|
|
|
|
|
f = ixp_walk(s, path);
|
|
|
|
if (!f) {
|
|
|
|
set_error(s, "file does not exist");
|
2005-12-14 00:48:52 +03:00
|
|
|
return nil;
|
2005-11-18 18:54:58 +03:00
|
|
|
}
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2005-12-05 01:45:59 +03:00
|
|
|
void ixp_close(IXPServer * s, int fd)
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
2005-12-05 01:45:59 +03:00
|
|
|
File *f = fd_to_file(s, fd);
|
2005-11-18 18:54:58 +03:00
|
|
|
if (!f)
|
|
|
|
set_error(s, "invalid file descriptor");
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
ixp_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
|
|
|
File *f = fd_to_file(s, fd);
|
|
|
|
void *result = 0;
|
|
|
|
size_t len = 0, res_len = 0;
|
2005-11-18 18:54:58 +03:00
|
|
|
|
|
|
|
if (!f) {
|
|
|
|
set_error(s, "invalid file descriptor");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* callback */
|
|
|
|
if (f->before_read)
|
|
|
|
f->before_read(s, f);
|
|
|
|
if (is_directory(f)) {
|
|
|
|
result = _ls(f->content);
|
|
|
|
res_len = strlen(result);
|
|
|
|
} else if (f->size) {
|
|
|
|
result = f->content;
|
|
|
|
res_len = f->size;
|
|
|
|
}
|
|
|
|
if (offset > res_len) {
|
|
|
|
set_error(s, "invalid offset when reading file");
|
|
|
|
if (is_directory(f))
|
|
|
|
free(result);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (result) {
|
|
|
|
len = res_len - offset;
|
|
|
|
if (len > out_buf_len)
|
|
|
|
len = out_buf_len;
|
|
|
|
memcpy(out_buf, (char *) result + offset, len);
|
|
|
|
if (is_directory(f))
|
|
|
|
free(result);
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ixp_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
|
|
|
File *f = fd_to_file(s, fd);
|
2005-11-18 18:54:58 +03:00
|
|
|
|
|
|
|
if (!f) {
|
|
|
|
set_error(s, "invalid file descriptor");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (is_directory(f)) {
|
|
|
|
/* we cannot write to directories */
|
|
|
|
set_error(s, "cannot write to a directory");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (in_len) {
|
|
|
|
/* offset 0 flushes the file */
|
|
|
|
if (!offset || (offset + in_len > f->size)) {
|
|
|
|
f->content = realloc(f->content, offset + in_len + 1);
|
|
|
|
f->size = offset + in_len;
|
|
|
|
}
|
|
|
|
memcpy((char *) f->content + offset, content, in_len);
|
|
|
|
/* internal EOF character */
|
2005-12-08 03:46:45 +03:00
|
|
|
((char *) f->content)[f->size] = 0;
|
2005-11-18 18:54:58 +03:00
|
|
|
} else if (!offset) {
|
|
|
|
/* blank file */
|
|
|
|
if (f->content)
|
|
|
|
free(f->content);
|
|
|
|
f->content = 0;
|
|
|
|
f->size = 0;
|
|
|
|
}
|
|
|
|
/* callback */
|
|
|
|
if (f->after_write)
|
|
|
|
f->after_write(s, f);
|
|
|
|
}
|
|
|
|
|
2005-12-05 01:45:59 +03:00
|
|
|
static void _ixp_remove(IXPServer * s, File * f)
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
|
|
|
if (!f)
|
|
|
|
return;
|
|
|
|
if (f->next) {
|
|
|
|
_ixp_remove(s, f->next);
|
|
|
|
if (s->errstr)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!f->bind && is_directory(f)) {
|
|
|
|
_ixp_remove(s, f->content);
|
|
|
|
if (s->errstr)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (f->content && f->size) {
|
|
|
|
free(f->content);
|
|
|
|
}
|
|
|
|
if (f != s->root) {
|
|
|
|
if (f->name) {
|
|
|
|
free(f->name);
|
|
|
|
}
|
|
|
|
free(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-05 01:45:59 +03:00
|
|
|
void ixp_remove_file(IXPServer * s, File * f)
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
2005-12-05 01:45:59 +03:00
|
|
|
File *p, *n;
|
2005-11-18 18:54:58 +03:00
|
|
|
set_error(s, 0);
|
|
|
|
if (!f) {
|
|
|
|
set_error(s, "file does not exist");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* detach */
|
|
|
|
p = f->parent;
|
|
|
|
n = f->next;
|
|
|
|
f->next = 0;
|
|
|
|
if (p) {
|
|
|
|
if (p->content == f)
|
|
|
|
p->content = n;
|
|
|
|
else {
|
|
|
|
p = p->content;
|
|
|
|
while (p && (p->next != f))
|
|
|
|
p = p->next;
|
|
|
|
if (p)
|
|
|
|
p->next = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* remove now */
|
|
|
|
_ixp_remove(s, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-05 01:45:59 +03:00
|
|
|
void ixp_remove(IXPServer * s, char *path)
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
|
|
|
ixp_remove_file(s, ixp_walk(s, path));
|
|
|
|
}
|
|
|
|
|
2005-12-05 01:45:59 +03:00
|
|
|
File *ixp_walk(IXPServer * s, char *path)
|
2005-11-18 18:54:58 +03:00
|
|
|
{
|
2005-12-05 01:45:59 +03:00
|
|
|
File *f = 0;
|
|
|
|
File *n;
|
|
|
|
char *buf;
|
|
|
|
char *tok, *tok_ptr;
|
2005-11-18 18:54:58 +03:00
|
|
|
|
|
|
|
if (!path) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
buf = strdup(path);
|
|
|
|
|
|
|
|
tok = strtok_r(buf, "/", &tok_ptr);
|
|
|
|
f = s->root->content;
|
|
|
|
if (!tok && buf[0] == '/') {
|
|
|
|
f = s->root;
|
|
|
|
}
|
|
|
|
while (f && tok) {
|
|
|
|
n = f->next;
|
|
|
|
if (!strcmp(f->name, tok)) {
|
|
|
|
tok = strtok_r(0, "/", &tok_ptr);
|
|
|
|
if (tok && f->size)
|
|
|
|
return 0;
|
|
|
|
if (!tok)
|
|
|
|
break;
|
|
|
|
f = f->content;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
f = n;
|
|
|
|
}
|
|
|
|
if (f && (path[strlen(path) - 1] == '/') && !is_directory(f))
|
|
|
|
f = 0;
|
|
|
|
free(buf);
|
|
|
|
return f;
|
|
|
|
}
|