vrfileman

diff src/fs.cc @ 1:9e3d77dad51b

moving on...
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 31 Jan 2015 20:01:35 +0200
parents
children 282da6123fd4
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/fs.cc	Sat Jan 31 20:01:35 2015 +0200
     1.3 @@ -0,0 +1,146 @@
     1.4 +#include <stdio.h>
     1.5 +#include <string.h>
     1.6 +#include <ctype.h>
     1.7 +#include <errno.h>
     1.8 +#include <alloca.h>
     1.9 +#include <unistd.h>
    1.10 +#include <dirent.h>
    1.11 +#include "fs.h"
    1.12 +
    1.13 +static char *clean_path(char *path);
    1.14 +static char *filename(char *path);
    1.15 +
    1.16 +FSNode::FSNode()
    1.17 +{
    1.18 +	init();
    1.19 +}
    1.20 +
    1.21 +FSNode::~FSNode()
    1.22 +{
    1.23 +	destroy();
    1.24 +}
    1.25 +
    1.26 +void FSNode::init()
    1.27 +{
    1.28 +	path = name = 0;
    1.29 +	parent = 0;
    1.30 +	expanded = false;
    1.31 +	uid = gid = mode = 0;
    1.32 +}
    1.33 +
    1.34 +void FSNode::destroy()
    1.35 +{
    1.36 +	delete [] path;
    1.37 +	children.clear();
    1.38 +	init();
    1.39 +}
    1.40 +
    1.41 +void FSNode::destroy_tree()
    1.42 +{
    1.43 +	for(size_t i=0; i<children.size(); i++) {
    1.44 +		children[i]->destroy_tree();
    1.45 +		delete children[i];
    1.46 +	}
    1.47 +	destroy();
    1.48 +}
    1.49 +
    1.50 +void FSNode::set_path(const char *path)
    1.51 +{
    1.52 +	delete [] this->path;
    1.53 +
    1.54 +	char *buf = new char[strlen(path) + 1];
    1.55 +	strcpy(buf, path);
    1.56 +
    1.57 +	char *tmp = clean_path(buf);
    1.58 +	if(tmp == buf) {
    1.59 +		this->path = tmp;
    1.60 +	} else {
    1.61 +		this->path = new char[strlen(tmp) + 1];
    1.62 +		strcpy(this->path, tmp);
    1.63 +		delete [] buf;
    1.64 +	}
    1.65 +
    1.66 +	name = filename(this->path);
    1.67 +}
    1.68 +
    1.69 +void FSNode::set_name(const char *name)
    1.70 +{
    1.71 +	delete [] path;
    1.72 +
    1.73 +	if(parent) {
    1.74 +		char *path = new char[strlen(name) + strlen(parent->path) + 2];
    1.75 +		sprintf(path, "%s/%s", parent->path, name);
    1.76 +		set_path(path);
    1.77 +	} else {
    1.78 +		path = this->name = new char[strlen(name) + 1];
    1.79 +		strcpy(path, name);
    1.80 +	}
    1.81 +}
    1.82 +
    1.83 +#if 0
    1.84 +FSDir *create_fsdir(const char *path)
    1.85 +{
    1.86 +	char *pathbuf;
    1.87 +
    1.88 +	FSDir *node = new FSDir;
    1.89 +	node->name = std::string(filename(path));
    1.90 +
    1.91 +	DIR *dir = opendir(path);
    1.92 +	if(!dir) {
    1.93 +		fprintf(stderr, "failed to open dir: %s: %s\n", path, strerror(errno));
    1.94 +		return 0;
    1.95 +	}
    1.96 +
    1.97 +	pathbuf = (char*)alloca(strlen(path) + NAME_MAX + 2);
    1.98 +
    1.99 +	struct dirent *ent;
   1.100 +	while((ent = readdir(dir))) {
   1.101 +		sprintf(pathbuf, "%s/%s", path, ent->d_ent);
   1.102 +
   1.103 +		struct stat st;
   1.104 +		if(stat(pathbuf, &st) == -1) {
   1.105 +			fprintf(stderr, "failed to stat: %s: %s\n", pathbuf, strerror(errno));
   1.106 +			continue;
   1.107 +		}
   1.108 +
   1.109 +		if(S_ISDIR(st.st_type)) {
   1.110 +			FSDir *subdir = new FSDir;
   1.111 +			subdir->name = std::string(ent->d_ent);
   1.112 +			add_subdir(node, subdir);
   1.113 +		} else {
   1.114 +			FSFile *file =  new FSFile;
   1.115 +			file->name = std::string(ent->d_ent);
   1.116 +			file->parent = node;
   1.117 +		}
   1.118 +	}
   1.119 +}
   1.120 +#endif
   1.121 +
   1.122 +static char *clean_path(char *path)
   1.123 +{
   1.124 +	while(*path && isspace(*path)) {
   1.125 +		path++;
   1.126 +	}
   1.127 +
   1.128 +	char *end = path + strlen(path) - 1;
   1.129 +	while(end >= path && isspace(*end)) {
   1.130 +		*end-- = 0;
   1.131 +	}
   1.132 +
   1.133 +	char *ptr = path - 1;
   1.134 +	while(*++ptr) {
   1.135 +		if(*ptr == '\\') {
   1.136 +			*ptr = '/';
   1.137 +		}
   1.138 +	}
   1.139 +	return path;
   1.140 +}
   1.141 +
   1.142 +static char *filename(char *path)
   1.143 +{
   1.144 +	char *ptr = strrchr(path, '/');
   1.145 +	if(ptr) {
   1.146 +		return ptr + 1;
   1.147 +	}
   1.148 +	return path;
   1.149 +}