vrfileman

annotate 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
rev   line source
nuclear@1 1 #include <stdio.h>
nuclear@1 2 #include <string.h>
nuclear@1 3 #include <ctype.h>
nuclear@1 4 #include <errno.h>
nuclear@1 5 #include <alloca.h>
nuclear@1 6 #include <unistd.h>
nuclear@1 7 #include <dirent.h>
nuclear@1 8 #include "fs.h"
nuclear@1 9
nuclear@1 10 static char *clean_path(char *path);
nuclear@1 11 static char *filename(char *path);
nuclear@1 12
nuclear@1 13 FSNode::FSNode()
nuclear@1 14 {
nuclear@1 15 init();
nuclear@1 16 }
nuclear@1 17
nuclear@1 18 FSNode::~FSNode()
nuclear@1 19 {
nuclear@1 20 destroy();
nuclear@1 21 }
nuclear@1 22
nuclear@1 23 void FSNode::init()
nuclear@1 24 {
nuclear@1 25 path = name = 0;
nuclear@1 26 parent = 0;
nuclear@1 27 expanded = false;
nuclear@1 28 uid = gid = mode = 0;
nuclear@1 29 }
nuclear@1 30
nuclear@1 31 void FSNode::destroy()
nuclear@1 32 {
nuclear@1 33 delete [] path;
nuclear@1 34 children.clear();
nuclear@1 35 init();
nuclear@1 36 }
nuclear@1 37
nuclear@1 38 void FSNode::destroy_tree()
nuclear@1 39 {
nuclear@1 40 for(size_t i=0; i<children.size(); i++) {
nuclear@1 41 children[i]->destroy_tree();
nuclear@1 42 delete children[i];
nuclear@1 43 }
nuclear@1 44 destroy();
nuclear@1 45 }
nuclear@1 46
nuclear@1 47 void FSNode::set_path(const char *path)
nuclear@1 48 {
nuclear@1 49 delete [] this->path;
nuclear@1 50
nuclear@1 51 char *buf = new char[strlen(path) + 1];
nuclear@1 52 strcpy(buf, path);
nuclear@1 53
nuclear@1 54 char *tmp = clean_path(buf);
nuclear@1 55 if(tmp == buf) {
nuclear@1 56 this->path = tmp;
nuclear@1 57 } else {
nuclear@1 58 this->path = new char[strlen(tmp) + 1];
nuclear@1 59 strcpy(this->path, tmp);
nuclear@1 60 delete [] buf;
nuclear@1 61 }
nuclear@1 62
nuclear@1 63 name = filename(this->path);
nuclear@1 64 }
nuclear@1 65
nuclear@1 66 void FSNode::set_name(const char *name)
nuclear@1 67 {
nuclear@1 68 delete [] path;
nuclear@1 69
nuclear@1 70 if(parent) {
nuclear@1 71 char *path = new char[strlen(name) + strlen(parent->path) + 2];
nuclear@1 72 sprintf(path, "%s/%s", parent->path, name);
nuclear@1 73 set_path(path);
nuclear@1 74 } else {
nuclear@1 75 path = this->name = new char[strlen(name) + 1];
nuclear@1 76 strcpy(path, name);
nuclear@1 77 }
nuclear@1 78 }
nuclear@1 79
nuclear@1 80 #if 0
nuclear@1 81 FSDir *create_fsdir(const char *path)
nuclear@1 82 {
nuclear@1 83 char *pathbuf;
nuclear@1 84
nuclear@1 85 FSDir *node = new FSDir;
nuclear@1 86 node->name = std::string(filename(path));
nuclear@1 87
nuclear@1 88 DIR *dir = opendir(path);
nuclear@1 89 if(!dir) {
nuclear@1 90 fprintf(stderr, "failed to open dir: %s: %s\n", path, strerror(errno));
nuclear@1 91 return 0;
nuclear@1 92 }
nuclear@1 93
nuclear@1 94 pathbuf = (char*)alloca(strlen(path) + NAME_MAX + 2);
nuclear@1 95
nuclear@1 96 struct dirent *ent;
nuclear@1 97 while((ent = readdir(dir))) {
nuclear@1 98 sprintf(pathbuf, "%s/%s", path, ent->d_ent);
nuclear@1 99
nuclear@1 100 struct stat st;
nuclear@1 101 if(stat(pathbuf, &st) == -1) {
nuclear@1 102 fprintf(stderr, "failed to stat: %s: %s\n", pathbuf, strerror(errno));
nuclear@1 103 continue;
nuclear@1 104 }
nuclear@1 105
nuclear@1 106 if(S_ISDIR(st.st_type)) {
nuclear@1 107 FSDir *subdir = new FSDir;
nuclear@1 108 subdir->name = std::string(ent->d_ent);
nuclear@1 109 add_subdir(node, subdir);
nuclear@1 110 } else {
nuclear@1 111 FSFile *file = new FSFile;
nuclear@1 112 file->name = std::string(ent->d_ent);
nuclear@1 113 file->parent = node;
nuclear@1 114 }
nuclear@1 115 }
nuclear@1 116 }
nuclear@1 117 #endif
nuclear@1 118
nuclear@1 119 static char *clean_path(char *path)
nuclear@1 120 {
nuclear@1 121 while(*path && isspace(*path)) {
nuclear@1 122 path++;
nuclear@1 123 }
nuclear@1 124
nuclear@1 125 char *end = path + strlen(path) - 1;
nuclear@1 126 while(end >= path && isspace(*end)) {
nuclear@1 127 *end-- = 0;
nuclear@1 128 }
nuclear@1 129
nuclear@1 130 char *ptr = path - 1;
nuclear@1 131 while(*++ptr) {
nuclear@1 132 if(*ptr == '\\') {
nuclear@1 133 *ptr = '/';
nuclear@1 134 }
nuclear@1 135 }
nuclear@1 136 return path;
nuclear@1 137 }
nuclear@1 138
nuclear@1 139 static char *filename(char *path)
nuclear@1 140 {
nuclear@1 141 char *ptr = strrchr(path, '/');
nuclear@1 142 if(ptr) {
nuclear@1 143 return ptr + 1;
nuclear@1 144 }
nuclear@1 145 return path;
nuclear@1 146 }