char* append_path2(std::string newp) { std::string fid = database_getval("name", newp); std::string file_path = database_getval(fid, "file_path"); return strdup(file_path.c_str()); }
void process_file(string server, string fileid) { string file = database_getval(fileid, "name"); string ext = database_getval(fileid, "ext"); file = server + "/" + file; string attrs=database_getval(ext,"attrs"); string token=""; stringstream ss2(attrs.c_str()); while(getline(ss2,token,':')){ if(strcmp(token.c_str(),"null")!=0){ if(token == "name") { continue; } //cout << "========= looking at attr = " << token <<endl; string cmd=database_getval(token+"gen","command"); if(cmd=="null") { //cout << "command is null, skipping" << endl; continue; } string msg2=(cmd+" \""+file+"\"").c_str(); //cout << "========= issuing command = " << msg2 <<endl; FILE* stream=popen(msg2.c_str(),"r"); if(fgets(msg,200,stream)!=0){ //cout << "========= attr value = " << msg <<endl; database_setval(fileid,token,msg); } pclose(stream); } } }
static int xmp_unlink(const char *path) { //log_msg("in xmp_unlink"); //TODO: handle in vold somehow int res; string fileid=database_getval("name",basename(strdup(path))); string fromext=database_getval(fileid,"ext"); string file=append_path2(basename(strdup(path))); string attrs=database_getval(fromext,"attrs"); //cout << fromext << fileid << endl; //cout<<"HERE!"<<endl; database_remove_val(fileid,"attrs","all_"+fromext+"s"); //cout<<"THERE!"<<endl; //database_remove_val("all_"+fromext+"s",strdup(basename(strdup(from))),fileid); //cout<<"WHERE!"<<endl; string token=""; stringstream ss2(attrs.c_str()); while(getline(ss2,token,':')){ if(strcmp(token.c_str(),"null")!=0){ string cmd=database_getval(token+"gen","command"); string msg2=(cmd+" "+file).c_str(); FILE* stream=popen(msg2.c_str(),"r"); if(fgets(msg,200,stream)!=0){ database_remove_val(fileid,token,msg); } pclose(stream); } } path=append_path2(basename(strdup(path))); res = unlink(path); if (res == -1) return -errno; return 0; }
int khan_flush (const char * path, struct fuse_file_info * info ) { //cout << "=============IN KHAN FLUSH!!!!!!!!" << endl << endl; string filename = basename(strdup(path)); string fileid=database_getval("name",filename); string server=database_getval(fileid,"server"); process_file(server, fileid); return 0; }
int khan_open(const char *path, struct fuse_file_info *fi) { int retstat = 0; int fd; path = basename(strdup(path)); //cout << "in khan_open with file " << path << endl << flush; // get file id string fileid = database_getval("name", path); // get server string server = database_getval(fileid, "server"); //cout << fileid << " " << server << endl << flush; if(server == "cloud") { //cout << "looking at cloud" << endl << flush; string long_path = "/tmp/"; long_path += path; //cout << "downloading to "<< long_path << endl << flush; cloud_download(path, long_path); } return 0; }
static int xmp_rename(const char *from, const char *to) { //cout << endl << endl << endl << "Entering Rename Function" << endl; double start_time = 0; struct timeval start_tv; gettimeofday(&start_tv, NULL); start_time = start_tv.tv_sec; start_time += (start_tv.tv_usec/1000000.0); start_times << fixed << start_time << endl << flush; string src = basename(strdup(from)); string dst = basename(strdup(to)); string fileid = database_getval("name", src); //cout << fileid << endl; database_remove_val(fileid,"name",src); //cout << src << endl; database_setval(fileid,"name",dst); //cout << dst << endl; string orig_path = append_path2(src); string orig_loc = database_getval(fileid,"location"); map_path(resolve_selectors(to), fileid); string new_path = append_path2(dst); string new_loc = database_getval(fileid,"location"); if(new_loc!=orig_loc) { if(new_loc=="google_music") { //upload cloud_upload(orig_path); } else if(orig_loc=="google_music") { //download cloud_download(src, new_path); } else { //file system rename rename(orig_path.c_str(), new_path.c_str()); } } double rename_time = 0; struct timeval end_tv; gettimeofday(&end_tv, NULL); rename_time = end_tv.tv_sec - start_tv.tv_sec; rename_time += (end_tv.tv_usec - start_tv.tv_usec) / 1000000.0; rename_times << fixed << rename_time << endl << flush; //cout << "Exiting Rename Function" << endl << endl << endl << endl; return 0; }
void populate_readdir_buffer(void* buf, fuse_fill_dir_t filler, stringstream &path) { string attr, val, file, more; string current_content = "none"; string current_attrs = "none"; void* aint=getline(path, attr, '/'); void* vint=getline(path, val, '/'); void* fint=getline(path, file, '/'); void* mint=getline(path, more, '/'); bool loop = true; while(loop) { loop = false; string content = database_getvals("attrs"); if(aint) { if(content_has(content, attr)) { current_attrs += ":"; current_attrs += attr; content = database_getvals(attr); if(vint) { if(content_has(content, val) || (attr=="tags")) { string dir_content = database_getval(attr, val); if(current_content!="none") { dir_content = intersect(current_content, dir_content); } string attrs_content = database_getvals("attrs"); if(fint) { if(content_has(attrs_content, file)) { //repeat with aint = fint, vint = mint, etc aint = fint; attr = file; vint = mint; val = more; fint = getline(path, file, '/'); mint = getline(path, more, '/'); current_content = dir_content; loop = true; } } else { // /attr/val dir fprintf(stderr, "%s, %s\n\n\n\n\n\n", attrs_content.c_str(), current_attrs.c_str()); attrs_content = subtract(attrs_content, current_attrs); dir_pop_buf(buf, filler, dir_content, true); dir_pop_buf(buf, filler, attrs_content, false); } } } else { // /attr dir dir_pop_buf(buf, filler, content, false); } } } else { dir_pop_buf(buf, filler, content, false); } } }
void dir_pop_buf(void* buf, fuse_fill_dir_t filler, string content, bool convert) { vector<string> contents = split(content, ":"); for(int i=0; i<contents.size(); i++) { if(convert) { string filename = database_getval(contents[i].c_str(), "name"); filler(buf, filename.c_str(), NULL, 0); } else { filler(buf, contents[i].c_str(), NULL, 0); } } }
int khan_create(const char *path, mode_t mode, struct fuse_file_info *fi) { create_calls++; string fileid=database_getval("name",basename(strdup(path))); if(strcmp(fileid.c_str(),"null")==0){ fileid=database_setval("null","name",basename(strdup(path))); database_setval(fileid, "server", servers.at(0)); string ext = strrchr(basename(strdup(path)),'.')+1; database_setval(fileid, "ext", ext); } string server = database_getval(fileid, "server"); process_file(server, fileid); map_path(resolve_selectors(path), fileid); return 0; }
static int xmp_mkdir(const char *path, mode_t mode) { struct timespec mkdir_start, mkdir_stop; string strpath=path; if(strpath.find("localize")!=string::npos) { if(strpath.find("usage")!=string::npos) { usage_localize(); } else { //cout << "LOCALIZING" << endl; //cout << strpath << endl; //check location string filename = "winter.mp3"; string fileid = database_getval("name", filename); string location = get_location(fileid); string server = database_getval(fileid, "server"); //cout << "======== LOCATION: " << location << endl << endl; //if not current if(location.compare(server)!=0) { // move to new location //cout << " MUST MOVE "<<server<<" TO "<<location<<endl; database_setval(fileid,"server",location); string from = server + "/" + filename; string to = location + "/" + filename; string command = "mv " + from + " " + to; FILE* stream=popen(command.c_str(),"r"); pclose(stream); } } //cout << "LOCALIZATION TIME:" << localize_time << endl <<endl; return -1; } if(strpath.find("stats")!=string::npos){ //print stats and reset ofstream stfile; stfile.open(stats_file.c_str(), ofstream::out); stfile << "TOT TIME :" << tot_time << endl; stfile << "Vold Calls :" << vold_calls << endl; stfile << " Avg Time:" << vold_avg_time << endl; stfile << "Readdir Calls:" << readdir_calls << endl; stfile << " Avg Time:" << readdir_avg_time << endl; stfile << "Access Calls :" << access_calls << endl; stfile << " Avg Time:" << access_avg_time << endl; stfile << "Read Calls :" << read_calls << endl; stfile << " Avg Time:" << read_avg_time << endl; stfile << "Getattr Calls:" << getattr_calls << endl; stfile << " Avg Time:" << getattr_avg_time << endl; stfile << "Write Calls :" << write_calls << endl; stfile << " Avg Time:" << write_avg_time << endl; stfile << "Create Calls :" << create_calls << endl; stfile << " Avg Time:" << create_avg_time << endl; stfile << "Rename Calls :" << rename_calls << endl; stfile << " Avg Time:" << rename_avg_time << endl; stfile.close(); //cout << "TOT TIME :" << tot_time << endl; //cout << "Vold Calls :" << vold_calls << endl; //cout << " Avg Time:" << vold_avg_time << endl; //cout << "Readdir Calls:" << readdir_calls << endl; //cout << " Avg Time:" << readdir_avg_time << endl; //cout << "Access Calls :" << access_calls << endl; //cout << " Avg Time:" << access_avg_time << endl; //cout << "Read Calls :" << read_calls << endl; //cout << " Avg Time:" << read_avg_time << endl; //cout << "Getattr Calls:" << getattr_calls << endl; //cout << " Avg Time:" << getattr_avg_time << endl; //cout << "Write Calls :" << write_calls << endl; //cout << " Avg Time:" << write_avg_time << endl; //cout << "Create Calls :" << create_calls << endl; //cout << " Avg Time:" << create_avg_time << endl; //cout << "Rename Calls :" << rename_calls << endl; //cout << " Avg Time:" << rename_avg_time << endl; vold_calls=0; readdir_calls=0; access_calls=0; getattr_calls=0; read_calls=0; write_calls=0; create_calls=0; rename_calls=0; tot_time=0; vold_avg_time=0; readdir_avg_time=0; access_avg_time=0; getattr_avg_time=0; read_avg_time=0; write_avg_time=0; create_avg_time=0; rename_avg_time=0; return -1; } //log_msg("xmp_mkdir"); sprintf(msg,"khan_mkdir for path=%s\n",path); //log_msg(msg); struct stat *st; if(khan_getattr(path, st)<0) { //add path database_setval("alldirs","paths",path); //and break into attr/val pair and add to vold } else { //log_msg("Directory exists\n"); } return 0; }
int xmp_access(const char *path, int mask) { char *path_copy=strdup(path); if(strcmp(path,"/")==0) { //log_msg("at root"); return 0; } // if(strcmp(path,"/")==0) { //log_msg("at root"); return 0; // } string dirs=database_getval("alldirs","paths"); string temptok=""; stringstream dd(dirs); while(getline(dd,temptok,':')){ if(strcmp(temptok.c_str(),path)==0){ return 0; } } int c=0; for(int i=0; path[i]!='\0'; i++){ if(path[i]=='/') c++; } //decompose path stringstream ss0(path+1); string type, attr, val, file, more; void* tint=getline(ss0, type, '/'); void* fint=getline(ss0, file, '/'); void* mint=getline(ss0, more, '/'); int reta=0; //check for filetype if(tint){ string types = database_getval("allfiles","types"); stringstream ss(types.c_str()); string token; while(getline(ss,token,':')){ if(strcmp(type.c_str(),token.c_str())==0){ reta=1; } } int found=0; do{ //get attr and val found=0; void *aint=fint; string attr=file; void *vint=mint; string val=more; fint=getline(ss0, file, '/'); mint=getline(ss0, more, '/'); //check for attr if(reta && aint) { //cout << attr << endl; string attrs= database_getval(type,"attrs"); stringstream ss3(attrs.c_str()); reta=0; while(getline(ss3,token,':')){ if(strcmp(attr.c_str(), token.c_str())==0){ reta=1; } } //check for val if(reta && vint) { //cout << val << endl; if(strcmp(attr.c_str(),("all_"+type+"s").c_str())==0) { return 0; } string vals=database_getvals(attr); stringstream ss4(vals.c_str()); reta=0; while(getline(ss4,token,':')){ //cout << val << token << endl; if(strcmp(val.c_str(), token.c_str())==0){ reta=1; } } //check for file if(reta && fint) { //cout << file << endl; string files=database_getval(attr, val); stringstream ss4(files.c_str()); if(!mint) { reta=0; while(getline(ss4,token,':')){ token=database_getval(token,"name"); if(strcmp(file.c_str(), token.c_str())==0){ reta=1; } } stringstream ss5(attrs.c_str()); while(getline(ss5,token,':')){ if(strcmp(file.c_str(),token.c_str())==0){ reta=1; } } } else { found=1; } } } } }while(found); } if(reta && !getline(ss0, val, '/')) { return 0; } path=append_path(path); int ret = access(path, mask); return ret; }
int populate_getattr_buffer(struct stat* stbuf, stringstream &path) { string attr, val, file, more; string current = "none"; void* aint=getline(path, attr, '/'); void* vint=getline(path, val, '/'); void* fint=getline(path, file, '/'); void* mint=getline(path, more, '/'); bool loop = true; while(loop) { //cout << "top of loop" << endl << flush; loop = false; if(aint) { string query = database_getval("attrs", attr); //cout << content << " " << attr << " " << query << endl; if(query!="null") { string content = database_getvals(attr); if(vint) { if(content_has(content, val) || (attr=="tags")) { string dir_content = database_getval(attr, val); if(current!="none") { dir_content = str_intersect(current, dir_content); } string attrs_content = database_getvals("attrs"); if(fint) { string fileid = database_getval("name",file); if(content_has(dir_content, fileid)) { if(!mint) { // /attr/val/file path file_pop_stbuf(stbuf, file); return 0; } } else if(content_has(attrs_content, file)) { //repeat with aint = fint, vint = mint, etc aint = fint; attr = file; vint = mint; val = more; fint=getline(path, file, '/'); mint=getline(path, more, '/'); current = dir_content; loop = true; } } else { // /attr/val dir dir_pop_stbuf(stbuf, dir_content+attrs_content); return 0; } } } else { // /attr dir dir_pop_stbuf(stbuf, content); return 0; } } } else { string types=database_getvals("attrs"); dir_pop_stbuf(stbuf, types); return 0; } } return -2; }
int initializing_khan(char * mnt_dir) { //log_msg("In initialize\n"); unmounting(mnt_dir); //Opening root directory and creating if not present //cout<<"khan_root[0] is "<<servers.at(0)<<endl; if(NULL == opendir(servers.at(0).c_str())) { sprintf(msg,"Error msg on opening directory : %s\n",strerror(errno)); //log_msg(msg); //log_msg("Root directory might not exist..Creating\n"); string command = "mkdir " + servers.at(0); if (system(command.c_str()) < 0) { log_msg("Unable to create storage directory...Aborting\n"); exit(1); } } else { fprintf(stderr, "directory opened successfully\n"); } init_database(); //check if we've loaded metadata before string output=database_getval("setup","value"); if(output.compare("true")==0){ log_msg("Database was previously initialized."); tot_time+=(stop.tv_sec-start.tv_sec)+(stop.tv_nsec-start.tv_nsec)/BILLION; return 0; //setup has happened before } //if we have not setup, do so now //log_msg("it hasnt happened, setvalue then setup"); database_setval("setup","value","true"); //load metadata associatons for(int i=0; i<servers.size(); i++){ process_transducers(servers.at(i)); } //load metadata for each file on each server string types=database_getval("allfiles","types"); //cout << "================= types to look for ="<<types<<endl; for(int i=0; i<servers.size(); i++) { if(servers.at(i) == "cloud") { PyObject* myFunction = PyObject_GetAttrString(cloud_interface,(char*)"get_all_titles"); PyObject* myResult = PyObject_CallObject(myFunction, NULL); if(myResult==NULL) { PyErr_PrintEx(0); continue; } int n = PyList_Size(myResult); //cout << "SIZE = " << n << endl << flush; for(int j = 0; j<n; j++) { PyObject* title = PyList_GetItem(myResult, j); char* temp = PyString_AsString(title); if(temp==NULL) { PyErr_PrintEx(0); continue; } string filename = temp; //cout << "Checking " << filename << " ... " << endl << flush; if(database_getval("name",filename)=="null") { string fileid = database_setval("null","name",filename); string ext = strrchr(filename.c_str(),'.')+1; database_setval(fileid,"ext",ext); database_setval(fileid,"server",servers.at(i)); database_setval(fileid,"location",server_ids.at(i)); string attrs=database_getval(ext,"attrs"); string token=""; stringstream ss2(attrs.c_str()); PyObject* myFunction = PyObject_GetAttrString(cloud_interface,(char*)"get_metadata"); while(getline(ss2,token,':')){ if(strcmp(token.c_str(),"null")!=0){ //cout << "========= looking at attr = " << token << endl << flush; PyObject* arglist = PyTuple_New(2); PyTuple_SetItem(arglist, 0, PyString_FromString(filename.c_str())); PyTuple_SetItem(arglist, 1, PyString_FromString(token.c_str())); PyObject* myResult = PyObject_CallObject(myFunction, arglist); //cout << myResult << endl << flush; if(myResult==NULL) { PyErr_PrintEx(0); continue; } char* msg = PyString_AsString(myResult); if(!msg) { PyErr_PrintEx(0); continue; } string val = msg; Py_DECREF(arglist); Py_DECREF(myResult); //cout << "========= got val = " << val << endl << flush; if(val!="na") { database_setval(fileid,token,val); } } } } else { string fileid = database_getval("name", filename); database_setval(fileid,"server",servers.at(i)); database_setval(fileid,"location",server_ids.at(i)); } } } else { glob_t files; glob((servers.at(i)+"/*.*").c_str(),0,NULL,&files); for(int j=0; j<files.gl_pathc; j++) {//for each file string file = files.gl_pathv[j]; string ext = strrchr(file.c_str(),'.')+1; string filename=strrchr(file.c_str(),'/')+1; if(database_getval("name", filename) == "null") { string fileid = database_setval("null","name",filename); database_setval(fileid,"ext",ext); database_setval(fileid,"server",servers.at(i)); database_setval(fileid,"location",server_ids.at(i)); for(int k=0; k<server_ids.size(); k++) { database_setval(fileid, server_ids.at(k), "0"); } if(j%10==0) { cout << "processed file " << j << "\n"; } process_file(servers.at(i), fileid); } else { string fileid = database_getval("name",filename); database_setval(fileid,"server",servers.at(i)); database_setval(fileid,"location",server_ids.at(i)); } } } } //log_msg("At the end of initialize\n"); return 0; }