Initial commit
This commit is contained in:
commit
fdfefe7d55
31 changed files with 3242 additions and 0 deletions
46
src/fs/notify.h
Normal file
46
src/fs/notify.h
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
|
||||
/*
|
||||
* Archived file-system notification
|
||||
*
|
||||
* Copyright (C) 2010 Archived
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef _FS_NOTIFY_H
|
||||
|
||||
#define _FS_NOTIFY_H
|
||||
|
||||
/* notify event def's and operations */
|
||||
#include "notify_event.h"
|
||||
|
||||
int notify_init();
|
||||
|
||||
void notify_cleanup();
|
||||
|
||||
int notify_add_watch(const char *path);
|
||||
|
||||
void notify_rm_watch(unsigned int wd);
|
||||
|
||||
void notify_rm_watch_path(const char *path);
|
||||
|
||||
void notify_print_stat();
|
||||
|
||||
notify_event* notify_read();
|
||||
|
||||
void notify_stat();
|
||||
|
||||
int notify_is_ready();
|
||||
|
||||
#endif /* _FS_NOTIFY_H */
|
||||
155
src/fs/notify_event.c
Normal file
155
src/fs/notify_event.c
Normal file
|
|
@ -0,0 +1,155 @@
|
|||
/*
|
||||
* Copyright (C) 2010 Archived
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "notify_event.h"
|
||||
#include "../common/path.h"
|
||||
|
||||
#define dealloc_data(ev) \
|
||||
if (ev->path != NULL) \
|
||||
free(ev->path); \
|
||||
if (ev->filename != NULL) \
|
||||
free(ev->filename)
|
||||
|
||||
static void init_event(notify_event* ev) {
|
||||
|
||||
ev->filename = NULL;
|
||||
ev->path = NULL;
|
||||
ev->type = 0;
|
||||
ev->dir = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Create event
|
||||
*/
|
||||
notify_event* notify_event_new() {
|
||||
|
||||
notify_event *ev = malloc(sizeof(notify_event));
|
||||
|
||||
if(ev == NULL)
|
||||
return NULL;
|
||||
|
||||
init_event(ev);
|
||||
|
||||
return ev;
|
||||
}
|
||||
|
||||
/*
|
||||
* Destroy event
|
||||
*/
|
||||
void notify_event_del(notify_event *event) {
|
||||
|
||||
if(event != NULL)
|
||||
return;
|
||||
|
||||
dealloc_data(event);
|
||||
free(event);
|
||||
}
|
||||
|
||||
/*
|
||||
* dealloc memory and set default values
|
||||
*/
|
||||
void notify_event_clear(notify_event *event) {
|
||||
|
||||
if(event == NULL)
|
||||
return;
|
||||
|
||||
dealloc_data(event);
|
||||
|
||||
// set init values
|
||||
init_event(event);
|
||||
}
|
||||
|
||||
/*
|
||||
* Set event path
|
||||
*/
|
||||
void notify_event_set_path(notify_event *event, const char *path) {
|
||||
|
||||
char *ptr;
|
||||
|
||||
if(event == NULL || path == NULL)
|
||||
return;
|
||||
|
||||
ptr = realloc(event->path, sizeof(char) * (strlen(path)+1));
|
||||
|
||||
if (ptr == NULL)
|
||||
return;
|
||||
|
||||
memcpy(ptr, path, strlen(path)+1);
|
||||
|
||||
event->path = ptr;
|
||||
}
|
||||
|
||||
/*
|
||||
* Set event filename
|
||||
*/
|
||||
void notify_event_set_filename(notify_event *event, const char *filename) {
|
||||
|
||||
char *tmp;
|
||||
|
||||
if(event == NULL || filename == NULL)
|
||||
return;
|
||||
|
||||
|
||||
tmp = realloc(event->filename, sizeof(char) * (strlen(filename)+1));
|
||||
|
||||
if(tmp == NULL)
|
||||
return;
|
||||
|
||||
memcpy(tmp, filename, strlen(filename)+1);
|
||||
|
||||
event->filename = tmp;
|
||||
}
|
||||
|
||||
/* set directory */
|
||||
void notify_event_set_dir(notify_event *event, uint8_t dir) {
|
||||
|
||||
if(event == NULL)
|
||||
return;
|
||||
|
||||
event->dir = dir;
|
||||
}
|
||||
|
||||
void notify_event_set_type(notify_event *event, uint8_t type) {
|
||||
|
||||
if(event == NULL)
|
||||
return;
|
||||
|
||||
event->type = type;
|
||||
}
|
||||
|
||||
const char* notify_event_typetostr(notify_event *event) {
|
||||
|
||||
switch(event->type) {
|
||||
case NOTIFY_CREATE :
|
||||
return "CREATE";
|
||||
case NOTIFY_DELETE :
|
||||
return "DELETE";
|
||||
break;
|
||||
case NOTIFY_MOVE_FROM :
|
||||
return "MOVE_FROM";
|
||||
break;
|
||||
case NOTIFY_MOVE_TO :
|
||||
return "MOVE_TO";
|
||||
break;
|
||||
default:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
56
src/fs/notify_event.h
Normal file
56
src/fs/notify_event.h
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* event data-structure and operation's for notify API
|
||||
*
|
||||
* Copyright (C) 2010 Archived
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef _NOTIFY_EVENT_H
|
||||
|
||||
#define _NOTIFY_EVENT_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/* event types */
|
||||
#define NOTIFY_UNKNOWN (1 << 0)
|
||||
#define NOTIFY_CREATE (1 << 1)
|
||||
#define NOTIFY_DELETE (1 << 2)
|
||||
#define NOTIFY_MOVE_FROM (1 << 3)
|
||||
#define NOTIFY_MOVE_TO (1 << 4)
|
||||
|
||||
typedef struct {
|
||||
uint8_t type; /* type of event */
|
||||
uint8_t dir; /* non zero if event is triggered on a directory */
|
||||
char *path; /* path of the triggered event */
|
||||
char *filename; /* the filename event was triggered on */
|
||||
} notify_event;
|
||||
|
||||
notify_event* notify_event_new();
|
||||
|
||||
void notify_event_del(notify_event *event);
|
||||
|
||||
void notify_event_clear(notify_event *event);
|
||||
|
||||
void notify_event_set_path(notify_event *event, const char *path);
|
||||
|
||||
void notify_event_set_filename(notify_event *event, const char *filename);
|
||||
|
||||
void notify_event_set_dir(notify_event *event, uint8_t dir);
|
||||
|
||||
void notify_event_set_type(notify_event *event, uint8_t type);
|
||||
|
||||
const char* notify_event_typetostr(notify_event *event);
|
||||
|
||||
#endif /* _NOTIFY_EVENT_H */
|
||||
307
src/fs/notify_inotify.c
Normal file
307
src/fs/notify_inotify.c
Normal file
|
|
@ -0,0 +1,307 @@
|
|||
/*
|
||||
* Copyright (C) 2010 Archived
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <errno.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/inotify.h>
|
||||
|
||||
/* red black tree for watch descriptors */
|
||||
#include "../common/rbtree.h"
|
||||
#include "../common/debug.h"
|
||||
#include "../common/path.h"
|
||||
#include "notify.h"
|
||||
|
||||
typedef struct inotify_event inoev;
|
||||
|
||||
#define MAXEVENT 0x200
|
||||
#define PADEVSIZE (sizeof(inoev) + 0x40)
|
||||
#define INOBUFSIZE (MAXEVENT*PADEVSIZE)
|
||||
|
||||
#define WATCH_MASK (IN_MOVE | IN_CREATE | IN_DELETE | IN_ONLYDIR)
|
||||
|
||||
/* Inotify file descriptor */
|
||||
static int fd = 0;
|
||||
|
||||
/* Available bytes on file descriptor */
|
||||
static unsigned int fd_bytes = 0;
|
||||
|
||||
/* redblack tree */
|
||||
static rbtree tree;
|
||||
|
||||
/* inotify queue buffer */
|
||||
static char *inobuf = NULL;
|
||||
|
||||
/* current event */
|
||||
static notify_event event;
|
||||
|
||||
static int rmwatch(unsigned int wd) {
|
||||
|
||||
void *data = rbtree_delete(&tree, wd);
|
||||
|
||||
if (data == NULL)
|
||||
return 0;
|
||||
|
||||
dprint("remove watch: %i %s\n", wd, (char*) data);
|
||||
free(data);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int addwatch(const char *path, const char *name) {
|
||||
|
||||
rbnode *node;
|
||||
char *cpath;
|
||||
int wd;
|
||||
|
||||
cpath = fmt_path(path, name, 1);
|
||||
|
||||
if (cpath == NULL)
|
||||
return -1;
|
||||
|
||||
wd = inotify_add_watch(fd, cpath, WATCH_MASK);
|
||||
|
||||
if (wd < 0) {
|
||||
perror("NOTIFY ADD");
|
||||
dprint("%i path %s\n", errno == EBADF, cpath);
|
||||
return -errno;
|
||||
}
|
||||
|
||||
/* we must update to not introduce duplicated wd's (keys) */
|
||||
node = rbtree_search(&tree, wd);
|
||||
|
||||
if (node == NULL) {
|
||||
dprint("added wd = %i on %s\n", wd, cpath);
|
||||
rbtree_insert(&tree, wd, (void*)cpath);
|
||||
} else {
|
||||
dprint("updated wd = %i from %s to %s\n", wd, (char*)node->data, cpath);
|
||||
free(node->data);
|
||||
node->data = (void*) cpath;
|
||||
}
|
||||
|
||||
return wd;
|
||||
}
|
||||
|
||||
/* REF:1 */
|
||||
static int proc_event(inoev *iev) {
|
||||
|
||||
rbnode *node;
|
||||
char buf[4096];
|
||||
uint8_t type = NOTIFY_UNKNOWN;
|
||||
|
||||
if (iev == NULL)
|
||||
return 0;
|
||||
|
||||
//notify_event_clear(&event);
|
||||
|
||||
#ifdef __DEBUG__
|
||||
fprintf(stderr, "RAW EVENT: %i, %x", iev->wd, iev->mask);
|
||||
if (iev->len)
|
||||
fprintf(stderr, ", %s\n", iev->name);
|
||||
else
|
||||
fprintf(stderr, "\n");
|
||||
#endif
|
||||
|
||||
/* lookup the watch descriptor in rbtree */
|
||||
node = rbtree_search(&tree, iev->wd);
|
||||
|
||||
if (node == NULL) {
|
||||
#ifdef __DEBUG__
|
||||
fprintf(stderr, "-- IGNORING EVENT -- invalid watchdescriptor %i\n", iev->wd);
|
||||
rb_assert(tree.root);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
notify_event_set_dir(&event, (iev->mask & IN_ISDIR) != 0);
|
||||
|
||||
/* set path, this is stored in void* node->data */
|
||||
notify_event_set_path(&event, (char*)node->data);
|
||||
notify_event_set_filename(&event, iev->name);
|
||||
|
||||
iev->mask &= ~IN_ISDIR;
|
||||
|
||||
switch (iev->mask) {
|
||||
|
||||
case IN_CREATE :
|
||||
|
||||
if (event.dir) {
|
||||
dprint("IN_CREATE on directory, adding\n");
|
||||
addwatch(event.path, event.filename);
|
||||
}
|
||||
|
||||
type = NOTIFY_CREATE;
|
||||
break;
|
||||
case IN_MOVED_TO :
|
||||
|
||||
if (event.dir) {
|
||||
dprint("IN_CREATE on directory, adding\n");
|
||||
addwatch(event.path, event.filename);
|
||||
}
|
||||
|
||||
type = NOTIFY_MOVE_TO;
|
||||
break;
|
||||
case IN_MOVED_FROM :
|
||||
/* TODO: clean this */
|
||||
sprintf(buf, "%s%s/", event.path, event.filename);
|
||||
notify_rm_watch_path(buf);
|
||||
case IN_DELETE :
|
||||
type = NOTIFY_DELETE;
|
||||
break;
|
||||
case IN_IGNORED :
|
||||
rmwatch(iev->wd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
notify_event_set_type(&event, type);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int notify_init() {
|
||||
|
||||
if (fd > 0) {
|
||||
fprintf(stderr, "inotify already instantiated.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
fd = inotify_init();
|
||||
|
||||
if (fd < 1) {
|
||||
perror("NOTIFY INIT");
|
||||
return -1;
|
||||
}
|
||||
|
||||
dprint("inotify descriptor = %i\n", fd);
|
||||
|
||||
inobuf = malloc(INOBUFSIZE);
|
||||
|
||||
if (inobuf == NULL) {
|
||||
perror("NOTIFY BUFFER");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void notify_cleanup() {
|
||||
|
||||
/* free rbtree */
|
||||
rbtree_free(&tree, NULL);
|
||||
|
||||
if (inobuf != NULL)
|
||||
free(inobuf);
|
||||
|
||||
fd = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Add inotify watch on path
|
||||
*/
|
||||
int notify_add_watch(const char *path) {
|
||||
|
||||
return addwatch(path, NULL);
|
||||
}
|
||||
|
||||
void notify_rm_watch(unsigned int wd) {
|
||||
inotify_rm_watch(fd, wd);
|
||||
}
|
||||
|
||||
void notify_rm_watch_path(const char *path) {
|
||||
|
||||
rbnode *node = rbtree_cmp_search(&tree, (void*)path, strlen(path));
|
||||
|
||||
if (node == NULL) {
|
||||
dprint("remove watch by path: can't find %s\n", path);
|
||||
return;
|
||||
}
|
||||
|
||||
dprint("remove watch by path: %i %s\n", node->key, (char*) node->data);
|
||||
notify_rm_watch(node->key);
|
||||
}
|
||||
|
||||
/*
|
||||
* Get next inotify event
|
||||
*/
|
||||
notify_event* notify_read() {
|
||||
|
||||
int rc;
|
||||
inoev *rev = NULL;
|
||||
static int offset;
|
||||
|
||||
while(! proc_event(rev)) {
|
||||
|
||||
if (fd_bytes < 1) {
|
||||
|
||||
offset = 0;
|
||||
|
||||
fd_bytes = read(fd, inobuf, INOBUFSIZE);
|
||||
|
||||
if (fd_bytes < 1) {
|
||||
|
||||
if (fd_bytes == 0) {
|
||||
fprintf(stderr, "NOTIFY READ: EOF\n");
|
||||
}
|
||||
/* EINTR = call interrupted, silent ignore */
|
||||
else if (errno != EINTR) {
|
||||
perror("NOTIFY READ");
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
rev = (inoev *) &inobuf[offset];
|
||||
offset += sizeof(inoev) + rev->len;
|
||||
|
||||
if (offset >= fd_bytes)
|
||||
fd_bytes = 0;
|
||||
}
|
||||
|
||||
return &event;
|
||||
}
|
||||
|
||||
static void print_node(rbnode *node) {
|
||||
printf("%i: %s\n", node->key, (char*) node->data);
|
||||
}
|
||||
|
||||
void notify_stat() {
|
||||
|
||||
#ifdef __DEBUG__
|
||||
rbtree_walk(&tree, print_node);
|
||||
#endif
|
||||
}
|
||||
|
||||
int notify_is_ready() {
|
||||
|
||||
unsigned int bytes;
|
||||
|
||||
if (fd_bytes > 1)
|
||||
return 1;
|
||||
|
||||
if (ioctl(fd, FIONREAD, &bytes) == -1)
|
||||
return 0;
|
||||
|
||||
dprint("%i bytes ready!\n", bytes);
|
||||
|
||||
return bytes > 512;
|
||||
}
|
||||
|
||||
|
||||
206
src/fs/tree.c
Normal file
206
src/fs/tree.c
Normal file
|
|
@ -0,0 +1,206 @@
|
|||
|
||||
/*
|
||||
* -- tree.c
|
||||
*
|
||||
* Copyright (C) 2010 Archived
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <malloc.h>
|
||||
#include <errno.h>
|
||||
#include <dirent.h>
|
||||
|
||||
#include "../common/mem.h"
|
||||
#include "../common/path.h"
|
||||
#include "tree.h"
|
||||
|
||||
#if 0
|
||||
#define pdebug(...) fprintf(stderr, __VA_ARGS__)
|
||||
#else
|
||||
#define pdebug(...) 0
|
||||
#endif
|
||||
|
||||
#define isrefdir(x) (!strcmp(x->d_name, ".") || !strcmp(x->d_name, ".."))
|
||||
|
||||
/* REF:1 */
|
||||
static int have_perm(const char *base, const char *name) {
|
||||
|
||||
char buf[1024];
|
||||
|
||||
sprintf(buf, "%s%s", base, name);
|
||||
|
||||
return access(buf, R_OK | X_OK) == 0;
|
||||
}
|
||||
|
||||
struct tree* tree_new(const char *path) {
|
||||
|
||||
int i;
|
||||
struct tree *t = malloc(sizeof(struct tree));
|
||||
|
||||
if (t == NULL)
|
||||
return NULL;
|
||||
|
||||
t->path = fmt_path(path, NULL, 0);
|
||||
|
||||
if (t->path == NULL) {
|
||||
free(t);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
t->depth = 0;
|
||||
|
||||
t->dirs[0] = opendir(t->path);
|
||||
|
||||
if (!t->dirs[0]) {
|
||||
free(t->path);
|
||||
free(t);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
t->ent.name = NULL;
|
||||
t->ent.base = NULL;
|
||||
t->ent.dir = 0;
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
void tree_del(struct tree *tree) {
|
||||
|
||||
int i;
|
||||
|
||||
for(i=0; i < tree->depth; i++)
|
||||
closedir(tree->dirs[i]);
|
||||
|
||||
if (tree->path != NULL)
|
||||
free(tree->path);
|
||||
|
||||
free(tree);
|
||||
}
|
||||
|
||||
/* REF:1 */
|
||||
int tree_mvup(struct tree *tree) {
|
||||
|
||||
char *slash;
|
||||
|
||||
if (closedir(tree->dirs[tree->depth]) == -1) {
|
||||
perror("TREE");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tree->depth == 0)
|
||||
return 0;
|
||||
#if __DEBUG__
|
||||
printf("tree_next_ent: tree depth is: %i \n", tree->depth);
|
||||
#endif
|
||||
tree->depth--;
|
||||
|
||||
slash = tree->path + strlen(tree->path)-1;
|
||||
if (*slash == '/')
|
||||
*slash = 0;
|
||||
|
||||
slash = strrchr(tree->path, '/');
|
||||
*(slash+1) = 0;
|
||||
|
||||
tree->path = realloc(tree->path, sizeof(char) * (strlen(tree->path)+1));
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* REF:1 */
|
||||
int tree_mvdown(struct tree *tree, const char *dir) {
|
||||
|
||||
char buf[PATH_MAX];
|
||||
DIR *ds;
|
||||
|
||||
if (tree->depth >= MAX_DEPTH-1)
|
||||
return 0;
|
||||
|
||||
sprintf(buf, "%s%s/\0", tree->path, dir);
|
||||
|
||||
if ((ds = opendir(buf)) == NULL) {
|
||||
if (errno != EACCES)
|
||||
perror("DIROPEN");
|
||||
return 0;
|
||||
}
|
||||
|
||||
tree->path = realloc(tree->path, sizeof(char) * (strlen(buf)+1));
|
||||
memcpy(tree->path, buf, strlen(buf)+1);
|
||||
|
||||
tree->dirs[++tree->depth] = ds;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
struct entry* tree_cpy_ent(struct entry *ent) {
|
||||
|
||||
struct entry *ptr = malloc(sizeof(struct entry));
|
||||
|
||||
if (ptr == NULL)
|
||||
return NULL;
|
||||
|
||||
memcpy(ptr, ent, sizeof(struct entry));
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
/*
|
||||
* NOTE: only read from the struct returned and DO NOT free it.
|
||||
* use tree_cpy_ent() if you need to store it.
|
||||
*/
|
||||
struct entry* tree_next_ent(struct tree *tree) {
|
||||
|
||||
struct dirent *ent = NULL;
|
||||
|
||||
if (tree == NULL)
|
||||
return NULL;
|
||||
|
||||
if (tree->ent.dir)
|
||||
tree_mvdown(tree, tree->ent.name);
|
||||
|
||||
for(;;) {
|
||||
|
||||
ent = readdir(tree->dirs[tree->depth]);
|
||||
|
||||
if (ent == NULL) {
|
||||
|
||||
if (tree_mvup(tree))
|
||||
continue;
|
||||
|
||||
tree_del(tree);
|
||||
return NULL;
|
||||
} else if (isrefdir(ent)) {
|
||||
continue;
|
||||
|
||||
} else if (ent->d_type == 4) {
|
||||
|
||||
if (have_perm(tree->path, ent->d_name))
|
||||
break;
|
||||
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
tree->ent.dir = ent->d_type == 4;
|
||||
tree->ent.base = tree->path;
|
||||
tree->ent.name = &ent->d_name[0];
|
||||
|
||||
|
||||
return &tree->ent;
|
||||
}
|
||||
34
src/fs/tree.h
Normal file
34
src/fs/tree.h
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
|
||||
#ifndef _FS_TREE_H
|
||||
|
||||
#define _FS_TREE_H
|
||||
|
||||
#define MAX_DEPTH 0x20
|
||||
|
||||
#include <dirent.h>
|
||||
|
||||
struct tree {
|
||||
char *path;
|
||||
DIR *dirs[MAX_DEPTH];
|
||||
unsigned char depth;
|
||||
/* REF:2 */
|
||||
struct entry {
|
||||
char *name;
|
||||
char *base;
|
||||
unsigned char dir;
|
||||
} ent;
|
||||
};
|
||||
|
||||
struct tree* tree_new(const char *path);
|
||||
|
||||
void tree_del(struct tree *tree);
|
||||
|
||||
int tree_mvup(struct tree *tree);
|
||||
|
||||
int tree_mvdown(struct tree *tree, const char *dir);
|
||||
|
||||
struct entry* tree_cpy_ent(struct entry *ent);
|
||||
|
||||
struct entry* tree_next_ent(struct tree *tree);
|
||||
|
||||
#endif /* _FS_TREE_H */
|
||||
Reference in a new issue