-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
1136 lines (1052 loc) · 31.3 KB
/
main.c
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
#include <unistd.h> //for getopt, optarg
#include <stdio.h>
#include <string.h>//remove warning strcpy
#include <signal.h>//for signals
#include <stdlib.h>
#include <pthread.h> //for pthread functions
#include <dirent.h> //for traversing directories
#include <sys/stat.h> // for lstat, stat, open
#include <fcntl.h> //for O_RDONLY
#include <time.h>
#define ERR(source) (perror(source),\
fprintf(stderr,"%s:%d\n",__FILE__,__LINE__),\
exit(EXIT_FAILURE))
#define PATH_L 90
#define OPTION_L 20
#define INDEX_SIZE 50
#define TYPE_L 5
//#define DEBUG
typedef struct index_t
{
char *file_name; //file name
char *full_path; //a full (absolute) path to a file
off_t size; //size
uid_t st_uid; //owner's uid
char* type;
} index_t;
typedef struct data_t
{
index_t*index;
int places_taken;
int prev_size;
char * path_d;
char *path_f;
int t;
int period_re_index;
int in_progress;
int can_be_cancelled;
sigset_t mask;
pthread_mutex_t *mxindex;
pthread_mutex_t mxother;
struct data_t * old_data;
} data_t;
void read_arguments(int argc, char**argv, char * path_d, char *path_f, int *t,int* period_re_index);
void usage();
void get_mole_dir(char *path_d);
void get_mole_index_path(char *path_f);
void option_check(char*arg);
void argument_check(char * path_d, char *path_f, int *t,int* period_re_index);
void* indexing(void *arg);
void traverse_files(void *arg,char *path);
void remove_slash(char*path);
int identify_type(void *arg, char*path);
void save_to_file(void *arg);
int read_from_file(void *arg);
void count_file_type(void *arg);
void get_option_val(char*option,char*option_value);
void get_option(void *arg,pthread_t thread_index);
void sig_handler(int sig);
void sethandler( void (*f)(int), int sigNo);
void print_owner(void*arg,int val);
void print_largerthan(void*arg,int val);
void print_namepart(void *arg, char* option_value);
int is_in_file(void*arg,char*name,char *path);
int is_in_index(void*arg,char*name,char*path);
int check_length(char *string);
void create_data(data_t *data);
void copy_data(data_t *data, data_t *old_data);
void free_data(data_t *data);
void reset_index(data_t* data);
void unlock_all(data_t *data);
int main(int argc, char**argv)
{
pthread_t thread_index;
data_t *data=malloc(sizeof(data_t));
create_data(data);
data->old_data=malloc(sizeof(data_t));
create_data(data->old_data);
read_arguments(argc,argv,data->path_d,data->path_f,&data->t,&data->period_re_index);
#ifdef DEBUG
argument_check(data->path_d,data->path_f,&data->t,&data->period_re_index);
#endif
// pthread_attr_t attribute;
// if(pthread_attr_init(&attribute))
// ERR("Couldn't initialise attribute");
// if(pthread_attr_setdetachstate(&attribute,PTHREAD_CREATE_DETACHED))
// ERR("Couldn't set attribute to detached");
sigset_t mask, oldmask;
sigemptyset(&mask);
sigaddset(&mask, SIGUSR2);
if(pthread_sigmask(SIG_BLOCK, &mask, &oldmask))
ERR("SIGMASk");
sethandler(sig_handler,SIGUSR1);
data->mask=mask;
if(pthread_create(&thread_index,NULL,indexing,data))
ERR("Couldn't create indexing thread");
//pthread_detach(thread_index);
// if(pthread_cancel(thread_index))
// ERR("Couldn't cancel indexing thread");
get_option(data,thread_index);
if(pthread_join(thread_index,NULL))
ERR("Couldn't join thread");
sigprocmask(SIG_UNBLOCK, &mask, NULL);
//pthread_attr_destroy(&attribute);
free_data(data);
free_data(data->old_data);
free(data->old_data);
free(data);
//pthread_exit(0);
return 0;
}
void read_arguments(int argc, char**argv, char* path_d, char *path_f, int *t, int* period_re_index)
{
int was_f_present=0;
int was_d_present=0;
int opt;
while ((opt = getopt(argc, argv, ":d:f:t:")) != -1)
{
switch (opt)
{
case 'd':
option_check(optarg);
strcpy(path_d,optarg);
remove_slash(path_d);
was_d_present=1;
break;
case 'f':
option_check(optarg);
strcpy(path_f,optarg);
was_f_present=1;
break;
case 't':
option_check(optarg);
*period_re_index=1;
*t = atoi(optarg);
if(*t<30||*t>7200)
ERR("Wrong range of t");
break;
default:
usage();
ERR("Wrong arguments");
}
}
if(!was_d_present)
get_mole_dir(path_d);
if(!was_f_present)
get_mole_index_path(path_f);
}
void get_mole_dir(char *path_d)
{
setenv("MOLE_DIR","MOLE_DIR",1);
char *mole_dir=malloc(PATH_L*sizeof(char));
if(mole_dir==NULL)
ERR("mole_dir malloc");
if(snprintf(mole_dir, PATH_L, "%s", getenv("MOLE_DIR")) >= PATH_L)
{
ERR("Size of buffer was too small");
}
mole_dir =getenv("MOLE_DIR");
if(mole_dir==NULL)
{
ERR("Couldn't retrieve value from MOLE_DIR");
}
else
strcpy(path_d,mole_dir);
//free(mole_dir);
}
void get_mole_index_path(char *path_f)
{
setenv("MOLE_INDEX_PATH","MOLE_INDEX_PATH",1);
char *mole_index_path=malloc(PATH_L*sizeof(char));
if(mole_index_path==NULL)
ERR("mole_index_path malloc");
if(snprintf(mole_index_path, PATH_L, "%s", getenv("MOLE_INDEX_PATH")) >= PATH_L)
{
ERR("Size of buffer was too small");
}
mole_index_path=getenv("MOLE_INDEX_PATH");
if(mole_index_path==NULL)
{
FILE *fp;
mole_index_path=calloc(PATH_L,sizeof(char));
fp=fopen(".mole-index","r");
if(fp==NULL)
ERR("Couldnt open the file .mole-index");
fgets(mole_index_path,PATH_L,fp);
fclose(fp);
}
strcpy(path_f,mole_index_path);
// free(mole_index_path);
}
void usage()
{
fprintf(stderr, "Usage: [-d path] [-f path] [-t n]\nn is from range [30,7200]");
}
void option_check(char*arg)
{
if(0==strcmp(arg," -f")||0==strcmp(arg," -t")||0==strcmp(arg," -d")||
0==strcmp(arg,"-f")||0==strcmp(arg,"-t")||0==strcmp(arg,"-d"))
{
usage();
ERR("Empty argument");
}
}
void argument_check(char * path_d, char *path_f, int *t,int* period_re_index)
{
printf("------LOADED VALUES-------\n");
if(*period_re_index)
{
printf("periodic re-indexing enabled value:%d\n",*t);
}
else
{
printf("periodic re-indexing disabled\n");
}
printf("path_d:%s\n",path_d);
printf("path_f:%s\n",path_f);
printf("--------------------------\n");
}
void* indexing(void* arg)
{
//pthread_detach(pthread_self());
data_t *data=arg;
int signo=0;
struct stat st;
struct timespec tt_new;
pthread_mutex_lock(&data->mxother);
struct timespec currtime;
clock_gettime(CLOCK_REALTIME, &currtime);
if(stat(data->path_f,&st)==0)
{
tt_new.tv_sec=data->t-(currtime.tv_sec-st.st_ctim.tv_sec);
//printf("%d\n", currtime.tv_sec-st.st_ctim.tv_sec);
}
else{
tt_new.tv_sec=data->t;}
tt_new.tv_nsec=0;
if(tt_new.tv_sec<0)
tt_new.tv_sec=0;
pthread_mutex_unlock(&data->mxother);
if(!read_from_file(arg))
{
pthread_mutex_lock(&data->mxother);
data->in_progress=1;
pthread_mutex_unlock(&data->mxother);
data->places_taken=0;
//reset_index(data);
unlock_all(data);
traverse_files(arg,data->path_d);
save_to_file(arg);
copy_data(data,data->old_data);
printf("Finished indexing...\n");
pthread_mutex_lock(&data->mxother);
data->in_progress=0;
pthread_mutex_unlock(&data->mxother);
}
while(1)
{
pthread_mutex_lock(&data->mxother);
if(data->period_re_index)
{
nanosleep(&tt_new,NULL);
tt_new.tv_sec=data->t;
data->in_progress=1;
pthread_mutex_unlock(&data->mxother);
data->places_taken=0;
//reset_index(data);
unlock_all(data);
traverse_files(arg,data->path_d);
save_to_file(arg);
copy_data(data,data->old_data);
printf("Finished indexing...\n");
pthread_mutex_lock(&data->mxother);
data->in_progress=0;
pthread_mutex_unlock(&data->mxother);
}
else
{
sigwait(&data->mask,&signo);
if(signo==SIGUSR2)
{
data->in_progress=1;
pthread_mutex_unlock(&data->mxother);
data->places_taken=0;
//reset_index(data);
unlock_all(data);
traverse_files(arg,data->path_d);
save_to_file(arg);
copy_data(data,data->old_data);
printf("Finished indexing...\n");
pthread_mutex_lock(&data->mxother);
data->in_progress=0;
pthread_mutex_unlock(&data->mxother);
}
}
}
return NULL;
}
void traverse_files(void *arg,char *given_path)
{
char *path=calloc(2*PATH_L,sizeof(char));
struct stat sb;
if(NULL==path)
ERR("Path calloc");
data_t *data=arg;
DIR *directory;
struct dirent *dir_entry;
directory=opendir(given_path);
if(NULL==directory)
{
free(path);
return;
}
while((dir_entry=readdir(directory))!=NULL)
{
if(strcmp(dir_entry->d_name,".")!=0&&strcmp(dir_entry->d_name,"..")!=0)
{
strcpy(path,given_path);
strcat(path,"/");
strcat(path,dir_entry->d_name);
strcat(path,"\0");
if(check_length(path))
{
printf("Absolute path: %s of file was too long (longer than %d)\n",path,PATH_L);
continue;
}
lstat(path,&sb);
//if(!is_in_index(arg,dir_entry->d_name,path))
//{
if(dir_entry->d_type==DT_DIR)
{
pthread_mutex_lock(&data->mxindex[data->places_taken]);
strcpy(data->index[data->places_taken].type,"DIR\0");
pthread_mutex_unlock(&data->mxindex[data->places_taken]);
}
if(dir_entry->d_type==DT_DIR||!identify_type(arg,path))
{
pthread_mutex_lock(&data->mxindex[data->places_taken]);
strcpy(data->index[data->places_taken].file_name,dir_entry->d_name);
strcat(data->index[data->places_taken].file_name,"\0");
strcpy(data->index[data->places_taken].full_path,path);
data->index[data->places_taken].st_uid=sb.st_uid;
data->index[data->places_taken].size=sb.st_size;
pthread_mutex_unlock(&data->mxindex[data->places_taken]);
pthread_mutex_lock(&data->mxother);
data->places_taken++;
pthread_mutex_unlock(&data->mxother);
}
// }
traverse_files(arg,path);
}
}
if(closedir(directory))
ERR("Couldnt close directory");
free(path);
}
void remove_slash(char*path)
{
int prev=0;
for(int i=1; i<PATH_L; i++)
{
if(path[i]=='\0'&&path[prev]=='/')
path[prev]='\0';
prev=i;
}
}
int identify_type(void *arg, char*path)
{
if(arg==NULL)
return 1;
data_t *data=arg;
int file_descr;
char *magic_numbers=malloc(8*sizeof(char));
if(magic_numbers==NULL)
ERR("magic_numbers malloc");
if((file_descr=open(path,O_RDONLY))>=0)
{
pthread_mutex_lock(&data->mxindex[data->places_taken]);
int check=read(file_descr,magic_numbers,8);
if(-1==check)
ERR("Couldn't file signature");
else if(0==check)
{
pthread_mutex_unlock(&data->mxindex[data->places_taken]);
close(file_descr);
free(magic_numbers);
return 1;
}
if(magic_numbers[0]==80&&magic_numbers[1]==75&&((magic_numbers[2]==3&&magic_numbers[3]==4)||(magic_numbers[2]==5&&magic_numbers[3]==6)||(magic_numbers[2]==7&&magic_numbers[3]==8)))
{
strcpy(data->index[data->places_taken].type,"ZIP\0");
close(file_descr);
free(magic_numbers);
pthread_mutex_unlock(&data->mxindex[data->places_taken]);
return 0;
}
else if(magic_numbers[0]==31&&magic_numbers[1]==-117)
{
strcpy(data->index[data->places_taken].type,"GZIP\0");
close(file_descr);
free(magic_numbers);
pthread_mutex_unlock(&data->mxindex[data->places_taken]);
return 0;
}
else if(magic_numbers[0]==-119&&magic_numbers[1]==80&&magic_numbers[2]==78&&magic_numbers[3]==71&&magic_numbers[4]==13&&magic_numbers[5]==10&&magic_numbers[6]==26&&magic_numbers[7]==10)
{
strcpy(data->index[data->places_taken].type,"PNG\0");
close(file_descr);
free(magic_numbers);
pthread_mutex_unlock(&data->mxindex[data->places_taken]);
return 0;
}
else if(magic_numbers[0]==-1&&magic_numbers[1]==-40&&magic_numbers[2]==-1&&magic_numbers[3]==-32)
{
strcpy(data->index[data->places_taken].type,"JPEG\0");
close(file_descr);
free(magic_numbers);
pthread_mutex_unlock(&data->mxindex[data->places_taken]);
return 0;
}
else
{
close(file_descr);
free(magic_numbers);
pthread_mutex_unlock(&data->mxindex[data->places_taken]);
return 1;
}
}
else
{
close(file_descr);
free(magic_numbers);
pthread_mutex_unlock(&data->mxindex[data->places_taken]);
return 1;
}
}
void save_to_file(void *arg)
{
if(arg==NULL)
return;
data_t *data=arg;
data->can_be_cancelled=0;
int file_descriptor;
file_descriptor=open(data->path_f,O_WRONLY|O_TRUNC|O_CREAT,0644);
if(file_descriptor==-1)
ERR("couldn't open the file");
for(int i=0; i<data->places_taken; i++)
{
//pthread_mutex_lock(&data->mxindex[i]);
// if(data->index[i].file_name[0]==0)
// {
//pthread_mutex_unlock(&data->mxindex[i]);
//break;
//}
if(data->index[i].file_name==NULL||data->index[i].full_path==NULL||data->index[i].type==NULL)
continue;
// pthread_mutex_unlock(&data->mxindex[i]);
if(is_in_file(arg,data->index[i].file_name,data->index[i].full_path))
{
pthread_mutex_lock(&data->mxindex[i]);
if(-1==write(file_descriptor,data->index[i].file_name,PATH_L))
ERR("write to index");
if(-1==write(file_descriptor,data->index[i].full_path,PATH_L))
ERR("Write to index");
if(-1==write(file_descriptor,&(data->index[i].size),sizeof(off_t)))
ERR("Write to index");
if(-1==write(file_descriptor,&(data->index[i].st_uid),sizeof(uid_t)))
ERR("Write to index");
if(-1==write(file_descriptor,data->index[i].type,TYPE_L))
ERR("Write to index");
pthread_mutex_unlock(&data->mxindex[i]);
}
//pthread_mutex_unlock(&data->mxindex[i]);
}
close(file_descriptor);
data->can_be_cancelled=1;
}
int read_from_file(void *arg)
{
data_t *data=arg;
int file_descriptor;
file_descriptor=open(data->path_f,O_RDONLY,0);
if(file_descriptor==-1)
return 0;
char * name_path=malloc(PATH_L*sizeof(char));
off_t size;
uid_t uid;
char*type=malloc(TYPE_L);
int result;
int i=0;
while(1)
{
pthread_mutex_lock(&data->mxother);
pthread_mutex_lock(&data->mxindex[data->places_taken]);
if(i>=INDEX_SIZE)
{
ERR("INDEX_SIZE IS TOO SMALL");
break;
}
result=read(file_descriptor,name_path,PATH_L);
if(0==result)
{
pthread_mutex_unlock(&data->mxother);
pthread_mutex_unlock(&data->mxindex[data->places_taken]);
break;
}
else if(-1==result)
ERR("Read index");
strcpy(data->index[data->places_taken].file_name,name_path);
if(-1==read(file_descriptor,name_path,PATH_L))
ERR("Read index");
strcpy(data->index[data->places_taken].full_path,name_path);
if(-1==read(file_descriptor,&size,sizeof(off_t)))
ERR("Read index");
data->index[data->places_taken].size=size;
if(-1==read(file_descriptor,&uid,sizeof(uid_t)))
ERR("Read index");
data->index[data->places_taken].st_uid=uid;
if(-1==read(file_descriptor,type,TYPE_L))
ERR("Read index");
strcpy(data->index[data->places_taken].type,type);
i++;
data->places_taken++;
pthread_mutex_unlock(&data->mxother);
pthread_mutex_unlock(&data->mxindex[data->places_taken]);
}
close(file_descriptor);
free(name_path);
free(type);
return 1;
}
void count_file_type(void *arg)
{
data_t *data=arg;
int jpeg=0;
int dir=0;
int zip=0;
int png=0;
int gzip=0;
for(int i=0; i<data->places_taken; i++)
{
//pthread_mutex_lock(&data->mxindex[i]);
if(data->index[i].file_name[0]==0)
{
//pthread_mutex_unlock(&data->mxindex[i]);
break;
}
if(!strncmp(data->index[i].type,"DIR",3))
{
dir++;
}
else if(!strncmp(data->index[i].type,"PNG",3))
{
png++;
}
else if(!strncmp(data->index[i].type,"ZIP",3))
{
zip++;
}
else if(!strncmp(data->index[i].type,"JPEG",4))
{
jpeg++;
}
else if(!strncmp(data->index[i].type,"GZIP",4))
{
gzip++;
}
//pthread_mutex_unlock(&data->mxindex[i]);
}
printf("DIR: %d\n",dir);
printf("ZIP: %d\n",zip);
printf("GZIP: %d\n",gzip);
printf("PNG: %d\n",png);
printf("JPEG: %d\n",jpeg);
}
void get_option_val(char*option,char*option_value)
{
int space=0;
for(int i=0; i<OPTION_L; i++)
{
if(option[i]==' ')
{
space=i;
break;
}
}
int j=0;
for(int i=space+1; i<OPTION_L; i++)
{
if(option[i]=='\0'||option[i]=='\n')
{
option_value[j]='\0';
break;
}
option_value[j]=option[i];
j++;
}
}
void get_option(void *arg, pthread_t thread_index)
{
data_t*data=arg;
char *option =malloc(OPTION_L);
if(option==NULL)
ERR("Option malloc");
char *option_value=malloc(OPTION_L);
if(option_value==NULL)
ERR("Option_value malloc");
struct timespec tt= {0,5000};
while(1)
{
fgets(option,OPTION_L,stdin);
if(!strncmp(option,"exit\n",5))
{
printf("exiting...\n");
while(data->in_progress==1)
nanosleep(&tt,NULL);
//pthread_detach(thread_index);
if(pthread_cancel(thread_index))
ERR("Couldn't cancel thread");
break;
}
else if(!strncmp(option,"exit!\n",6))
{
printf("Exiting!...\n");
while(data->can_be_cancelled==0)
nanosleep(&tt,NULL);
//pthread_detach(thread_index);
if(pthread_cancel(thread_index))
ERR("Couldn't cancel thread");
break;
}
else if(!strncmp(option,"index\n",6))
{
if(data->in_progress==0)
{
if(data->period_re_index)
pthread_kill(thread_index,SIGUSR1);
else
pthread_kill(thread_index,SIGUSR2);
}
else
{
printf("Indexing is already in progess\n");
}
}
else if(!strncmp(option,"count\n",6))
{
if(data->in_progress==0)
count_file_type(data);
else
count_file_type(data->old_data);
}
else if(!strncmp(option,"largerthan ",11))
{
get_option_val(option,option_value);
int val=atoi(option_value);
if(data->in_progress==0)
print_largerthan(arg,val);
else
print_largerthan(data->old_data,val);
}
else if(!strncmp(option,"namepart ",9))
{
get_option_val(option,option_value);
if(data->in_progress==0)
print_namepart(arg,option_value);
else
print_namepart(data->old_data,option_value);
}
else if(!strncmp(option,"owner ",6))
{
get_option_val(option,option_value);
int val=atoi(option_value);
if(data->in_progress==0)
print_owner(arg,val);
else
print_owner(data->old_data,val);
}
else
{
fprintf(stderr,"Incorrect option!\n");
}
}
free(option);
free(option_value);
}
void sig_handler(int sig)
{
return;
}
void sethandler( void (*f)(int), int sigNo)
{
struct sigaction act;
memset(&act, 0, sizeof(struct sigaction));
act.sa_handler = f;
if (-1==sigaction(sigNo, &act, NULL)) ERR("sigaction");
}
void print_owner(void*arg,int val)
{
data_t *data=arg;
int howmany=0;
for(int i=0; i<data->places_taken; i++)
{
// pthread_mutex_lock(&data->mxindex[i]);
if(data->index[i].st_uid==val)
{
howmany++;
}
// pthread_mutex_unlock(&data->mxindex[i]);
}
if(howmany>=3)
{
FILE *fp;
fp=popen("less","w");
if(fp==NULL)
ERR("popen");
for(int i=0; i<data->places_taken; i++)
{
//pthread_mutex_lock(&data->mxindex[i]);
if(data->index[i].st_uid==val)
{
fprintf(fp,"%s\n",data->index[i].full_path);
fprintf(fp,"%ld\n",data->index[i].size);
fprintf(fp,"%s\n",data->index[i].type);
}
//pthread_mutex_unlock(&data->mxindex[i]);
}
pclose(fp);
}
else
{
printf("-------files with owener uid == %d---------\n",val);
for(int i=0; i<data->places_taken; i++)
{
//pthread_mutex_lock(&data->mxindex[i]);
if(data->index[i].st_uid==val)
{
printf("%s\n",data->index[i].full_path);
printf("%ld\n",data->index[i].size);
printf("%s\n",data->index[i].type);
}
//pthread_mutex_unlock(&data->mxindex[i]);
}
}
}
void print_largerthan(void*arg,int val)
{
data_t *data=arg;
int howmany=0;
for(int i=0; i<data->places_taken; i++)
{
//pthread_mutex_lock(&data->mxindex[i]);
if(data->index[i].size>val)
{
howmany++;
}
//pthread_mutex_unlock(&data->mxindex[i]);
}
if(howmany>=3)
{
FILE *fp;
fp=popen("less","w");
if(fp==NULL)
ERR("popen");
for(int i=0; i<data->places_taken; i++)
{
// pthread_mutex_lock(&data->mxindex[i]);
if(data->index[i].size>val)
{
fprintf(fp,"%s\n",data->index[i].full_path);
fprintf(fp,"%ld\n",data->index[i].size);
fprintf(fp,"%s\n",data->index[i].type);
}
// pthread_mutex_unlock(&data->mxindex[i]);
}
pclose(fp);
}
else
{
printf("-------files with size > %d---------\n",val);
for(int i=0; i<data->places_taken; i++)
{
//pthread_mutex_lock(&data->mxindex[i]);
if(data->index[i].size>val)
{
printf("%s\n",data->index[i].full_path);
printf("%ld\n",data->index[i].size);
printf("%s\n",data->index[i].type);
}
//pthread_mutex_unlock(&data->mxindex[i]);
}
}
}
void print_namepart(void *arg, char* option_value)
{
data_t *data=arg;
int howmany=0;
for(int i=0; i<data->places_taken; i++)
{
//pthread_mutex_lock(&data->mxindex[i]);
if(strstr(data->index[i].file_name,option_value)!=NULL)
{
howmany++;
}
//pthread_mutex_unlock(&data->mxindex[i]);
}
if(howmany>=3)
{
FILE *fp;
fp=popen("less","w");
if(fp==NULL)
ERR("popen");
for(int i=0; i<data->places_taken; i++)
{
//pthread_mutex_lock(&data->mxindex[i]);
if(strstr(data->index[i].file_name,option_value)!=NULL)
{
fprintf(fp,"%s\n",data->index[i].full_path);
fprintf(fp,"%ld\n",data->index[i].size);
fprintf(fp,"%s\n",data->index[i].type);
}
//pthread_mutex_unlock(&data->mxindex[i]);
}
pclose(fp);
}
else
{
for(int i=0; i<data->places_taken; i++)
{
//pthread_mutex_lock(&data->mxindex[i]);
if(strstr(data->index[i].file_name,option_value)!=NULL)
{
printf("%s\n",data->index[i].full_path);
printf("%ld\n",data->index[i].size);
printf("%s\n",data->index[i].type);
}
//pthread_mutex_unlock(&data->mxindex[i]);
}
}
}
int is_in_file(void*arg,char*name,char*path)
{
if(name==NULL||path==NULL||arg==NULL)
return 0;
int index=0;
int index2=0;
for(int i=0; i<PATH_L; i++)
{
if(name[i]=='\0')
{
index=i+1;
break;
}
}
for(int i=0; i<PATH_L; i++)
{
if(path[i]=='\0')
{
index2=i+1;
break;
}
}
data_t *data=arg;
int file_descriptor;
file_descriptor=open(data->path_f,O_RDONLY|O_CREAT,0644);
if(file_descriptor==-1)
ERR("Couldn't open the file");
char * temp=malloc(PATH_L*sizeof(char));
if(temp==NULL)
ERR("temp malloc error");
char * temp2=malloc(PATH_L*sizeof(char));
if(temp2==NULL)
ERR("temp malloc error");
off_t temp3;
uid_t temp4;
char*temp5=malloc(TYPE_L);
if(temp5==NULL)
ERR("temp malloc error");
for(int i=0; i<data->places_taken; i++)
{
if(data->index[i].file_name[0]==0)
{
break;
}
if(-1==read(file_descriptor,temp,PATH_L))
ERR("Read index");
if(-1==read(file_descriptor,temp2,PATH_L))
ERR("Read index");
if(!strncmp(name,temp,index)&&!strncmp(path,temp2,index2))
{
close(file_descriptor);
free(temp);
free(temp2);
free(temp5);
return 0;
}
if(-1==read(file_descriptor,&temp3,sizeof(off_t)))
ERR("Read index");
if(-1==read(file_descriptor,&temp4,sizeof(uid_t)))
ERR("Read index");
if(-1==read(file_descriptor,temp5,TYPE_L))
ERR("Read index");
}
close(file_descriptor);
free(temp);
free(temp2);
free(temp5);
return 1;
}
int is_in_index(void*arg,char*name,char*path)
{
data_t *data=arg;
for(int i=0; i<data->places_taken; i++)
{
//pthread_mutex_lock(&data->mxindex[i]);
if(!strcmp(data->index[i].full_path,path))
{
// pthread_mutex_unlock(&data->mxindex[i]);
return 1;
}