#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using namespace net::lliurex; #define BUF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1)) bool filesystem::Exists(string path) { return (access( path.c_str(), F_OK )!=-1); } bool filesystem::IsDir(string path) { struct stat fflags; stat(path.c_str(),&fflags); return S_ISDIR(fflags.st_mode); } std::vector filesystem::ListDir(string path) { struct stat fflags; std::vector files; stat(path.c_str(),&fflags); if(S_ISDIR(fflags.st_mode)) { DIR *dp; struct dirent *dirp; if((dp = opendir(path.c_str())) == NULL) { cout << "Error opening " << path << endl; } while ((dirp = readdir(dp)) != NULL) { files.push_back(string(dirp->d_name)); } closedir(dp); } return files; } std::vector filesystem::List(string expression) { glob_t glob_result; glob(expression.c_str(),GLOB_TILDE,NULL,&glob_result); std::vector files; for(int n=0;npw_dir); } void CreateDirAux(string path) { if(!filesystem::Exists(path)) { int pos = path.rfind("/"); if(pos!=string::npos) { CreateDirAux(path.substr(0,pos)); mkdir(path.c_str(),S_IRWXU); } } } void filesystem::CreateDir(string path,bool create_path) { if(create_path) { if(path[path.length()-1]=='/') CreateDirAux(path.substr(0,path.length()-1)); else CreateDirAux(path); } else { mkdir(path.c_str(),S_IRWXU); } } string filesystem::GetWorkingDir() { int len=128; char * buff; repeat: buff = new char[len]; if(getcwd(buff,len)==NULL) { delete buff; len*=2; goto repeat; } string ret(buff); delete buff; return ret; } string filesystem::BaseName(string path) { char * dup = new char[path.length()+1]; strcpy(dup,path.c_str()); string ret(basename(dup)); delete dup; return ret; } string filesystem::DirName(string path) { char * dup = new char[path.length()+1]; strcpy(dup,path.c_str()); string ret(dirname(dup)); delete dup; return ret; } filesystem::NotifyHandler::NotifyHandler(string path) { this->path=path; inotify_fd = inotify_init(); wd = inotify_add_watch(inotify_fd, path.c_str(), IN_ALL_EVENTS); if(wd==-1) { cerr<<"Error adding inotify watch"<0) { char * p = buf; while(p < buf + num_read) { event = (struct inotify_event *) p; if(event->mask & IN_ACCESS)OnAccess(string(event->name)); if(event->mask & IN_MODIFY)OnModify(string(event->name)); if(event->mask & IN_ATTRIB)OnAttrib(string(event->name)); if(event->mask & IN_CLOSE_WRITE)OnCloseWrite(string(event->name)); if(event->mask & IN_CLOSE_NOWRITE)OnCloseNoWrite(string(event->name)); if(event->mask & IN_OPEN)OnOpen(string(event->name)); if(event->mask & IN_MOVED_FROM)OnMovedFrom(string(event->name)); if(event->mask & IN_MOVED_TO)OnMovedTo(string(event->name)); if(event->mask & IN_CREATE)OnCreate(string(event->name)); if(event->mask & IN_DELETE)OnDelete(string(event->name)); if(event->mask & IN_DELETE_SELF)OnDeleteSelf(string(event->name)); p=p+sizeof(struct inotify_event) + event->len; } } else { OnError(); } } void filesystem::NotifyHandler::OnError() { cerr<<"NotifyHandler: Error reading from inotify"<