forked from hasse69/rar2fs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrar2fs.c
5382 lines (4900 loc) · 200 KB
/
rar2fs.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
/*
Copyright (C) 2009 Hans Beckerus (hans.beckerus#AT#gmail.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
This program take use of the freeware "Unrar C++ Library" (libunrar)
by Alexander Roshal and some extensions to it.
Unrar source may be used in any software to handle RAR archives
without limitations free of charge, but cannot be used to re-create
the RAR compression algorithm, which is proprietary. Distribution
of modified Unrar source in separate form or as a part of other
software is permitted, provided that it is clearly stated in
the documentation and source comments that the code may not be used
to develop a RAR (WinRAR) compatible archiver.
*/
#include <platform.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <sys/select.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <libgen.h>
#include <fuse.h>
#include <fcntl.h>
#include <getopt.h>
#include <syslog.h>
#include <wchar.h>
#ifdef HAVE_LOCALE_H
# include <locale.h>
#endif
#ifdef HAVE_MMAP
# include <sys/mman.h>
#endif
#include <limits.h>
#include <pthread.h>
#include <ctype.h>
#ifdef HAVE_SCHED_H
# include <sched.h>
#endif
#ifdef HAVE_SYS_XATTR_H
# include <sys/xattr.h>
#endif
#ifdef HAVE_SYS_SYSMACROS_H
# include <sys/sysmacros.h>
#endif
#ifdef HAVE_ICONV
#include <iconv.h>
#endif
#include <assert.h>
#include "version.h"
#include "debug.h"
#include "index.h"
#include "dllwrapper.h"
#include "filecache.h"
#include "iobuffer.h"
#include "optdb.h"
#include "sighandler.h"
#include "dirlist.h"
#define E_TO_MEM 0
#define E_TO_TMP 1
#define MOUNT_FOLDER 0
#define MOUNT_ARCHIVE 1
struct vol_handle {
FILE *fp;
off_t pos;
};
struct io_context {
FILE* fp;
off_t pos;
struct io_buf *buf;
pid_t pid;
unsigned int seq;
short vno;
short vno_max;
dir_elem_t *entry_p;
struct vol_handle *volHdl;
int pfd1[2];
int pfd2[2];
volatile int terminate;
pthread_t thread;
pthread_mutex_t mutex;
/* mmap() data */
void *mmap_addr;
FILE *mmap_fp;
int mmap_fd;
/* debug */
#ifdef DEBUG_READ
FILE *dbg_fp;
#endif
};
struct io_handle {
int type;
#define IO_TYPE_NRM 0
#define IO_TYPE_RAR 1
#define IO_TYPE_RAW 2
#define IO_TYPE_ISO 3
#define IO_TYPE_INFO 4
#define IO_TYPE_DIR 5
union {
struct io_context *context; /* type = IO_TYPE_RAR/IO_TYPE_RAW */
int fd; /* type = IO_TYPE_NRM/IO_TYPE_ISO */
DIR *dp; /* type = IO_TYPE_DIR */
void *buf_p; /* type = IO_TYPE_INFO */
uintptr_t bits;
} u;
dir_elem_t *entry_p; /* type = IO_TYPE_ISO */
char *path; /* type = IO_TYPE_DIR */
};
#define FH_ZERO(fh) ((fh) = 0)
#define FH_ISSET(fh) (fh)
#define FH_SETCONTEXT(fh, v) (FH_TOIO(fh)->u.context = (v))
#define FH_SETFD(fh, v) (FH_TOIO(fh)->u.fd = (v))
#define FH_SETDP(fh, v) (FH_TOIO(fh)->u.dp = (v))
#define FH_SETBUF(fh, v) (FH_TOIO(fh)->u.buf_p = (v))
#define FH_SETIO(fh, v) ((fh) = (uintptr_t)(v))
#define FH_SETENTRY(fh, v) (FH_TOIO(fh)->entry_p = (v))
#define FH_SETPATH(fh, v) (FH_TOIO(fh)->path = (v))
#define FH_SETTYPE(fh, v) (FH_TOIO(fh)->type = (v))
#define FH_TOCONTEXT(fh) (FH_TOIO(fh)->u.context)
#define FH_TOFD(fh) (FH_TOIO(fh)->u.fd)
#define FH_TODP(fh) (FH_TOIO(fh)->u.dp)
#define FH_TOBUF(fh) (FH_TOIO(fh)->u.buf_p)
#define FH_TOENTRY(fh) (FH_TOIO(fh)->entry_p)
#define FH_TOPATH(fh) (FH_TOIO(fh)->path)
#define FH_TOIO(fh) ((struct io_handle*)(uintptr_t)(fh))
/* Special DIR pointer for root folder in a file system loop. */
#define FS_LOOP_ROOT_DP ((DIR *)~0U)
#define WAIT_THREAD(pfd) \
do {\
int fd = pfd[0];\
int nfsd = fd+1;\
fd_set rd;\
FD_ZERO(&rd);\
FD_SET(fd, &rd);\
int retval = select(nfsd, &rd, NULL, NULL, NULL); \
if (retval == -1) {\
if (errno != EINTR) \
perror("select");\
} else if (retval) {\
/* FD_ISSET(0, &rfds) will be true. */\
char buf[2];\
NO_UNUSED_RESULT read(fd, buf, 1); /* consume byte */\
printd(4, "%lu thread wakeup (%d, %u)\n",\
(unsigned long)pthread_self(),\
retval,\
(int)buf[0]);\
} else {\
perror("select");\
}\
} while (0)
#define WAKE_THREAD(pfd, op) \
do {\
/* Wakeup the reader thread */ \
char buf[2]; \
buf[0] = (op); \
if (write((pfd)[1], buf, 1) != 1) \
perror("write"); \
} while (0)
/*
* This is to the handle the workaround for the destroyed file pointer
* position in some early libunrar5 versions when calling RARInitArchiveEx().
*/
#ifdef HAVE_FMEMOPEN
#define INIT_FP_ARG_(fp) (fp),0
#else
#define INIT_FP_ARG_(fp) (fp),1
#endif
#ifdef HAVE_ICONV
static iconv_t icd;
#endif
static long page_size_ = 0;
static int mount_type;
static struct dir_entry_list arch_list_root; /* internal list root */
static struct dir_entry_list *arch_list = &arch_list_root;
static pthread_attr_t thread_attr;
static unsigned int rar2_ticks;
static int fs_terminated = 0;
static int fs_loop = 0;
static char *fs_loop_mp_root = NULL;
static char *fs_loop_mp_base = NULL;
static size_t fs_loop_mp_base_len = 0;
static struct stat fs_loop_mp_stat;
static int64_t blkdev_size = -1;
static mode_t umask_ = 0022;
#define P_ALIGN_(a) (((a)+page_size_)&~(page_size_-1))
#ifdef __linux
#define IS_BLKDEV() (blkdev_size >= 0)
#else
#define IS_BLKDEV() (0)
#endif
#define BLKDEV_SIZE() (blkdev_size > 0 ? blkdev_size : 0)
static int extract_rar(char *arch, const char *file, FILE *fp, void *arg);
struct eof_cb_arg {
off_t toff;
off_t coff;
size_t size;
int fd;
char *arch;
};
struct extract_cb_arg {
char *arch;
void *arg;
};
static int extract_index(const dir_elem_t *entry_p, off_t offset);
static int preload_index(struct io_buf *buf, const char *path);
#if RARVER_MAJOR > 4
static const char *file_cmd[] = {
"#info",
NULL
};
#endif
/*!
*****************************************************************************
*
****************************************************************************/
static inline int is_rxx_vol(const char *name)
{
size_t len = strlen(name);
if (name[len - 4] == '.' &&
(name[len - 3] >= 'r' || name[len - 3] >= 'R') &&
isdigit(name[len - 2]) && isdigit(name[len - 1])) {
/* This seems to be a classic .rNN rar volume file.
* Let the rar header be the final judge. */
return 1;
}
return 0;
}
/*!
*****************************************************************************
*
****************************************************************************/
static inline int is_nnn_vol(const char *name)
{
size_t len = strlen(name);
if (name[len - 4] == '.' &&
isdigit(name[len - 3]) && isdigit(name[len - 2]) &&
isdigit(name[len - 1])) {
/* This seems to be a .NNN rar volume file.
* Let the rar header be the final judge. */
return 1;
}
return 0;
}
#define IS_ISO(s) (!strcasecmp((s)+(strlen(s)-4), ".iso"))
#define IS_AVI(s) (!strcasecmp((s)+(strlen(s)-4), ".avi"))
#define IS_MKV(s) (!strcasecmp((s)+(strlen(s)-4), ".mkv"))
#define IS_RAR(s) (!strcasecmp((s)+(strlen(s)-4), ".rar"))
#define IS_CBR(s) (!OPT_SET(OPT_KEY_NO_EXPAND_CBR) && \
!strcasecmp((s)+(strlen(s)-4), ".cbr"))
#define IS_RXX(s) (is_rxx_vol(s))
#define IS_NNN(s) (is_nnn_vol(s))
#define VTYPE(f, flags) \
(IS_RAR(f) || IS_RXX(f)) ? (flags & MHD_NEWNUMBERING) ? 1 : 0 : 1
/*!
*****************************************************************************
*
****************************************************************************/
#if RARVER_MAJOR > 4 || ( RARVER_MAJOR == 4 && RARVER_MINOR >= 20 )
static wchar_t *get_password(const char *file, wchar_t *buf, size_t len)
#else
static char *get_password(const char *file, char *buf, size_t len)
#endif
{
if (file) {
size_t l = strlen(file);
char *F = alloca(l + 2); /* might try . below */
strcpy(F, file);
strcpy(F + (l - 4), ".pwd");
FILE *fp = fopen(F, "r");
if (!fp) {
char *tmp1 = strdup(file);
strcpy(F, dirname(tmp1));
free(tmp1);
strcat(F, "/.");
tmp1 = strdup(file);
strcat(F, basename(tmp1));
free(tmp1);
strcpy(F + (l - 3), ".pwd");
fp = fopen(F, "r");
}
if (fp) {
#if RARVER_MAJOR > 4 || ( RARVER_MAJOR == 4 && RARVER_MINOR >= 20 )
buf = fgetws(buf, len, fp);
if (buf) {
wchar_t *eol = wcspbrk(buf, L"\r\n");
if (eol != NULL)
*eol = 0;
}
#else
buf = fgets(buf, len, fp);
#endif
fclose(fp);
return buf;
}
}
return NULL;
}
/*!
*****************************************************************************
*
****************************************************************************/
size_t wide_to_utf8(const wchar_t *src, char *dst, size_t dst_size)
{
--dst_size;
size_t in_size = dst_size;
ssize_t out_size = dst_size;
while (*src !=0 && --out_size >= 0) {
unsigned int c = *(src++);
if (c < 0x80) {
*(dst++) = c;
continue;
}
if (c < 0x800 && --out_size >= 0) {
*(dst++) =(0xc0 | (c >> 6));
*(dst++) =(0x80 | (c & 0x3f));
continue;
}
/* Check for surrogate pair */
if (c >= 0xd800 && c <= 0xdbff &&
*src >= 0xdc00 && *src <= 0xdfff) {
c = ((c - 0xd800) << 10) + (*src - 0xdc00) + 0x10000;
src++;
}
if (c < 0x10000 && (out_size -= 2) >= 0) {
*(dst++) = (0xe0 | (c >> 12));
*(dst++) = (0x80 | ((c >> 6) & 0x3f));
*(dst++) = (0x80 | (c & 0x3f));
} else if (c < 0x200000 && (out_size -= 3) >= 0) {
*(dst++) = (0xf0 | (c >> 18));
*(dst++) = (0x80 | ((c >> 12) & 0x3f));
*(dst++) = (0x80 | ((c >> 6) & 0x3f));
*(dst++) = (0x80 | (c & 0x3f));
}
}
*dst = 0;
return in_size - out_size;
}
/*!
*****************************************************************************
*
****************************************************************************/
static inline size_t
wide_to_char(char *dst, const wchar_t *src, size_t size)
{
dst[0] = '\0';
#ifdef HAVE_ICONV
if (icd == (iconv_t)-1)
goto fallback;
size_t in_bytes = (wcslen(src) + 1) * sizeof(wchar_t);
size_t out_bytes = size;
char *in_buf = (char *)src;
char *out_buf = dst;
if (iconv(icd, (ICONV_CONST char **)&in_buf, &in_bytes,
&out_buf, &out_bytes) == (size_t)-1) {
if (errno == E2BIG) {
/* Make sure the buffer is terminated properly but
* otherwise keep the truncated result. */
dst[size] = '\0';
return size;
} else {
*out_buf = '\0';
perror("iconv");
}
} else {
return size - out_bytes;
}
return -1;
fallback:
#endif
if (*src) {
#ifdef HAVE_WCSTOMBS
#if 0
size_t n = wcstombs(NULL, src, 0);
if (n != (size_t)-1) {
if (size >= (n + 1))
return wcstombs(dst, src, size);
}
#endif
#endif
/* Translation failed! Use fallback to strict UTF-8. */
return wide_to_utf8(src, dst, size);
}
return -1;
}
/*!
*****************************************************************************
*
****************************************************************************/
static dir_elem_t *path_lookup_miss(const char *path, struct stat *stbuf)
{
struct stat st;
char *root;
printd(3, "MISS %s\n", path);
if (fs_loop) {
if (!strcmp(path, fs_loop_mp_root)) {
memcpy(stbuf, &fs_loop_mp_stat, sizeof(struct stat));
return LOCAL_FS_ENTRY;
} else {
if (!strncmp(path, fs_loop_mp_base, fs_loop_mp_base_len))
return LOOP_FS_ENTRY;
}
}
ABS_ROOT(root, path);
/* Check if the missing file can be found on the local fs */
if (!lstat(root, stbuf ? stbuf : &st)) {
printd(3, "STAT retrieved for %s\n", root);
return LOCAL_FS_ENTRY;
}
/* Check if the missing file is a fake .iso */
if (OPT_SET(OPT_KEY_FAKE_ISO) && IS_ISO(root)) {
dir_elem_t *e_p;
int i;
int obj = OPT_CNT(OPT_KEY_FAKE_ISO)
? OPT_KEY_FAKE_ISO
: OPT_KEY_IMG_TYPE;
/* Try the image file extensions one by one */
for (i = 0; i < OPT_CNT(obj); i++) {
char *tmp = (OPT_STR(obj, i));
int l = strlen(tmp ? tmp : "");
char *root1 = strdup(root);
if (l > 4)
root1 = realloc(root1, strlen(root1) + 1 + (l - 4));
strcpy(root1 + (strlen(root1) - 4), tmp ? tmp : "");
if (lstat(root1, &st)) {
free(root1);
continue;
}
e_p = filecache_alloc(path);
e_p->name_p = strdup(path);
e_p->file_p = strdup(path);
e_p->flags.fake_iso = 1;
if (l > 4) {
e_p->file_p = realloc(
e_p->file_p,
strlen(path) + 1 + (l - 4));
}
/* back-patch *real* file name */
strncpy(e_p->file_p + (strlen(e_p->file_p) - 4),
tmp ? tmp : "", l);
*(e_p->file_p+(strlen(path)-4+l)) = 0;
memcpy(&e_p->stat, &st, sizeof(struct stat));
if (stbuf)
memcpy(stbuf, &st, sizeof(struct stat));
free(root1);
return e_p;
}
}
return NULL;
}
/*!
*****************************************************************************
*
****************************************************************************/
static dir_elem_t *path_lookup(const char *path, struct stat *stbuf)
{
dir_elem_t *e_p = filecache_get(path);
if (e_p && !e_p->flags.fake_iso) {
if (stbuf)
memcpy(stbuf, &e_p->stat, sizeof(struct stat));
return e_p;
}
/* Do not remember fake .ISO entries between eg. getattr() calls */
if (e_p && e_p->flags.fake_iso)
filecache_invalidate(path);
return path_lookup_miss(path, stbuf);
}
/*!
*****************************************************************************
*
****************************************************************************/
static void *extract_to(const char *file, off_t sz, const dir_elem_t *entry_p,
int oper)
{
ENTER_("%s", file);
int out_pipe[2] = {-1, -1};
if (pipe(out_pipe) != 0) /* make a pipe */
return MAP_FAILED;
printd(3, "Extracting %" PRIu64 "bytes resident in %s\n", sz, entry_p->rar_p);
pid_t pid = fork();
if (pid == 0) {
int ret;
close(out_pipe[0]);
ret = extract_rar(entry_p->rar_p, file, NULL,
(void *)(uintptr_t) out_pipe[1]);
close(out_pipe[1]);
_exit(ret);
} else if (pid < 0) {
close(out_pipe[0]);
close(out_pipe[1]);
/* The fork failed. Report failure. */
return MAP_FAILED;
}
close(out_pipe[1]);
FILE *tmp = NULL;
char *buffer = malloc(sz);
if (!buffer)
return MAP_FAILED;
if (oper == E_TO_TMP)
tmp = tmpfile();
off_t off = 0;
ssize_t n;
do {
/* read from pipe into buffer */
n = read(out_pipe[0], buffer + off, sz - off);
if (n == -1) {
if (errno == EINTR)
continue;
perror("read");
break;
}
off += n;
} while (n && off != sz);
/* Sync */
if (waitpid(pid, NULL, 0) == -1) {
/*
* POSIX.1-2001 specifies that if the disposition of
* SIGCHLD is set to SIG_IGN or the SA_NOCLDWAIT flag
* is set for SIGCHLD (see sigaction(2)), then
* children that terminate do not become zombies and
* a call to wait() or waitpid() will block until all
* children have terminated, and then fail with errno
* set to ECHILD.
*/
if (errno != ECHILD)
perror("waitpid");
}
/* Check for incomplete buffer error */
if (off != sz) {
free(buffer);
return MAP_FAILED;
}
printd(4, "Read %" PRIu64 "bytes from PIPE %d\n", off, out_pipe[0]);
close(out_pipe[0]);
if (tmp) {
if (!fwrite(buffer, sz, 1, tmp)) {
fclose(tmp);
tmp = MAP_FAILED;
} else {
fseeko(tmp, 0, SEEK_SET);
}
free(buffer);
return tmp;
}
return buffer;
}
/*!
*****************************************************************************
*
****************************************************************************/
static FILE *popen_(const dir_elem_t *entry_p, pid_t *cpid, void **mmap_addr,
FILE **mmap_fp, int *mmap_fd)
{
char *maddr = MAP_FAILED;
FILE *fp = NULL;
int fd = -1;
int pfd[2] = {-1,};
if (entry_p->flags.mmap) {
fd = open(entry_p->rar_p, O_RDONLY);
if (fd == -1) {
perror("open");
goto error;
}
if (entry_p->flags.mmap == 2) {
#ifdef HAVE_FMEMOPEN
maddr = extract_to(entry_p->file_p, entry_p->msize,
entry_p, E_TO_MEM);
if (maddr != MAP_FAILED) {
fp = fmemopen(maddr, entry_p->msize, "r");
if (fp == NULL) {
perror("fmemopen");
goto error;
}
}
#else
fp = extract_to(entry_p->file_p, entry_p->msize,
entry_p, E_TO_TMP);
if (fp == MAP_FAILED) {
printd(1, "Extract to tmpfile failed\n");
goto error;
}
#endif
} else {
#if defined ( HAVE_FMEMOPEN ) && defined ( HAVE_MMAP )
maddr = mmap(0, P_ALIGN_(entry_p->msize), PROT_READ,
MAP_SHARED, fd, 0);
if (maddr != MAP_FAILED) {
fp = fmemopen(maddr + entry_p->offset,
entry_p->msize -
entry_p->offset, "r");
if (fp == NULL) {
perror("fmemopen");
goto error;
}
} else {
perror("mmap");
goto error;
}
#else
fp = fopen(entry_p->rar_p, "r");
if (fp)
fseeko(fp, entry_p->offset, SEEK_SET);
else
goto error;
#endif
}
*mmap_addr = maddr;
*mmap_fp = fp;
*mmap_fd = fd;
}
pid_t pid;
if (pipe(pfd) == -1) {
perror("pipe");
goto error;
}
pid = fork();
if (pid == 0) {
int ret;
setpgid(getpid(), 0);
close(pfd[0]); /* Close unused read end */
ret = extract_rar(entry_p->rar_p,
entry_p->flags.mmap
? entry_p->file2_p
: entry_p->file_p,
fp, (void *)(uintptr_t) pfd[1]);
close(pfd[1]);
_exit(ret);
} else if (pid < 0) {
/* The fork failed. */
goto error;
}
/* This is the parent process. */
close(pfd[1]); /* Close unused write end */
*cpid = pid;
return fdopen(pfd[0], "r");
error:
if (maddr != MAP_FAILED) {
#ifdef HAVE_MMAP
if (entry_p->flags.mmap == 1)
munmap(maddr, P_ALIGN_(entry_p->msize));
else
#endif
free(maddr);
}
if (fp)
fclose(fp);
if (fd >= 0)
close(fd);
if (pfd[0] >= 0)
close(pfd[0]);
if (pfd[1] >= 0)
close(pfd[1]);
return NULL;
}
/*!
*****************************************************************************
*
****************************************************************************/
static int pclose_(FILE *fp, pid_t pid)
{
pid_t wpid;
int status = 0;
fclose(fp);
killpg(pid, SIGKILL);
/* Sync */
do {
wpid = waitpid(pid, &status, WNOHANG | WUNTRACED);
if (wpid == -1) {
/*
* POSIX.1-2001 specifies that if the disposition of
* SIGCHLD is set to SIG_IGN or the SA_NOCLDWAIT flag
* is set for SIGCHLD (see sigaction(2)), then
* children that terminate do not become zombies and
* a call to wait() or waitpid() will block until all
* children have terminated, and then fail with errno
* set to ECHILD.
*/
if (errno != ECHILD)
perror("waitpid");
return 0;
}
} while (!wpid || (!WIFEXITED(status) && !WIFSIGNALED(status)));
if (WIFEXITED(status))
return WEXITSTATUS(status);
return 0;
}
/* Size of file in first volume number in which it exists */
#define VOL_FIRST_SZ op->entry_p->vsize_first
/*
* Size of file in the following volume numbers (if situated in more than one).
* Compared to VOL_FIRST_SZ this is a qualified guess value only and it is
* not uncommon that it actually becomes equal to VOL_FIRST_SZ.
* For reference VOL_NEXT_SZ is basically the end of file data offset in
* first volume file reduced by the size of applicable headers and meta
* data. For the last volume file this value is more than likely bogus.
* This does not matter since the total file size is still reported
* correctly and anything trying to read it should stop once reaching EOF.
* The read function will infact verify that this is always the case and
* throw an error if trying to read beyond EOF. The important thing here
* is that the volumes files other than the first and last match this value.
*/
#define VOL_NEXT_SZ op->entry_p->vsize_next
/* Size of file data (including headers) in first volume number */
#define VOL_REAL_SZ(x) \
((x)?op->entry_p->vsize_real_next:op->entry_p->vsize_real_first)
/*!
*****************************************************************************
*
****************************************************************************/
static char *get_vname(int t, const char *str, int vol, int len, int pos)
{
ENTER_("%s vol=%d, len=%d, pos=%d", str, vol, len, pos);
char *s = strdup(str);
if (t) {
char f[16];
char f1[16];
sprintf(f1, "%%0%dd", len);
sprintf(f, f1, vol);
strncpy(&s[pos], f, len);
} else {
char f[16];
int lower = s[pos - 1] >= 'r';
if (vol == 0) {
sprintf(f, "%s", (lower ? "ar" : "AR"));
} else if (vol <= 100) {
sprintf(f, "%02d", (vol - 1));
} else { /* Possible, but unlikely */
sprintf(f, "%c%02d", (lower ? 'r' : 'R') + (vol - 1) / 100,
(vol - 1) % 100);
--pos;
++len;
}
strncpy(&s[pos], f, len);
}
return s;
}
/*!
*****************************************************************************
*
****************************************************************************/
static int get_vformat(const char *s, int t, int *l, int *p)
{
int len = 0;
int pos = 0;
int vol = 0;
const size_t SLEN = strlen(s);
if (t) {
const char *c = s + SLEN - 1;
while (!isdigit(*c) && c > s)
c--;
const char *num = c;
while (isdigit(*num) && num > s)
num--;
while (num > s && *num != '.') {
if (isdigit(*num)) {
char *dot = strchr(s, '.');
if (dot != NULL && dot < num)
c = num;
break;
}
num--;
}
while (c != s) {
if (isdigit(*c)) {
--c;
++len;
} else {
break;
}
}
if (c != s)
vol = strtoul(c + 1, NULL, 10);
else
vol = 0;
pos = c - s + 1;
} else {
int dot = 0;
len = SLEN - 1;
while (dot < 1 && len >= 0) {
if (s[len--] == '.')
++dot;
}
if (len >= 0) {
pos = len + 1;
len = SLEN - pos;
if (len == 4) {
pos += 2;
len -= 2;
if ((s[pos - 1] == 'r' || s[pos - 1] == 'R') &&
(s[pos ] == 'a' || s[pos ] == 'A') &&
(s[pos + 1] == 'r' || s[pos + 1] == 'R')) {
vol = 0;
} else {
int lower = s[pos - 1] >= 'r';
errno = 0;
vol = strtoul(&s[pos], NULL, 10) + 1 +
/* Possible, but unlikely */
(100 * (s[pos - 1] - (lower ? 'r' : 'R')));
vol = errno ? 0 : vol;
}
}
}
}
if (l) *l = len;
if (p) *p = pos;
return vol;
}
/*!
*****************************************************************************
*
****************************************************************************/
static void RARVolNameToFirstName_BUGGED(char *s, int vtype)
{
int len;
int pos;
RARVolNameToFirstName(s, vtype);
int n = get_vformat(s, !vtype, &len, &pos);
if (n == 1) {
char *s_copy = strdup(s);
while (--len >= 0)
s_copy[pos+len] = '0';
if (!access(s_copy, F_OK))
strcpy(s, s_copy);
free(s_copy);
}
}
#if _POSIX_TIMERS < 1
#define CLOCK_REALTIME 0
/*!
*****************************************************************************
*
****************************************************************************/
static int clock_gettime(int clk_id, struct timespec *ts)
{
struct timeval now;
(void)clk_id; /* not used */
int rv = gettimeofday(&now, NULL);
if (rv)
return rv;
ts->tv_sec = now.tv_sec;
ts->tv_nsec = now.tv_usec * 1000;
return 0;
}
#endif
/*!
*****************************************************************************
*
****************************************************************************/
static void update_atime(dir_elem_t *entry_p, struct timespec *tp_in)
{
dir_elem_t *e_p;
struct timespec tp[2];
int dir_updated = 0;
tp[0].tv_sec = tp_in->tv_sec;
tp[0].tv_nsec = tp_in->tv_nsec;
entry_p->stat.st_atime = tp[0].tv_sec;
#ifdef HAVE_STRUCT_STAT_ST_ATIM
entry_p->stat.st_atim.tv_sec = tp[0].tv_sec;
entry_p->stat.st_atim.tv_nsec = tp[0].tv_nsec;
#endif
char* tmp1 = strdup(entry_p->name_p);
e_p = filecache_get(dirname(tmp1));
free(tmp1);
if (e_p && S_ISDIR(e_p->stat.st_mode)) {
dir_updated = 1;
e_p->stat.st_atime = tp[0].tv_sec;
#ifdef HAVE_STRUCT_STAT_ST_ATIM
e_p->stat.st_atim.tv_sec = tp[0].tv_sec;
e_p->stat.st_atim.tv_nsec = tp[0].tv_nsec;
#endif
}
if (!dir_updated || OPT_SET(OPT_KEY_ATIME_RAR)) {
#if defined( HAVE_UTIMENSAT ) && defined( AT_SYMLINK_NOFOLLOW )
tp[1].tv_nsec = UTIME_OMIT;
tmp1 = strdup(entry_p->rar_p);
RARVolNameToFirstName_BUGGED(tmp1, !entry_p->vtype);
char *tmp2 = strdup(tmp1);
int res = utimensat(0, dirname(tmp2), tp, AT_SYMLINK_NOFOLLOW);
if (!res && OPT_SET(OPT_KEY_ATIME_RAR)) {
for (;;) {
res = utimensat(0, tmp1, tp, AT_SYMLINK_NOFOLLOW);
if (res == -1 && errno == ENOENT)
break;
RARNextVolumeName(tmp1, !entry_p->vtype);
}
}
free(tmp1);
free(tmp2);
#endif
}
}
/*!
*****************************************************************************
*
****************************************************************************/
static void check_atime(dir_elem_t *entry_p)
{
dir_elem_t *e_p;
struct timespec tp;
if (!OPT_SET(OPT_KEY_ATIME) && !OPT_SET(OPT_KEY_ATIME_RAR))
goto no_check_atime;
if (clock_gettime(CLOCK_REALTIME, &tp))