OSDN Git Service

* tests/lha-test.in (lha-test14): added to test the symbolic link file.
[lha/lha.git] / src / lhext.c
1 /* ------------------------------------------------------------------------ */
2 /* LHa for UNIX                                                                                                                         */
3 /*                              lhext.c -- LHarc extract                                                                        */
4 /*                                                                                                                                                      */
5 /*              Copyright (C) MCMLXXXIX Yooichi.Tagawa                                                          */
6 /*              Modified                        Nobutaka Watazaki                                                       */
7 /*                                                                                                                                                      */
8 /*  Ver. 0.00  Original                                                         1988.05.23  Y.Tagawa    */
9 /*  Ver. 1.00  Fixed                                                            1989.09.22  Y.Tagawa    */
10 /*  Ver. 0.03  LHa for UNIX                                                     1991.12.17  M.Oki               */
11 /*  Ver. 1.12  LHa for UNIX                                                     1993.10.01      N.Watazaki      */
12 /*  Ver. 1.13b Symbolic Link Update Bug Fix                     1994.06.21      N.Watazaki      */
13 /*      Ver. 1.14  Source All chagned                                   1995.01.14      N.Watazaki      */
14 /*  Ver. 1.14e bugfix                               1999.04.30  T.Okamoto   */
15 /* ------------------------------------------------------------------------ */
16 #include "lha.h"
17 /* ------------------------------------------------------------------------ */
18 static int      skip_flg = FALSE;       /* FALSE..No Skip , TRUE..Skip */
19 static char        *methods[] =
20 {
21         LZHUFF0_METHOD, LZHUFF1_METHOD, LZHUFF2_METHOD, LZHUFF3_METHOD,
22         LZHUFF4_METHOD, LZHUFF5_METHOD, LZHUFF6_METHOD, LZHUFF7_METHOD,
23         LARC_METHOD, LARC5_METHOD, LARC4_METHOD,
24         LZHDIRS_METHOD,
25         NULL
26 };
27
28 /* ------------------------------------------------------------------------ */
29 static          boolean
30 inquire_extract(name)
31         char           *name;
32 {
33         struct stat     stbuf;
34
35         skip_flg = FALSE;
36         if (stat(name, &stbuf) >= 0) {
37                 if (!is_regularfile(&stbuf)) {
38                         error("\"%s\" already exists (not a file)", name);
39                         return FALSE;
40                 }
41
42                 if (noexec) {
43                         printf("EXTRACT %s but file is exist.\n", name);
44                         return FALSE;
45                 }
46                 else if (!force) {
47                         if (!isatty(0))
48                                 return FALSE;
49
50                         switch (inquire("OverWrite ?(Yes/[No]/All/Skip)", name, "YyNnAaSs\n")) {
51                         case 0:
52                         case 1:/* Y/y */
53                                 break;
54                         case 2:
55                         case 3:/* N/n */
56                         case 8:/* Return */
57                                 return FALSE;
58                         case 4:
59                         case 5:/* A/a */
60                                 force = TRUE;
61                                 break;
62                         case 6:
63                         case 7:/* S/s */
64                                 skip_flg = TRUE;
65                                 break;
66                         }
67                 }
68         }
69         if (noexec)
70                 printf("EXTRACT %s\n", name);
71
72         return TRUE;
73 }
74
75 /* ------------------------------------------------------------------------ */
76 static          boolean
77 make_parent_path(name)
78         char           *name;
79 {
80         char            path[FILENAME_LENGTH];
81         struct stat     stbuf;
82         register char  *p;
83
84         /* make parent directory name into PATH for recursive call */
85         strcpy(path, name);
86         for (p = path + strlen(path); p > path; p--)
87                 if (p[-1] == '/') {
88                         *--p = '\0';
89                         break;
90                 }
91
92         if (p == path) {
93                 message("invalid path name \"%s\"", name);
94                 return FALSE;   /* no more parent. */
95         }
96
97         if (GETSTAT(path, &stbuf) >= 0) {
98                 if (is_directory(&stbuf))
99                         return TRUE;
100                 error("Not a directory \"%s\"", path);
101                 return FALSE;
102         }
103
104         if (verbose)
105                 message("Making directory \"%s\".", path);
106
107 #if defined __MINGW32__
108     if (mkdir(path) >= 0)
109         return TRUE;
110 #else
111         if (mkdir(path, 0777) >= 0)     /* try */
112                 return TRUE;    /* successful done. */
113 #endif
114
115         if (!make_parent_path(path))
116                 return FALSE;
117
118 #if defined __MINGW32__
119     if (mkdir(path) < 0) {      /* try again */
120         error("Cannot make directory \"%s\"", path);
121         return FALSE;
122     }
123 #else
124         if (mkdir(path, 0777) < 0) {    /* try again */
125         error("Cannot make directory \"%s\"", path);
126         return FALSE;
127         }
128 #endif
129
130         return TRUE;
131 }
132
133 /* ------------------------------------------------------------------------ */
134 static FILE    *
135 open_with_make_path(name)
136         char           *name;
137 {
138         FILE           *fp;
139
140         if ((fp = fopen(name, WRITE_BINARY)) == NULL) {
141                 if (!make_parent_path(name) ||
142                     (fp = fopen(name, WRITE_BINARY)) == NULL)
143                         error("Cannot extract a file \"%s\"", name);
144         }
145         return fp;
146 }
147
148 /* ------------------------------------------------------------------------ */
149 static void
150 adjust_info(name, hdr)
151         char           *name;
152         LzHeader       *hdr;
153 {
154     struct utimbuf utimebuf;
155
156         /* adjust file stamp */
157         utimebuf.actime = utimebuf.modtime = hdr->unix_last_modified_stamp;
158
159         if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) != UNIX_FILE_SYMLINK)
160                 utime(name, &utimebuf);
161
162         if (hdr->extend_type == EXTEND_UNIX
163             || hdr->extend_type == EXTEND_OS68K
164             || hdr->extend_type == EXTEND_XOSK) {
165 #ifdef NOT_COMPATIBLE_MODE
166                 Please need your modification in this space.
167 #else
168                 if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) != UNIX_FILE_SYMLINK)
169                         chmod(name, hdr->unix_mode);
170 #endif
171                 if (!getuid()){
172             uid_t uid = hdr->unix_uid;
173             gid_t gid = hdr->unix_gid;
174
175 #if HAVE_GETPWNAM && HAVE_GETGRNAM
176             if (hdr->user[0]) {
177                 struct passwd *ent = getpwnam(hdr->user);
178                 if (ent) uid = ent->pw_uid;
179             }
180             if (hdr->group[0]) {
181                 struct group *ent = getgrnam(hdr->group);
182                 if (ent) gid = ent->gr_gid;
183             }
184 #endif
185
186 #if HAVE_LCHOWN
187                         if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK)
188                                 lchown(name, uid, gid);
189                         else
190 #endif /* HAVE_LCHWON */
191                                 chown(name, uid, gid);
192                 }
193         }
194 }
195
196 /* ------------------------------------------------------------------------ */
197 static void
198 extract_one(afp, hdr)
199         FILE           *afp;    /* archive file */
200         LzHeader       *hdr;
201 {
202         FILE           *fp;     /* output file */
203         struct stat     stbuf;
204         char            name[FILENAME_LENGTH];
205         unsigned int crc;
206         int             method;
207         boolean         save_quiet, save_verbose, up_flag;
208         char           *q = hdr->name, c;
209
210         if (ignore_directory && strrchr(hdr->name, '/')) {
211                 q = (char *) strrchr(hdr->name, '/') + 1;
212         }
213         else {
214                 if (*q == '/') {
215                         q++;
216                         /*
217                          * if OSK then strip device name
218                          */
219                         if (hdr->extend_type == EXTEND_OS68K
220                             || hdr->extend_type == EXTEND_XOSK) {
221                                 do
222                                         c = (*q++);
223                                 while (c && c != '/');
224                                 if (!c || !*q)
225                                         q = ".";        /* if device name only */
226                         }
227                 }
228         }
229
230         if (extract_directory)
231                 xsnprintf(name, sizeof(name), "%s/%s", extract_directory, q);
232         else
233                 strcpy(name, q);
234
235
236         /* LZHDIRS_METHOD¤ò»ý¤Ä¥Ø¥Ã¥À¤ò¥Á¥§¥Ã¥¯¤¹¤ë */
237         /* 1999.4.30 t.okamoto */
238         for (method = 0;; method++) {
239                 if (methods[method] == NULL) {
240                         error("Unknown method \"%.*s\"; \"%s\" will be skiped ...",
241                   5, hdr->method, name);
242                         return;
243                 }
244                 if (memcmp(hdr->method, methods[method], 5) == 0)
245                         break;
246         }
247
248         if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_REGULAR
249                 && method != LZHDIRS_METHOD_NUM) {
250     extract_regular:
251 #if 0
252                 for (method = 0;; method++) {
253                         if (methods[method] == NULL) {
254                 error("Unknown method \"%.*s\"; \"%s\" will be skiped ...",
255                       5, hdr->method, name);
256                                 return;
257                         }
258                         if (memcmp(hdr->method, methods[method], 5) == 0)
259                                 break;
260                 }
261 #endif
262
263                 reading_filename = archive_name;
264                 writing_filename = name;
265                 if (output_to_stdout || verify_mode) {
266                         if (noexec) {
267                                 printf("%s %s\n", verify_mode ? "VERIFY" : "EXTRACT", name);
268                                 if (afp == stdin) {
269                                         int             i = hdr->packed_size;
270                                         while (i--)
271                                                 fgetc(afp);
272                                 }
273                                 return;
274                         }
275
276                         save_quiet = quiet;
277                         save_verbose = verbose;
278                         if (!quiet && output_to_stdout) {
279                                 printf("::::::::\n%s\n::::::::\n", name);
280                                 quiet = TRUE;
281                                 verbose = FALSE;
282                         }
283                         else if (verify_mode) {
284                                 quiet = FALSE;
285                                 verbose = TRUE;
286                         }
287
288 #if __MINGW32__
289             {
290                 int old_mode;
291                 fflush(stdout);
292                 old_mode = setmode(fileno(stdout), O_BINARY);
293 #endif
294
295                         crc = decode_lzhuf
296                                 (afp, stdout, hdr->original_size, hdr->packed_size, name, method);
297 #if __MINGW32__
298                 fflush(stdout);
299                 setmode(fileno(stdout), old_mode);
300             }
301 #endif
302                         quiet = save_quiet;
303                         verbose = save_verbose;
304                 }
305                 else {
306                         if (skip_flg == FALSE)  {
307                                 up_flag = inquire_extract(name);
308                                 if (up_flag == FALSE && force == FALSE) {
309                                         return;
310                                 }
311                         }
312
313                         if (skip_flg == TRUE) { /* if skip_flg */
314                                 if (stat(name, &stbuf) == 0 && force != TRUE) {
315                                         if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {
316                                                 if (quiet != TRUE)
317                                                         printf("%s : Skipped...\n", name);
318                                                 return;
319                                         }
320                                 }
321                         }
322                         if (noexec) {
323                                 if (afp == stdin) {
324                                         int i = hdr->packed_size;
325                                         while (i--)
326                                                 fgetc(afp);
327                                 }
328                                 return;
329                         }
330
331                         signal(SIGINT, interrupt);
332 #ifdef SIGHUP
333                         signal(SIGHUP, interrupt);
334 #endif
335
336                         unlink(name);
337                         remove_extracting_file_when_interrupt = TRUE;
338
339                         if ((fp = open_with_make_path(name)) != NULL) {
340                                 crc = decode_lzhuf
341                                         (afp, fp, hdr->original_size, hdr->packed_size, name, method);
342                                 fclose(fp);
343                         }
344                         remove_extracting_file_when_interrupt = FALSE;
345                         signal(SIGINT, SIG_DFL);
346 #ifdef SIGHUP
347                         signal(SIGHUP, SIG_DFL);
348 #endif
349                         if (!fp)
350                                 return;
351                 }
352
353                 if (hdr->has_crc && crc != hdr->crc)
354                         error("CRC error: \"%s\"", name);
355         }
356         else if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_DIRECTORY
357                          || (hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK
358                          || method == LZHDIRS_METHOD_NUM) {
359                 /* ¢¬¤³¤ì¤Ç¡¢Symbolic Link ¤Ï¡¢Âç¾æÉפ«¡© */
360                 if (!ignore_directory && !verify_mode) {
361                         if (noexec) {
362                                 if (quiet != TRUE)
363                                         printf("EXTRACT %s (directory)\n", name);
364                                 return;
365                         }
366                         /* NAME has trailing SLASH '/', (^_^) */
367                         if ((hdr->unix_mode & UNIX_FILE_TYPEMASK) == UNIX_FILE_SYMLINK) {
368                                 int             l_code;
369
370 #ifdef S_IFLNK
371                                 if (skip_flg == FALSE)  {
372                                         up_flag = inquire_extract(name);
373                                         if (up_flag == FALSE && force == FALSE) {
374                                                 return;
375                                         }
376                                 } else {
377                                         if (GETSTAT(name, &stbuf) == 0 && force != TRUE) {
378                                                 if (stbuf.st_mtime >= hdr->unix_last_modified_stamp) {
379                                                         if (quiet != TRUE)
380                                                                 printf("%s : Skipped...\n", name);
381                                                         return;
382                                                 }
383                                         }
384                                 }
385
386                                 unlink(name);
387                 make_parent_path(name);
388                                 l_code = symlink(hdr->realname, name);
389                                 if (l_code < 0) {
390                                         if (quiet != TRUE)
391                                                 warning("Can't make Symbolic Link \"%s\" -> \"%s\"",
392                                 hdr->realname, name);
393                                 }
394                                 if (quiet != TRUE) {
395                                         message("Symbolic Link %s -> %s",
396                             hdr->realname, name);
397                                 }
398 #else
399                                 warning("Can't make Symbolic Link %s -> %s",
400                         hdr->realname, name);
401                                 return;
402 #endif
403                         } else { /* make directory */
404                                 if (!output_to_stdout && !make_parent_path(name))
405                                         return;
406                         }
407                 }
408         }
409         else {
410         if (force)              /* force extract */
411             goto extract_regular;
412         else
413             error("Unknown file type: \"%s\". use `f' option to force extract.", name);
414         }
415
416         if (!output_to_stdout)
417                 adjust_info(name, hdr);
418 }
419
420 /* ------------------------------------------------------------------------ */
421 /* EXTRACT COMMAND MAIN                                                                                                         */
422 /* ------------------------------------------------------------------------ */
423 void
424 cmd_extract()
425 {
426         LzHeader        hdr;
427         long            pos;
428         FILE           *afp;
429
430         /* open archive file */
431         if ((afp = open_old_archive()) == NULL)
432                 fatal_error("Cannot open archive file \"%s\"", archive_name);
433
434         if (archive_is_msdos_sfx1(archive_name))
435                 skip_msdos_sfx1_code(afp);
436
437         /* extract each files */
438         while (get_header(afp, &hdr)) {
439                 if (need_file(hdr.name)) {
440                         pos = ftell(afp);
441                         extract_one(afp, &hdr);
442             /* when error occurred in extract_one(), should adjust
443                point of file stream */
444                         if (afp != stdin)
445                 fseek(afp, pos + hdr.packed_size, SEEK_SET);
446             else {
447                 /* FIXME: assume that the extract_one() has read
448                    packed_size bytes from stdin. */
449                 long i = 0;
450                                 while (i--) fgetc(afp);
451             }
452                 } else {
453                         if (afp != stdin)
454                                 fseek(afp, hdr.packed_size, SEEK_CUR);
455                         else {
456                                 int             i = hdr.packed_size;
457                                 while (i--)
458                                         fgetc(afp);
459                         }
460                 }
461         }
462
463         /* close archive file */
464         fclose(afp);
465
466         return;
467 }
468
469 /* Local Variables: */
470 /* mode:c */
471 /* tab-width:4 */
472 /* End: */
473 /* vi: set tabstop=4: */