nuclear@1: #include nuclear@1: #include nuclear@1: #include nuclear@1: #include nuclear@1: #include nuclear@1: #include nuclear@1: #include nuclear@1: #include "fs.h" nuclear@1: nuclear@1: static char *clean_path(char *path); nuclear@1: static char *filename(char *path); nuclear@1: nuclear@1: FSNode::FSNode() nuclear@1: { nuclear@1: init(); nuclear@1: } nuclear@1: nuclear@1: FSNode::~FSNode() nuclear@1: { nuclear@1: destroy(); nuclear@1: } nuclear@1: nuclear@1: void FSNode::init() nuclear@1: { nuclear@1: path = name = 0; nuclear@1: parent = 0; nuclear@1: expanded = false; nuclear@1: uid = gid = mode = 0; nuclear@1: } nuclear@1: nuclear@1: void FSNode::destroy() nuclear@1: { nuclear@1: delete [] path; nuclear@1: children.clear(); nuclear@1: init(); nuclear@1: } nuclear@1: nuclear@1: void FSNode::destroy_tree() nuclear@1: { nuclear@1: for(size_t i=0; idestroy_tree(); nuclear@1: delete children[i]; nuclear@1: } nuclear@1: destroy(); nuclear@1: } nuclear@1: nuclear@1: void FSNode::set_path(const char *path) nuclear@1: { nuclear@1: delete [] this->path; nuclear@1: nuclear@1: char *buf = new char[strlen(path) + 1]; nuclear@1: strcpy(buf, path); nuclear@1: nuclear@1: char *tmp = clean_path(buf); nuclear@1: if(tmp == buf) { nuclear@1: this->path = tmp; nuclear@1: } else { nuclear@1: this->path = new char[strlen(tmp) + 1]; nuclear@1: strcpy(this->path, tmp); nuclear@1: delete [] buf; nuclear@1: } nuclear@1: nuclear@1: name = filename(this->path); nuclear@1: } nuclear@1: nuclear@1: void FSNode::set_name(const char *name) nuclear@1: { nuclear@1: delete [] path; nuclear@1: nuclear@1: if(parent) { nuclear@1: char *path = new char[strlen(name) + strlen(parent->path) + 2]; nuclear@1: sprintf(path, "%s/%s", parent->path, name); nuclear@1: set_path(path); nuclear@1: } else { nuclear@1: path = this->name = new char[strlen(name) + 1]; nuclear@1: strcpy(path, name); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: #if 0 nuclear@1: FSDir *create_fsdir(const char *path) nuclear@1: { nuclear@1: char *pathbuf; nuclear@1: nuclear@1: FSDir *node = new FSDir; nuclear@1: node->name = std::string(filename(path)); nuclear@1: nuclear@1: DIR *dir = opendir(path); nuclear@1: if(!dir) { nuclear@1: fprintf(stderr, "failed to open dir: %s: %s\n", path, strerror(errno)); nuclear@1: return 0; nuclear@1: } nuclear@1: nuclear@1: pathbuf = (char*)alloca(strlen(path) + NAME_MAX + 2); nuclear@1: nuclear@1: struct dirent *ent; nuclear@1: while((ent = readdir(dir))) { nuclear@1: sprintf(pathbuf, "%s/%s", path, ent->d_ent); nuclear@1: nuclear@1: struct stat st; nuclear@1: if(stat(pathbuf, &st) == -1) { nuclear@1: fprintf(stderr, "failed to stat: %s: %s\n", pathbuf, strerror(errno)); nuclear@1: continue; nuclear@1: } nuclear@1: nuclear@1: if(S_ISDIR(st.st_type)) { nuclear@1: FSDir *subdir = new FSDir; nuclear@1: subdir->name = std::string(ent->d_ent); nuclear@1: add_subdir(node, subdir); nuclear@1: } else { nuclear@1: FSFile *file = new FSFile; nuclear@1: file->name = std::string(ent->d_ent); nuclear@1: file->parent = node; nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: #endif nuclear@1: nuclear@1: static char *clean_path(char *path) nuclear@1: { nuclear@1: while(*path && isspace(*path)) { nuclear@1: path++; nuclear@1: } nuclear@1: nuclear@1: char *end = path + strlen(path) - 1; nuclear@1: while(end >= path && isspace(*end)) { nuclear@1: *end-- = 0; nuclear@1: } nuclear@1: nuclear@1: char *ptr = path - 1; nuclear@1: while(*++ptr) { nuclear@1: if(*ptr == '\\') { nuclear@1: *ptr = '/'; nuclear@1: } nuclear@1: } nuclear@1: return path; nuclear@1: } nuclear@1: nuclear@1: static char *filename(char *path) nuclear@1: { nuclear@1: char *ptr = strrchr(path, '/'); nuclear@1: if(ptr) { nuclear@1: return ptr + 1; nuclear@1: } nuclear@1: return path; nuclear@1: }