forked from wenchy/tinyFTP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.cpp
808 lines (691 loc) · 16 KB
/
common.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
#include "common.h"
#include "error.h"
void
Fclose(FILE *fp)
{
if (fclose(fp) != 0)
Error::sys("fclose error");
}
void
Fclose(FILE **fp)
{
if (fclose(*fp) != 0)
Error::sys("fclose error");
*fp = NULL;
}
FILE *
Fdopen(int fd, const char *type)
{
FILE *fp;
if ( (fp = fdopen(fd, type)) == NULL)
Error::sys("fdopen error");
return(fp);
}
char *
Fgets(char *ptr, int n, FILE *stream)
{
char *rptr;
if ( (rptr = fgets(ptr, n, stream)) == NULL && ferror(stream))
Error::sys("fgets error");
return (rptr);
}
FILE *
Fopen(const char *filename, const char *mode)
{
FILE *fp;
if ( (fp = fopen(filename, mode)) == NULL)
Error::sys("fopen error");
return(fp);
}
void
Fputs(const char *ptr, FILE *stream)
{
if (fputs(ptr, stream) == EOF)
Error::sys("fputs error");
}
void *
Malloc(size_t size)
{
void *ptr;
if ( (ptr = malloc(size)) == NULL)
Error::sys("malloc error");
memset(ptr, 0, size);
return(ptr);
}
void
Pthread_create(pthread_t *tid, const pthread_attr_t *attr,
void * (*func)(void *), void *arg)
{
int n;
if ( (n = pthread_create(tid, attr, func, arg)) == 0)
return;
errno = n;
Error::sys("pthread_create error");
}
void
Pthread_join(pthread_t tid, void **status)
{
int n;
if ( (n = pthread_join(tid, status)) == 0)
return;
errno = n;
Error::sys("pthread_join error");
}
void
Pthread_detach(pthread_t tid)
{
int n;
if ( (n = pthread_detach(tid)) == 0)
return;
errno = n;
Error::sys("pthread_detach error");
}
void
Pthread_kill(pthread_t tid, int signo)
{
int n;
if ( (n = pthread_kill(tid, signo)) == 0)
return;
errno = n;
Error::sys("pthread_kill error");
}
void
Pthread_mutexattr_init(pthread_mutexattr_t *attr)
{
int n;
if ( (n = pthread_mutexattr_init(attr)) == 0)
return;
errno = n;
Error::sys("pthread_mutexattr_init error");
}
#ifdef _POSIX_THREAD_PROCESS_SHARED
void
Pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int flag)
{
int n;
if ( (n = pthread_mutexattr_setpshared(attr, flag)) == 0)
return;
errno = n;
Error::sys("pthread_mutexattr_setpshared error");
}
#endif
void
Pthread_mutex_init(pthread_mutex_t *mptr, pthread_mutexattr_t *attr)
{
int n;
if ( (n = pthread_mutex_init(mptr, attr)) == 0)
return;
errno = n;
Error::sys("pthread_mutex_init error");
}
/* include Pthread_mutex_lock */
void
Pthread_mutex_lock(pthread_mutex_t *mptr)
{
int n;
if ( (n = pthread_mutex_lock(mptr)) == 0)
return;
errno = n;
Error::sys("pthread_mutex_lock error");
}
/* end Pthread_mutex_lock */
void
Pthread_mutex_unlock(pthread_mutex_t *mptr)
{
int n;
if ( (n = pthread_mutex_unlock(mptr)) == 0)
return;
errno = n;
Error::sys("pthread_mutex_unlock error");
}
void
Pthread_cond_broadcast(pthread_cond_t *cptr)
{
int n;
if ( (n = pthread_cond_broadcast(cptr)) == 0)
return;
errno = n;
Error::sys("pthread_cond_broadcast error");
}
void
Pthread_cond_signal(pthread_cond_t *cptr)
{
int n;
if ( (n = pthread_cond_signal(cptr)) == 0)
return;
errno = n;
Error::sys("pthread_cond_signal error");
}
void
Pthread_cond_wait(pthread_cond_t *cptr, pthread_mutex_t *mptr)
{
int n;
if ( (n = pthread_cond_wait(cptr, mptr)) == 0)
return;
errno = n;
Error::sys("pthread_cond_wait error");
}
void
Pthread_cond_timedwait(pthread_cond_t *cptr, pthread_mutex_t *mptr,
const struct timespec *tsptr)
{
int n;
if ( (n = pthread_cond_timedwait(cptr, mptr, tsptr)) == 0)
return;
errno = n;
Error::sys("pthread_cond_timedwait error");
}
void
Pthread_once(pthread_once_t *ptr, void (*func)(void))
{
int n;
if ( (n = pthread_once(ptr, func)) == 0)
return;
errno = n;
Error::sys("pthread_once error");
}
void
Pthread_key_create(pthread_key_t *key, void (*func)(void *))
{
int n;
if ( (n = pthread_key_create(key, func)) == 0)
return;
errno = n;
Error::sys("pthread_key_create error");
}
void
Pthread_setspecific(pthread_key_t key, const void *value)
{
int n;
if ( (n = pthread_setspecific(key, value)) == 0)
return;
errno = n;
Error::sys("pthread_setspecific error");
}
int getFileNslice(const char *pathname, uint32_t *pnslice_o)
{
unsigned long filesize = 0, n = MAXNSLICE;
struct stat statbuff;
if(stat(pathname, &statbuff) < 0){
return -1; // error
} else {
if (statbuff.st_size == 0)
{
return 0; // file is empty.
} else {
filesize = statbuff.st_size;
}
}
if (filesize % SLICECAP == 0)
{
*pnslice_o = filesize/SLICECAP;
} else if ( (n = filesize/SLICECAP + 1) > MAXNSLICE ){
Error::msg("too large file size: %d\n (MAX: %d)", n, MAXNSLICE);
return -2;
} else {
*pnslice_o = filesize/SLICECAP + 1;
}
//printf("getFileNslice nslice: %d\n", *pnslice_o);
return 1;
}
string getFileSizeString(const char *pathname)
{
unsigned long filesize = 0;
unsigned long n = 0;
string hsize_o;
char buf[MAXLINE];
unsigned long kbase = 1024;
unsigned long mbase = 1024 * 1024;
unsigned long gbase = 1024 * 1024 * 1024;
struct stat statbuff;
if(stat(pathname, &statbuff) < 0){
hsize_o = "error";
return hsize_o; // error
} else {
if (statbuff.st_size == 0)
{
hsize_o = "0B"; // file is empty.
} else {
filesize = statbuff.st_size;
if (filesize / kbase == 0)
{
snprintf(buf, MAXLINE, "%lu", filesize);
hsize_o += buf;
hsize_o +="B";
} else if ( filesize / mbase == 0 ){
snprintf(buf, MAXLINE, "%lu", filesize / kbase);
hsize_o += buf;
n = (filesize % kbase)* 100 / kbase;
if (n != 0)
{
hsize_o += ".";
snprintf(buf, MAXLINE, "%02lu", n);
hsize_o += buf;
}
hsize_o +="K";
} else if ( filesize / gbase == 0 ){
snprintf(buf, MAXLINE, "%2lu", filesize / mbase);
hsize_o += buf;
n = (filesize % mbase)* 100 / mbase;
if (n != 0)
{
hsize_o += ".";
snprintf(buf, MAXLINE, "%02lu", n);
hsize_o += buf;
}
hsize_o +="M";
} else {
snprintf(buf, MAXLINE, "%lu", filesize / gbase);
hsize_o += buf;
n = (filesize % gbase) * 100 / gbase ;
//printf("filesize n: %lu\n", n);
if (n != 0)
{
hsize_o += ".";
snprintf(buf, MAXLINE, "%02lu", n);
hsize_o += buf;
}
hsize_o +="G";
}
}
}
return hsize_o;
}
string visualmd5sum(const char * pathname)
{
int n;
char buf[SLICECAP];
unsigned char out[MD5_DIGEST_LENGTH];
string md5str;
int oldProgress = 0, newProgress = 0;
MD5_CTX ctx;
uint32_t nslice = 0, sindex = 0;
string tipstr;
tipstr += "\033[32mMD5SUM\033[0m(";
tipstr += pathname;
tipstr += ")";
string hfilesize = getFileSizeString(pathname);
if ( (n = getFileNslice(pathname, &nslice)) < 0) {
Error::msg("getFileNslice error");
return md5str;
}
FILE *fp;
if ( (fp = fopen(pathname, "rb")) == NULL)
{
Error::ret("md5sum#fopen");
return md5str;
}
MD5_Init(&ctx);
while( (n = fread(buf, sizeof(char), SLICECAP, fp)) >0 )
{
MD5_Update(&ctx, buf, n);
if (nslice > (1024 * 512))
{
newProgress = (++sindex*1.0)/nslice*100;
if (newProgress > oldProgress)
{
//printf("\033[2K\r\033[0m");
fprintf(stderr, "\033[2K\r\033[0m%-40s%10s\t%3d%%", tipstr.c_str(), hfilesize.c_str(), newProgress);
}
oldProgress = newProgress;
}
}
if (nslice > (1024 * 512))
printf("\n");
MD5_Final(out, &ctx);
for(n = 0; n< MD5_DIGEST_LENGTH; n++)
{
snprintf(buf, SLICECAP, "%02x", out[n]);
md5str += buf;
}
return md5str;
}
string md5sum(const char * pathname)
{
int n;
char buf[SLICECAP];
unsigned char out[MD5_DIGEST_LENGTH];
string md5str;
MD5_CTX ctx;
FILE *fp;
if ( (fp = fopen(pathname, "rb")) == NULL)
{
Error::ret("md5sum#fopen");
return md5str;
}
MD5_Init(&ctx);
while( (n = fread(buf, sizeof(char), SLICECAP, fp)) >0 )
{
MD5_Update(&ctx, buf, n);
}
printf("\n");
MD5_Final(out, &ctx);
for(n = 0; n< MD5_DIGEST_LENGTH; n++)
{
snprintf(buf, SLICECAP, "%02x", out[n]);
md5str += buf;
}
return md5str;
}
string visualmd5sumNslice(const char * pathname, uint32_t nslice)
{
int n;
char buf[SLICECAP];
unsigned char out[MD5_DIGEST_LENGTH];
string md5str;
int oldProgress = 0, newProgress = 0;
MD5_CTX ctx;
uint32_t fileslice =0, sindex = 0;
if ( (n = getFileNslice(pathname, &fileslice)) < 0) {
Error::msg("getFileNslice error");
return md5str;
}
int percent = (nslice*1.0)/fileslice*100;
snprintf(buf, SLICECAP, "%u/%u %3d%%", nslice, fileslice, percent);
string tipstr;
tipstr += "\033[32mMD5SUM\033[0m(";
tipstr += pathname;
tipstr += " ";
tipstr += buf;
tipstr += ")";
string hfilesize = getFileSizeString(pathname);
FILE *fp;
if ( (fp = fopen(pathname, "rb")) == NULL)
{
Error::ret("md5sum#fopen");
return md5str;
}
MD5_Init(&ctx);
while( (n = fread(buf, sizeof(char), SLICECAP, fp)) >0 )
{
++sindex;
MD5_Update(&ctx, buf, n);
if (nslice > (1024 * 512))
{
newProgress = (sindex*1.0)/nslice*100;
if (newProgress > oldProgress)
{
fprintf(stderr, "\033[2K\r\033[0m%-40s%10s\t%3d%%", tipstr.c_str(), hfilesize.c_str(), newProgress);
}
oldProgress = newProgress;
}
if ( sindex == nslice)
{
break;
}
}
if (nslice > (1024 * 512))
printf("\n");
MD5_Final(out, &ctx);
for(n = 0; n< MD5_DIGEST_LENGTH; n++)
{
snprintf(buf, SLICECAP, "%02x", out[n]);
md5str += buf;
}
return md5str;
}
string md5sumNslice(const char * pathname, uint32_t nslice)
{
int n;
char buf[SLICECAP];
unsigned char out[MD5_DIGEST_LENGTH];
string md5str;
MD5_CTX ctx;
uint32_t sindex = 0;
FILE *fp;
if ( (fp = fopen(pathname, "rb")) == NULL)
{
Error::ret("md5sum#fopen");
return md5str;
}
MD5_Init(&ctx);
while( (n = fread(buf, sizeof(char), SLICECAP, fp)) >0 )
{
MD5_Update(&ctx, buf, n);
if ((++sindex) == nslice)
{
break;
}
}
MD5_Final(out, &ctx);
for(n = 0; n< MD5_DIGEST_LENGTH; n++)
{
snprintf(buf, SLICECAP, "%02x", out[n]);
md5str += buf;
}
return md5str;
}
string md5sum(const char * str, int len)
{
int n;
MD5_CTX ctx;
char buf[SLICECAP];
unsigned char out[MD5_DIGEST_LENGTH];
string md5str;
MD5_Init(&ctx);
MD5_Update(&ctx, str, len);
MD5_Final(out, &ctx);
for(n = 0; n< MD5_DIGEST_LENGTH; n++)
{
snprintf(buf, SLICECAP, "%02x", out[n]);
md5str += buf;
}
return md5str;
}
unsigned long long getFilesize(const char * pathname)
{
struct stat statbuff;
if(stat(pathname, &statbuff) < 0)
{
Error::ret("getFilesize#stat");
return 0;
} else
{
return (unsigned long long)statbuff.st_size;
}
}
string getFilesize(string pathname)
{
struct stat statbuff;
char buf[MAXLINE];
string sizestr;
if(stat(pathname.c_str(), &statbuff) < 0)
{
Error::ret("getFilesize#stat");
return sizestr;
} else
{
snprintf(buf, MAXLINE, "%llu", (unsigned long long)statbuff.st_size);
sizestr = buf;
return sizestr;
}
}
string size2str(unsigned long filesize)
{
unsigned long n = 0;
string hsize_o;
char buf[MAXLINE];
unsigned long kbase = 1024;
unsigned long mbase = 1024 * 1024;
unsigned long gbase = 1024 * 1024 * 1024;
if (filesize == 0)
{
hsize_o = "0B"; // file is empty.
}
else
{
if (filesize / kbase == 0)
{
snprintf(buf, MAXLINE, "%lu", filesize);
hsize_o += buf;
hsize_o +="B";
} else if ( filesize / mbase == 0 ){
snprintf(buf, MAXLINE, "%lu", filesize / kbase);
hsize_o += buf;
n = (filesize % kbase)* 100 / kbase;
if (n != 0)
{
hsize_o += ".";
snprintf(buf, MAXLINE, "%02lu", n);
hsize_o += buf;
}
hsize_o +="K";
} else if ( filesize / gbase == 0 ){
snprintf(buf, MAXLINE, "%2lu", filesize / mbase);
hsize_o += buf;
n = (filesize % mbase)* 100 / mbase;
if (n != 0)
{
hsize_o += ".";
snprintf(buf, MAXLINE, "%02lu", n);
hsize_o += buf;
}
hsize_o +="M";
} else {
snprintf(buf, MAXLINE, "%lu", filesize / gbase);
hsize_o += buf;
n = (filesize % gbase) * 100 / gbase ;
//printf("filesize n: %lu\n", n);
if (n != 0)
{
hsize_o += ".";
snprintf(buf, MAXLINE, "%02lu", n);
hsize_o += buf;
}
hsize_o +="G";
}
}
return hsize_o;
}
// string getFileSizeString(const char *pathname)
// {
// unsigned long filesize = 0;
// unsigned long n = 0;
// string hsize_o;
// char buf[MAXLINE];
// unsigned long kbase = 1024;
// unsigned long mbase = 1024 * 1024;
// unsigned long gbase = 1024 * 1024 * 1024;
// struct stat statbuff;
// if(stat(pathname, &statbuff) < 0){
// hsize_o = "error";
// return hsize_o; // error
// } else {
// if (statbuff.st_size == 0)
// {
// hsize_o = "0B"; // file is empty.
// } else {
// filesize = statbuff.st_size;
// if (filesize / kbase == 0)
// {
// snprintf(buf, MAXLINE, "%lu", filesize);
// hsize_o += buf;
// hsize_o +="B";
// } else if ( filesize / mbase == 0 ){
// snprintf(buf, MAXLINE, "%lu", filesize / kbase);
// hsize_o += buf;
// n = (filesize % kbase)* 100 / kbase;
// if (n != 0)
// {
// hsize_o += ".";
// snprintf(buf, MAXLINE, "%02lu", n);
// hsize_o += buf;
// }
// hsize_o +="K";
// } else if ( filesize / gbase == 0 ){
// snprintf(buf, MAXLINE, "%2lu", filesize / mbase);
// hsize_o += buf;
// n = (filesize % mbase)* 100 / mbase;
// if (n != 0)
// {
// hsize_o += ".";
// snprintf(buf, MAXLINE, "%02lu", n);
// hsize_o += buf;
// }
// hsize_o +="M";
// } else {
// snprintf(buf, MAXLINE, "%lu", filesize / gbase);
// hsize_o += buf;
// n = (filesize % gbase) * 100 / gbase ;
// //printf("filesize n: %lu\n", n);
// if (n != 0)
// {
// hsize_o += ".";
// snprintf(buf, MAXLINE, "%02lu", n);
// hsize_o += buf;
// }
// hsize_o +="G";
// }
// }
// }
// return hsize_o;
// }
string encryptPassword(string password)
{
string saltedPass = PASSSALT0 + password + PASSSALT1;
//cout << "***********saltedPass: " << saltedPass << endl;
saltedPass = md5sum(saltedPass.c_str(), saltedPass.size());
//cout << "***********saltedPass: " << saltedPass << endl;
return saltedPass;
}
string getCurrentTime()
{
char buf[MAXLINE];
time_t ticks;
struct tm *p;
time(&ticks);
p=localtime(&ticks);
snprintf(buf, MAXLINE, "%04d%02d%02d%02d%02d%02d", (1900 + p->tm_year), (1+p->tm_mon), p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
return string(buf);
}
unsigned long long getDiskAvailable()
{
struct statfs diskInfo;
statfs(ROOTDIR, &diskInfo);
unsigned long long blocksize = diskInfo.f_bsize;
unsigned long long availableDisk;
/*unsigned long long totalsize = blocksize * diskInfo.f_blocks; // Total_size
printf("Total_size = %llu B = %llu KB = %llu MB = %llu GB\n",
totalsize, totalsize>>10, totalsize>>20, totalsize>>30);
unsigned long long freeDisk = diskInfo.f_bfree * blocksize; // Disk_free
availableDisk = diskInfo.f_bavail * blocksize; // Disk_available
printf("Disk_free = %llu MB = %llu GB\nDisk_available = %llu MB = %llu GB\n",
freeDisk>>20, freeDisk>>30, availableDisk>>20, availableDisk>>30);*/
availableDisk = diskInfo.f_bavail * blocksize;
return availableDisk;
}
static struct termios oldt;
void restore_terminal_settings(void)
{
//Apply saved settings
tcsetattr(0, TCSANOW, &oldt);
}
//make terminal read 1 char at a time
void disable_terminal_return(void)
{
struct termios newt;
//save terminal settings
tcgetattr(0, &oldt);
//init new settings
newt = oldt;
//change settings
newt.c_lflag &= ~(ICANON | ECHO);
//apply settings
tcsetattr(0, TCSANOW, &newt);
//make sure settings will be restored when program ends
atexit(restore_terminal_settings);
}
string getInode(const char * pathname)
{
string inode;
struct stat statBuf;
char buf[MAXLINE];
if (stat(pathname, &statBuf) < 0)
{
Error::ret("\033[31mstat\033[0m");
} else {
snprintf(buf, MAXLINE, "%lu", statBuf.st_ino);
inode = buf;
}
return inode;
}