forked from dbratcher/MediaKhan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkhan.cpp
executable file
·1799 lines (1607 loc) · 65.6 KB
/
khan.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
** File: khan.cpp
** Description: Main functionality for the khan filesystem.
**/
#include "khan.h"
#define SELECTOR_C '@'
#define SELECTOR_S "@"
#define log stderr
//mkdir stats prints stats to stats file and console:
string stats_file="./stats.txt";
string rename_times_file_name="./rename_times.txt";
string start_times_file_name = "./start_times.txt";
vector<string> servers;
vector<string> server_ids;
string this_server;
string this_server_id;
static string primary_attribute = "";
//PyObject* cloud_interface;
ofstream rename_times;
ofstream start_times;
string mountpoint;
void analytics(void);
string call_pyfunc(string script_name, string func_name, string file_path);
/*
void cloud_upload(string path) {
FILE* stream=popen(("id3convert -1 -2 '"+path+"'").c_str(),"r");
pclose(stream);
//cout << " Uploading song " << path << endl;
PyObject* arglist = PyTuple_New(1);
PyTuple_SetItem(arglist, 0, PyString_FromString(path.c_str()));
PyObject* myFunction =PyObject_GetAttrString(cloud_interface,(char*)"upload_song");
PyObject* myResult = PyObject_CallObject(myFunction, arglist);
if(myResult==NULL) {
PyErr_PrintEx(0);
}
}
void cloud_download(string song, string path) {
//cout << " Downloading song " << song << " to " << path << endl;
PyObject* arglist = PyTuple_New(2);
PyTuple_SetItem(arglist, 0, PyString_FromString(song.c_str()));
PyTuple_SetItem(arglist, 1, PyString_FromString(path.c_str()));
PyObject* myFunction = PyObject_GetAttrString(cloud_interface,(char*)"get_song");
PyObject* myResult = PyObject_CallObject(myFunction, arglist);
if(myResult==NULL) {
PyErr_PrintEx(0);
}
FILE* stream=popen(("id3convert -1 '"+path+"'").c_str(),"r");
pclose(stream);
}
*/
string call_pyfunc(string script_name, string func_name, string file_path){
string result;
PyObject *pName, *pModule, *pDict, *pValue, *pArgs, *pClass, *pInstance;
PyObject *pFile;
pName = PyString_FromString(script_name.c_str());
pModule = PyImport_Import(pName);
pDict = PyModule_GetDict(pModule);
pClass = PyDict_GetItemString(pDict, script_name.c_str());
if (PyCallable_Check(pClass))
{
pFile = PyString_FromString(file_path.c_str());
pArgs = PyTuple_New(1);
PyTuple_SetItem(pArgs, 0, pFile);
pInstance = PyObject_CallObject(pClass, pArgs);
}
pValue = PyObject_CallMethod(pInstance, strdup(func_name.c_str()), NULL);
if(pValue != NULL)
{
result = PyString_AsString(pValue);
Py_DECREF(pValue);
}
else {
PyErr_Print();
}
// Clean up
/*Py_DECREF(pModule);
Py_DECREF(pValue);
Py_DECREF(pFile);
Py_DECREF(pArgs);
Py_DECREF(pClass);
Py_DECREF(pInstance);
*/
return result;
}
void process_transducers(string server) {
log_msg("Process Transducers\n");
if(server == "cloud") {
return;
}
string line;
ifstream transducers_file(("/net/hu21/agangil3/KhanScripts/transducers.txt"));
getline(transducers_file, line);
while(transducers_file.good()){
log_msg("=============== got type = \n");
//add line to vold as file type
database_setval("allfiles","types",line);
database_setval(line,"attrs","name");
database_setval(line,"attrs","tags");
database_setval(line,"attrs","location");
database_setval("namegen","command","basename");
database_setval(line,"attrs","ext");
database_setval(line, "attrs", "experiment_id");
// database_setval(line, "attrs", "file_path");
string ext=line;
log_msg("===Unique Attribute!=== ");
getline(transducers_file, line);
stringstream s_uniq(line.c_str());
cout << "Line " << line << endl;
string uniq_attr = "";
getline(s_uniq, uniq_attr, '*');
if(uniq_attr != ""){
primary_attribute = uniq_attr;
}
cout << primary_attribute << endl;
getline(transducers_file,line);
const char *firstchar=line.c_str();
while(firstchar[0]=='-') {
//add line to vold under filetype as vold
stringstream ss(line.c_str());
string attr;
getline(ss,attr,'-');
getline(ss,attr,':');
string command;
getline(ss,command,':');
log_msg("============ checking attr = \n");
log_msg("============ checking command = \n");
attr=trim(attr);
database_setval(ext,"attrs",attr);
database_setval(attr+"gen","command",command);
getline(transducers_file,line);
firstchar=line.c_str();
}
}
}
/*processes files: issues the mp3info command for the file
and fills in the values of the attributes*/
void process_file(string server, string fileid, string file_path) {
log_msg("Inside the Process File \n");
string file = database_getval(fileid, "name");
string ext = database_getval(fileid, "ext");
file = server + "/" + file;
string attrs=database_getval(ext,"attrs");
cout << "Attrs :" << attrs << endl;
char msg4[100];
if(attrs != "null"){
string token="";
stringstream ss2(attrs.c_str());
// FILE* stream;
while(getline(ss2,token,':')){
if(strcmp(token.c_str(),"null")!=0){
sprintf(msg, "=== looking at attr === : %s\n", token.c_str());
log_msg(msg);
if(token == "name" || token == "ext" || token == "location" ||
token == "experiment_id" || token == "file_path" || token == "tags") {
continue;
}
string res = call_pyfunc("Khan",token.c_str(), file_path);
cout << res << endl;
database_setval(fileid, token , res.c_str());
}
}
}
}
void map_path(string path, string fileid) {
//cout << "in map_path" << endl;
//cout << "path: " << path << " fileid: " << fileid << endl;
string token = "";
string attr = "";
stringstream ss2(path.c_str());
while(getline(ss2, token, '/')) {
//cout << "got token " << token << endl;
if(strcmp(token.c_str(),"null")!=0) {
if(attr.length()>0) {
//cout << "mapping " << attr << " to " << token << endl;
database_setval(fileid, attr, token);
if(attr=="location") {
int pos=find(server_ids.begin(),server_ids.end(),token)-server_ids.begin();
if( pos < server_ids.size() ) {
database_setval(fileid, "server", servers.at(pos));
}
}
attr = "";
} else {
attr = token;
}
}
}
//cout << "finished map_path" << endl << endl;
}
void unmap_path(string path, string fileid) {
//cout << "in unmap_path" << endl;
//cout << "path: " << path << " fileid: " << fileid << endl;
string token = "";
string attr = "";
stringstream ss2(path.c_str());
while(getline(ss2, token, '/')) {
//cout << "got token " << token << endl;
if(strcmp(token.c_str(),"null")!=0) {
if(attr.length()>0) {
//cout << "removing map " << attr << " to " << token << endl;
database_remove_val(fileid, attr, token);
if(attr=="location") {
int pos=find(server_ids.begin(),server_ids.end(),token)-server_ids.begin();
if( pos < server_ids.size() ) {
database_remove_val(fileid, "server", servers.at(pos));
}
}
attr = "";
} else {
attr = token;
}
}
}
//cout << "finished unmap_path" << endl << endl;
}
/*unmount the fuse file system*/
void unmounting(string mnt_dir) {
log_msg("in umounting\n");
#ifdef APPLE
string command = "umount " + mnt_dir + "\n";
#else
string command = "fusermount -zu " + mnt_dir + "\n";
#endif
if (system(command.c_str()) < 0) {
log_msg("Could not unmount mounted directory!\n");
log_msg(msg);
return;
}
log_msg("fusermount successful\n");
}
void* initializing_khan(void * mnt_dir) {
log_msg("In initialize\n");
//unmounting((char *)mnt_dir);
//Opening root directory and creating if not present
sprintf(msg, "khan_root[0] is %s\n", servers.at(0).c_str());
log_msg(msg);
//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 {
log_msg("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\n");
database_setval("setup","value","true");
//load metadata associatons
for(int i=0; i<servers.size(); i++){
//log_msg("servers: " + servers.at(i) + "\n");
process_transducers(servers.at(i));
}
//load metadata for each file on each server
string types=database_getval("allfiles","types");
sprintf(msg, "=== types to look for = %s\n", types.c_str());
log_msg(msg);
//log_msg("Server Size" + servers.size() + "\n");
for(int i=0; i<servers.size(); i++) {
/* if(servers.at(i) == "cloud") {
cout << " Cloud \n";
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 {
*/
log_msg("Not Cloud \n");
// string command = "find -type d | awk -F'/' '{print NF-1}' | sort -n | tail -1";
//string command = "find /net/hp100/ihpcae -type d | awk -F'/' '{print NF-1}' | sort -n | tail -1";
glob_t files;
string pattern= servers.at(0) + "/*";
static int experiment_id = 0;
set<string> experiments;
for(int count = 18; count > 0; count--)
{
sprintf(msg, "Globbing with pattern: %s .im7\n", pattern.c_str());
//log_msg("Globbing with pattern " + pattern + ".im7\n");
log_msg(msg);
glob((pattern +".im7").c_str(), 0, NULL, &files);
sprintf(msg, "Glob Buffer: %d\n", files.gl_pathc);
log_msg(msg);
//log_msg("Glob buffer" + files.gl_pathc + "\n");
// if(files.gl_pathc != 0 ) {
// experiment_id++;
// }
for(int j=0; j<files.gl_pathc; j++) {//for each file
string file_path = files.gl_pathv[j];
// experiments.insert(file_path.substr(0, file_path.size()-11));
// ostringstream ss;
// ss.flush();
// ss << experiments.size();
sprintf(msg, "*** FILE Path *** %s\n", file_path.c_str());
string ext = strrchr(file_path.c_str(),'.')+1;
string filename=strrchr(file_path.c_str(),'/')+1;
if(database_getval("name", filename) == "null" || 1) {
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));
// database_setval(fileid, "experiment_id", ss.str());
database_setval(fileid, "file_path", file_path);
for(int k=0; k<server_ids.size(); k++) {
database_setval(fileid, server_ids.at(k), "0");
}
process_file(servers.at(i), fileid, file_path);
} else {
string fileid = database_getval("name",filename);
database_setval(fileid,"server",servers.at(i));
database_setval(fileid,"location",server_ids.at(i));
}
}
pattern += "/*";
}
log_msg("At the end of initialize\n");
analytics();
return 0;
}
}
int khan_opendir(const char *c_path, struct fuse_file_info *fi) {
return 0;
}
bool find(string str, vector<string> arr) {
for(int i=0; i<arr.size(); i++) {
if(str == arr[i]) return true;
}
return false;
}
string str_intersect(string str1, string str2) {
vector<string> vec_1 = split(str1, ":");
vector<string> vec_2 = split(str2, ":");
vector<string> ret;
for(int i=0; i<vec_1.size(); i++) {
for(int j=0; j<vec_2.size(); j++) {
if((vec_1[i]==vec_2[j]) && (!find(vec_1[i], ret))) {
ret.push_back(vec_1[i]);
}
}
}
return join(ret,":");
}
bool content_has(string vals, string val) {
static string last_string = "";
static vector<string> last_vector;
vector<string> checks;
if(last_string == vals){
checks = last_vector;
} else {
checks = split(vals,":");
last_string = vals;
last_vector = checks;
}
for(int i=0; i<checks.size(); i++) {
if(checks[i]==val) {
return true;
}
}
return false;
}
void dir_pop_stbuf(struct stat* stbuf, string contents) {
cout << "Dir pop STBUF called ! " << endl;
time_t current_time;
time(¤t_time);
stbuf->st_mode=S_IFDIR | 0755;
stbuf->st_nlink=count_string(contents)+2;
stbuf->st_size=4096;
stbuf->st_mtime=current_time;
stbuf->st_uid = getuid();
stbuf->st_gid = getgid();
}
void file_pop_stbuf(struct stat* stbuf, string filename) {
time_t current_time;
time(¤t_time);
stbuf->st_mode=S_IFREG | 0644;
stbuf->st_nlink=1;
stbuf->st_size=get_file_size(filename);
stbuf->st_mtime=current_time;
stbuf->st_uid = getuid();
stbuf->st_gid = getgid();
}
string resolve_selectors(string path) {
//cout << "starting split" << endl << flush;
vector<string> pieces = split(path, "/");
//cout << "starting process" << endl << flush;
for(int i=0; i<pieces.size(); i++) {
//cout << "looking at " << pieces[i] << endl << flush;
if(pieces[i].at(0)==SELECTOR_C) {
//cout << "is a selector" << endl << flush;
vector<string> selectores = split(pieces[i], SELECTOR_S);
pieces[i]="";
//cout << selectores.size() << " selectors to be exact" << endl << flush;
for(int j=0; j<selectores.size(); j++) {
//cout << "checking " << selectores[j] << endl << flush;
bool matched = false;
string content = database_getvals("attrs");
//cout << "content " << content << endl << flush;
vector<string> attr_vec = split(content, ":");
//cout << "vs " << attr_vec.size() << " attrs" << endl << flush;
//for all attrs
for(int k=0; k<attr_vec.size(); k++) {
//cout << "on " << attr_vec[k] << endl << flush;
string vals = database_getvals(attr_vec[k]);
//cout << "with " << vals << endl << flush;
//see if piece is in vals
if(content_has(vals, selectores[j])) {
//if so piece now equals attr/val
if(pieces[i].length()>0) {
pieces[i]+="/";
}
matched = true;
pieces[i]+=attr_vec[k]+"/"+selectores[j];
break;
}
}
if(!matched) {
pieces[i]+="tags/"+selectores[j];
}
}
}
}
string ret = join(pieces, "/");
//cout << "selector path " << path << " resolved to " << ret << endl;
return ret;
}
int populate_getattr_buffer(struct stat* stbuf, stringstream &path) {
string attr, val, file, more;
string current = "none";
string current_path = path.str();
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 << "PrINT " << " " << attr << " " << query << endl;
if(query!="null") {
string content = database_getvals(attr);
// cout << "Query not null " << content << endl;
if(vint) {
// cout << "Vint is true " << endl;
// cout << "Value is " << content_has(content, val) << endl;
// cout << "Val is " << val << endl;
if(content_has(content, val) || (attr=="tags")) {
// cout << "Here1 " << val << endl;
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) {
// cout << "fint is true " << file << endl;
string fileid = database_getval("name",file);
if(content_has(dir_content, fileid)) {
if(!mint) {
// /attr/val/file path
string file_path = database_getval(fileid, "file_path");
file_pop_stbuf(stbuf, file_path);
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
cout << "Fint is flase " << dir_content + attrs_content << endl;
//if(dir_content != ""){
dir_pop_stbuf(stbuf, dir_content+attrs_content);
/*}
else{
cout << "Here 1234" << endl;
string p;
cout << "ABC " << endl;
//getline(path, p);
cout << "ADSASDAS " << (mountpoint + "/" + current_path).c_str() << endl;
//cout << current_path;
//cout << "DEF " << endl;
//cout << getline(path, p);
//unlink((mountpoint + "/" + current_path).c_str());
// unlink();
}*/
return 0;
}
}
} else {
// /attr dir
//cout << " Here 2 " << content << endl;
dir_pop_stbuf(stbuf, content);
return 0;
}
}
} else {
string types=database_getvals("attrs");
//cout << " HEre 3 " << types << endl;
dir_pop_stbuf(stbuf, types);
return 0;
}
}
return -2;
}
static int khan_getattr(const char *c_path, struct stat *stbuf) {
//cout << "started get attr" << endl << flush;
string pre_processed = c_path+1;
if(pre_processed == ".DS_Store") {
file_pop_stbuf(stbuf, pre_processed);
return 0;
}
//cout << "starting to resolve selectors" << endl << flush;
string after = resolve_selectors(pre_processed);
stringstream path(after);
//cout << "working to pop buffer" << endl << flush;
//file_pop_stbuf(stbuf, "test");
//int ret = 0;
int ret = populate_getattr_buffer(stbuf, path);
//cout << "ended get attr" << endl << flush;
return ret;
}
void dir_pop_buf(void* buf, fuse_fill_dir_t filler, string content, bool convert) {
sprintf(msg, "Inside dir_pop_buf: %s\n", content.c_str());
log_msg(msg);
vector<string> contents = split(content, ":");
for(int i=0; i<contents.size(); i++) {
if(convert) {
string filename = database_getval(contents[i].c_str(), "name");
sprintf(msg, "dir_pop_buf loop%s\n", filename.c_str());
log_msg(msg);
filler(buf, filename.c_str(), NULL, 0);
} else {
cout << "Convert is false " << endl;
filler(buf, contents[i].c_str(), NULL, 0);
}
}
}
void populate_readdir_buffer(void* buf, fuse_fill_dir_t filler, stringstream &path) {
log_msg("Populate Read Dir Buffer\n");
sprintf(msg, "Path is %s\n", path.str().c_str());
log_msg(msg);
string attr, val, file, more;
string current_content = "none";
string current_attrs = "none";
string non_empty_content = "";
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;
cout << "HO HO JUMPING!! " << endl;
string content = database_getvals("attrs");
cout << "Attrs is " << content << endl;
if(aint) {
cout << "Aint is true " << endl;
cout << "Attr " << attr << endl;
if(content_has(content, attr)) {
current_attrs += ":";
current_attrs += attr;
content = database_getvals(attr);
cout << "Current Content " << current_content << endl;
if(current_content != "none"){
cout << "Content " << content << endl;
non_empty_content = "";
vector<string> vec_1 = split(content, ":");
for(int i = 0; i < vec_1.size(); ++i){
cout << "Attr " << attr << " Val " << vec_1[i] << endl;
string dir_content = database_getval(attr, vec_1[i]);
if(current_content!="none") {
dir_content = str_intersect(current_content, dir_content);
}
cout << "Iteration " << " ** " << i << " " << dir_content << endl;
if(dir_content != ""){
non_empty_content += vec_1[i] + ":";
}
}
}else non_empty_content = content;
cout << "Non Empty Content " << non_empty_content << endl;
if(vint) {
cout << " Content is " << content << endl;
cout << "Value is " << content_has(content, val) << endl;
cout << "Vint is true " << endl;
if(content_has(content, val) || (attr=="tags")) {
string dir_content = database_getval(attr, val);
cout << " ABRA " << endl;
if(current_content!="none") {
cout << " f sdfdsfsdf " << endl;
dir_content = intersect(current_content, dir_content);
}
string attrs_content = database_getvals("attrs");
if(fint) {
cout << "Fint is true " << endl;
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
sprintf(msg, "Else %s, %s\n\n\n\n\n\n", attrs_content.c_str(), current_attrs.c_str());
log_msg(msg);
attrs_content = subtract(attrs_content, current_attrs);
cout << "Dir Content " << dir_content << endl;
cout << "Attr Content " << attrs_content << endl;
dir_pop_buf(buf, filler, dir_content, true);
dir_pop_buf(buf, filler, attrs_content, false);
}
}
} else {
// /attr dir
cout << "Going solo1 " << non_empty_content << endl;
dir_pop_buf(buf, filler, non_empty_content, false);
}
}
} else {
cout << " Going solo2 " << endl;
dir_pop_buf(buf, filler, content, false);
}
}
cout << "populate read dir end " << endl;
}
static int xmp_readdir(const char *c_path, void *buf, fuse_fill_dir_t filler,off_t offset, struct fuse_file_info *fi) {
sprintf(msg, "Khan Read directory: %s\n", c_path);
log_msg(msg);
filler(buf, ".", NULL, 0);
filler(buf, "..", NULL, 0);
string pre_processed = c_path+1;
string after = resolve_selectors(pre_processed);
stringstream path(after);
populate_readdir_buffer(buf, filler, path);
//cout << "xmp_readdir end " << endl;
return 0;
}
int khan_open(const char *path, struct fuse_file_info *fi) {
sprintf(msg, "Khan Open directory %s\n ", path);
log_msg(msg);
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;
}
int xmp_access(const char *path, int mask)
{
sprintf(msg, "Khan Access: %s", path);
log_msg(msg);
char *path_copy=strdup(path);
if(strcmp(path,"/")==0) {
log_msg("at root");
return 0;
}
// if(strcmp(path,"/")==0) {
log_msg("at root\n");
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) {
clock_gettime(CLOCK_REALTIME,&stop);
time_spent = (stop.tv_sec-start.tv_sec)+(stop.tv_nsec-start.tv_nsec)/BILLION; tot_time += time_spent;;
access_avg_time=(access_avg_time*(access_calls-1)+time_spent)/access_calls;
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;
}
static int xmp_mknod(const char *path, mode_t mode, dev_t rdev) {
log_msg("in xmp_mknod\n");
path=append_path2(basename(strdup(path)));
sprintf(msg,"khan_mknod, path=%s\n",path);
log_msg(msg);
int res;
if (S_ISFIFO(mode))
res = mkfifo(path, mode);
else
res = mknod(path, mode, rdev);
if (res == -1) {
fprintf(stderr, "\nmknod error \n");
return -errno;
}
return 0;
}
static int xmp_mkdir(const char *path, mode_t mode) {
struct timespec mkdir_start, mkdir_stop;
sprintf(msg, "Khan mkdir: %s\n", path);
log_msg(msg);
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;