-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
601 lines (497 loc) · 11.2 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
///////xmp lib https://github.com/rfjakob/fuse/blob/master/example/fusexmp_fh.c
#define FUSE_USE_VERSION 28
#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <dirent.h>
#include <regex.h>
#include <errno.h>
#include <grp.h>
#include <pwd.h>
static char *dirpath = "/home/anggar/shift4";
char key[97] = "qE1~ YMUR2\"`hNIdPzi%^t@(Ao:=CQ,nx4S[7mHFye#aT6+v)DfKL$r?bkOGB>}!9_wV']jcp5JZ&Xl|\\8s;g<{3.u*W-0";
regex_t regex;
int reti;
void enc(char * input)
{
if(!strcmp(input,".") || !strcmp(input,"..")) return;
for(int i=0;i<strlen(input);i++)
{
for(int j=0;j<94;j++){
if(input[i]==key[j]){
input[i] = key[(j+17)%94];
break;
}
}
}
}
void dec(char * input)
{
if(!strcmp(input,".") || !strcmp(input,"..")) return;
for(int i=0;i<strlen(input);i++)
{
for(int j=0;j<94;j++){
if(input[i]==key[j]){
input[i] = key[(j+77)%94];
break;
}
}
}
}
static void* pre_init(struct fuse_conn_info *conn){
// Create Videos folder in main
char folder[40] = "/Videos";
enc(folder);
char fpath[1000];
sprintf(fpath,"%s%s", dirpath, folder);
mkdir(fpath, 0775);
(void) conn;
return NULL;
}
static const int xmp_getattr(const char *ppath, struct stat *stbuf)
{
int res;
char fpath[1000];
char path[1000];
strcpy(path, ppath);
enc(path);
if(strcmp(path,"/") == 0) sprintf(fpath,"%s",path);
else sprintf(fpath, "%s%s",dirpath, path);
res = lstat(fpath, stbuf);
// menangani waktu touch
if (res == -1){
dec(path);
if(!strncmp(path, "/YOUTUBER", 9)){
dec(fpath);
strcat(fpath, ".iz1");
enc(fpath);
if(lstat(fpath, stbuf) != -1){
return 0;
}
}
return -errno;
}
return 0;
}
static int xmp_readdir(const char *ppath, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
DIR *dp;
struct dirent *de;
(void) offset;
(void) fi;
char fpath[1000];
char path[1000];
strcpy(path, ppath);
enc(path);
if(strcmp(path,"/") == 0)
{
strcpy(path,dirpath);;
sprintf(fpath,"%s",path);
}
else sprintf(fpath, "%s%s",dirpath,path);
dp = opendir(fpath);
if (dp == NULL)
return -errno;
while ((de = readdir(dp)) != NULL) {
struct stat st;
char yeoreum[1000];
char meureum[1000];
memset(&st, 0, sizeof(st));
st.st_ino = de->d_ino;
st.st_mode = de->d_type << 12;
strcpy(yeoreum,de->d_name);
// untuk filepath aslinya
sprintf(meureum, "%s/%s", fpath, yeoreum);
dec(yeoreum);
// dapatkan user berkas aslinya
struct stat tmstat;
stat(meureum, &tmstat);
struct passwd *ireum = getpwuid(tmstat.st_uid);
struct group *gruppe = getgrgid(tmstat.st_gid);
int ireumDiffA = strcmp(ireum->pw_name, "chipset");
int ireumDiffB = strcmp(ireum->pw_name, "ic_controller");
int gruppeDiff = strcmp(gruppe->gr_name, "rusak");
FILE* fp;
char miris[1000], timestr[1000], writebuf[1000], miramar[1000];
// printf(":: %d %d %s", ireum->pw_uid, ireumDiffA, ireum->pw_name);
if((!ireumDiffA || !ireumDiffB) &&
!gruppeDiff &&
!(tmstat.st_mode & 0444)) { // Gak bisa dibaca
strcpy(miris, dirpath);
strcpy(miramar, "/filemiris.txt"); // diroot
enc(miramar);
strcat(miris, miramar);
fp = fopen(miris, "a+"); // TODO: ganti a aja
// Untuk dapat tanggal
// need improvement for atime
strftime(timestr, 40, "%y%m%d (%H:%M:%S)", localtime(&tmstat.st_atime));
fprintf(fp, "%s\t%d:%d\t%s\t%s\n", timestr, ireum->pw_uid, gruppe->gr_gid, path, yeoreum);
remove(meureum);
fclose(fp);
} else {
if (filler(buf, yeoreum, &st, 0))
break;
}
}
closedir(dp);
return 0;
}
static int xmp_flush(const char *path, struct fuse_file_info *fi)
{
int res;
(void) path;
/* This is called from every close on an open file, so call the
close on the underlying filesystem. But since flush may be
called multiple times for an open file, this must not really
close the file. This is important if used on a network
filesystem like NFS which flush the data/metadata on close() */
res = close(dup(fi->fh));
if (res == -1)
return -errno;
return 0;
}
static int xmp_mknod(const char *ppath, mode_t mode, dev_t rdev)
{
int res;
char path[1000];
char fpath[1000];
strcpy(path, ppath);
enc(path);
if(strcmp(path,"/") == 0)
{
strcpy(path,dirpath);;
sprintf(fpath,"%s",path);
}
else sprintf(fpath, "%s%s",dirpath,path);
res = mknod(fpath, mode, rdev);
if (res == -1)
return -errno;
return 0;
}
// Asumsi untuk folder YOUTUBER ada di root path
static int xmp_mkdir(const char *ppath, mode_t mode)
{
int res;
char fpath[1000];
char path[1000];
strcpy(path, ppath);
// buat folder youtuber
mode_t cmode = mode;
if(!strncmp(path,"/YOUTUBER", 9)){
printf("UYEEEE .. ");
cmode = 0750;
}
enc(path);
if(strcmp(path,"/") == 0)
{
strcpy(path,dirpath);;
sprintf(fpath,"%s",path);
}
else sprintf(fpath, "%s%s",dirpath,path);
res = mkdir(fpath, cmode);
if (res == -1)
return -errno;
return 0;
}
static int xmp_unlink(const char *ppath)
{
int res;
char fpath[1000];
char path[1000];
strcpy(path, ppath);
enc(path);
if(strcmp(path,"/") == 0)
{
strcpy(path,dirpath);;
sprintf(fpath,"%s",path);
}
else sprintf(fpath, "%s%s",dirpath,path);
res = unlink(fpath);
if (res == -1)
return -errno;
return 0;
}
static int xmp_rmdir(const char *ppath)
{
int res;
char fpath[1000];
char path[1000];
strcpy(path, ppath);
enc(path);
if(strcmp(path,"/") == 0)
{
strcpy(path,dirpath);;
sprintf(fpath,"%s",path);
}
else sprintf(fpath, "%s%s",dirpath,path);
res = rmdir(fpath);
if (res == -1)
return -errno;
return 0;
}
static int xmp_rename(char *from, char *to)
{
int res;
char new_from[1000];
char new_to[1000];
enc(from);
enc(to);
sprintf(new_from,"%s%s",dirpath,from);
sprintf(new_to,"%s%s",dirpath,to);
res = rename(new_from, new_to);
if (res == -1)
return -errno;
return 0;
}
// IZ*ONE
static int xmp_chmod(const char *ppath, mode_t mode)
{
int res;
char fpath[1000];
char path[1000];
strcpy(path, ppath);
int pathDiff = strncmp(path, "/YOUTUBER", 9);
reti = regcomp(®ex, ".*\\.iz1$", 0);
reti = regexec(®ex, path, 0, NULL, 0);
enc(path);
if(strcmp(path,"/") == 0)
{
strcpy(path,dirpath);;
sprintf(fpath,"%s",path);
}
else sprintf(fpath, "%s%s",dirpath,path);
struct stat stbuf;
stat(fpath, &stbuf);
if(!reti && S_ISREG(stbuf.st_mode)) { // MATCH
perror("File ekstensi iz1 tidak boleh diubah permissionnya");
return -errno;
}
res = chmod(fpath, mode);
if (res == -1)
return -errno;
return 0;
}
static int xmp_chown(const char *ppath, uid_t uid, gid_t gid)
{
int res;
char fpath[1000];
char path[1000];
strcpy(path, ppath);
enc(path);
if(strcmp(path,"/") == 0)
{
strcpy(path,dirpath);;
sprintf(fpath,"%s",path);
}
else sprintf(fpath, "%s%s",dirpath,path);
res = lchown(fpath, uid, gid);
if (res == -1)
return -errno;
return 0;
}
static int xmp_truncate(const char *ppath, off_t size)
{
int res;
char fpath[1000];
char path[1000];
strcpy(path, ppath);
enc(path);
if(strcmp(path,"/") == 0)
{
strcpy(path,dirpath);;
sprintf(fpath,"%s",path);
}
else sprintf(fpath, "%s%s",dirpath,path);
res = truncate(fpath, size);
if (res == -1)
return -errno;
return 0;
}
static int xmp_utimens(const char *ppath, const struct timespec ts[2])
{
int res;
char fpath[1000];
char path[1000];
strcpy(path, ppath);
enc(path);
if(strcmp(path,"/") == 0)
{
strcpy(path,dirpath);;
sprintf(fpath,"%s",path);
}
else sprintf(fpath, "%s%s",dirpath,path);
/* don't use utime/utimes since they follow symlinks */
res = utimensat(0, fpath, ts, AT_SYMLINK_NOFOLLOW);
if (res == -1){
// menangani waktu touch
dec(path);
if(!strncmp(path, "/YOUTUBER", 9)){
dec(fpath);
strcat(fpath, ".iz1");
enc(fpath);
if(utimensat(0, fpath, ts, AT_SYMLINK_NOFOLLOW)!= -1){
return 0;
}
}
return -errno;
}
return 0;
}
static int xmp_open(const char *ppath, struct fuse_file_info *fi)
{
int res;
char fpath[1000];
char path[1000];
strcpy(path, ppath);
enc(path);
if(strcmp(path,"/") == 0)
{
strcpy(path,dirpath);;
sprintf(fpath,"%s",path);
}
else sprintf(fpath, "%s%s",dirpath,path);
res = open(fpath, fi->flags);
if (res == -1)
return -errno;
close(res);
return 0;
}
static int xmp_read(const char *ppath, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
int fd;
int res;
char fpath[1000];
char path[1000];
strcpy(path, ppath);
enc(path);
if(strcmp(path,"/") == 0)
{
strcpy(path,dirpath);;
sprintf(fpath,"%s",path);
}
else sprintf(fpath, "%s%s",dirpath,path);
(void) fi;
fd = open(fpath, O_RDONLY);
if (fd == -1)
return -errno;
res = pread(fd, buf, size, offset);
if (res == -1)
res = -errno;
close(fd);
return res;
}
static int xmp_write(const char *ppath, char *buf, size_t size,
off_t offset, struct fuse_file_info *fi)
{
int fd;
int res;
char fpath[1000];
char path[1000];
strcpy(path, ppath);
enc(path);
if(strcmp(path,"/") == 0)
{
strcpy(path,dirpath);;
sprintf(fpath,"%s",path);
}
else sprintf(fpath, "%s%s",dirpath,path);
(void) fi;
fd = open(fpath, O_WRONLY);
if (fd == -1)
return -errno;
res = pwrite(fd, buf, size, offset);
if (res == -1)
res = -errno;
close(fd);
return res;
}
static int xmp_create(const char *ppath, mode_t mode, struct fuse_file_info *fi)
{
int fd;
int res;
char fpath[1000];
char path[1000];
char bpath[1000];
char strtime[1000];
char zipname[1000];
char bkpath[1000];
mode_t cmode = mode;
strcpy(path, ppath);
// enc(path);
// folder youtuber
if(!strncmp(path, "/YOUTUBER", 9)){
strcat(path, ".iz1");
cmode = 0640;
}
// Ketika akan membuat file swap
// reti = regcomp(®ex, ".*\\.swp$", 0);
// reti = regexec(®ex, path, 0, NULL, 0);
strcpy(bpath, path);
char spp* = strstr(bpath, ".swp");
if(spp) {
char bkF = "/Backup";
char rbF = "/RecycleBin";
strcpy(spp, "");
time_t n = time(NULL);
strftime(strtime, 40, "_%Y-%m-%d_%H:%M:%S", localtime(&now));
sprintf(bkpath, "%s/Backup/%s%s", dirpath, bpath, strtime);
// Jika dihapus
// sprintf(zipname, "%s/RecycleBin/%s_deleted_%s.zip", dirpath, bpath, strtime);
pid_t erzi1, erzi2;
erzi1 = fork();
if(erzi1 == 0) {
enc(path);
sprintf(fpath, "%s%s", dirpath, path);
execl("/bin/cp", "-p", , bkpath)
}
}
enc(path);
sprintf(fpath, "%s%s", dirpath, path);
res = creat(fpath, cmode);
// fd = open(path, fi->flags, mode);
// if (fd == -1)
// return -errno;
// fi->fh = fd;
// Waktu dia akan create file
// retina = regcomp(&xeger, ".*([^/]*\\.swp)$");
// size_t maxGroups = 3;
// regmatch_t groupArrays[maxGroups];
// dec(fpath);
// retina = regexec(&xeger, fpath, maxGroups, groupArrays, 0);
// if(!reti){ // MATCH
// }
if (res == -1) return -errno;
close(res);
return 0;
}
static struct fuse_operations xmp_oper = {
.init = pre_init,
.getattr = xmp_getattr,
.readdir = xmp_readdir,
.mknod = xmp_mknod,
.mkdir = xmp_mkdir,
.unlink = xmp_unlink,
.rmdir = xmp_rmdir,
.rename = xmp_rename,
.chmod = xmp_chmod,
.chown = xmp_chown,
.truncate = xmp_truncate,
.utimens = xmp_utimens,
.open = xmp_open,
.read = xmp_read,
.write = xmp_write,
.flush = xmp_flush,
.create = xmp_create
};
int main(int argc, char *argv[])
{
umask(0);
return fuse_main(argc, argv, &xmp_oper, NULL);
}