-
Notifications
You must be signed in to change notification settings - Fork 2
/
khan.cpp
executable file
·1455 lines (1331 loc) · 44 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;
PyObject* cloud_interface;
ofstream rename_times;
ofstream start_times;
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);
}
void process_transducers(string server) {
if(server == "cloud") {
return;
}
string line;
ifstream transducers_file((server+"/transducers.txt").c_str());
getline(transducers_file, line);
while(transducers_file.good()){
//cout << "=============== got type = " << line <<endl;
//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");
string ext=line;
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,':');
//cout << "============ checking attr = "<<attr<<endl;
//cout << "============ checking command = "<<command<<endl;
attr=trim(attr);
database_setval(ext,"attrs",attr);
database_setval(attr+"gen","command",command);
getline(transducers_file,line);
firstchar=line.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);
}
}
}
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;
}
void unmounting(string mnt_dir) {
//log_msg("in umounting");
#ifdef APPLE
string command = "umount " + mnt_dir + "\n";
#else
string command = "fusermount -u " + mnt_dir + "\n";
#endif
if (system(command.c_str()) < 0) {
sprintf(msg,"Could not unmount mounted directory!\n");
//log_msg(msg);
return;
}
//log_msg("fusermount successful\n");
}
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;
}
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,":");
}
string last_string = "";
vector<string> last_vector;
bool content_has(string vals, string val) {
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) {
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;
}
void file_pop_stbuf(struct stat* stbuf, string filename) {
time_t current_time;
time(¤t_time);
stbuf->st_mode=S_IFREG | 0666;
stbuf->st_nlink=1;
stbuf->st_size=get_file_size(filename);
stbuf->st_mtime=current_time;
}
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";
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;
}
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) {
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);
}
}
}
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);
}
}
}
static int xmp_readdir(const char *c_path, void *buf, fuse_fill_dir_t filler,off_t offset, struct fuse_file_info *fi) {
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);
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;
}
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;
}
static int xmp_mknod(const char *path, mode_t mode, dev_t rdev) {
//log_msg("in xmp_mknod");
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;
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;
}
static int xmp_readlink(const char *path, char *buf, size_t size) {
//log_msg("xmp_readlink");
//TODO: handle in vold somehow
//log_msg("In readlink\n");
int res = -1;
path=append_path2(basename(strdup(path)));
//res = readlink(path, buf, size - 1);
if (res == -1)
return -errno;
buf[res] = '\0';
return 0;
}
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;
}
static int xmp_rmdir(const char *path) {
//if hardcoded, just remove
database_remove_val("alldirs","paths",path);
//if exists
//get contained files
//get attrs+vals from path
//unset files attrs
//if entire attr, remove attr
return 0;
}
static int xmp_symlink(const char *from, const char *to) {
//log_msg("in xmp_symlink");
//TODO: handle in vold somehow
int res=-1;
from=append_path2(basename(strdup(from)));
to=append_path2(basename(strdup(to)));
sprintf(msg,"In symlink creating a symbolic link from %s to %s\n",from, to);
//log_msg(msg);
//res = symlink(from, to);
if (res == -1)
return -errno;
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;
}
static int xmp_link(const char *from, const char *to) {
//log_msg("in xmp_link");
//TODO:handle in vold somehow...
int retstat = 0;
from=append_path2(basename(strdup(from)));
to=append_path2(basename(strdup(to)));
sprintf(msg,"khan_link initial path=\"%s\", initial to=\"%s\")\n",from, to);
//log_msg(msg);
retstat = link(from,to);
return retstat;
}
static int xmp_chmod(const char *path, mode_t mode) {
//log_msg("in xmp_chmod");
int res;
path=append_path2(basename(strdup(path)));
sprintf(msg, "In chmod for: %s\n",path);
//log_msg(msg);
res = chmod(path, mode);
#ifdef APPLE