Yet another installment in the ongoing tar saga
[oweals/busybox.git] / archival / tar.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  *
4  * Mini tar implementation for busybox Note, that as of BusyBox 0.43 tar has
5  * been completely rewritten from the ground up.  It still has remnents of the
6  * old code lying about, but it pretty different (i.e. cleaner, less global
7  * variables, etc)
8  *
9  * Copyright (C) 1999 by Lineo, inc.
10  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
11  *
12  * Based in part in the tar implementation in sash
13  *  Copyright (c) 1999 by David I. Bell
14  *  Permission is granted to use, distribute, or modify this source,
15  *  provided that this copyright notice remains intact.
16  *  Permission to distribute sash derived code under the GPL has been granted.
17  *
18  * Based in part on the tar implementation in busybox-0.28
19  *  Copyright (C) 1995 Bruce Perens
20  *  This is free software under the GNU General Public License.
21  *
22  * This program is free software; you can redistribute it and/or modify
23  * it under the terms of the GNU General Public License as published by
24  * the Free Software Foundation; either version 2 of the License, or
25  * (at your option) any later version.
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30  * General Public License for more details.
31  *
32  * You should have received a copy of the GNU General Public License
33  * along with this program; if not, write to the Free Software
34  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35  *
36  */
37
38
39 #include "internal.h"
40 #define BB_DECLARE_EXTERN
41 #define bb_need_io_error
42 #include "messages.c"
43 #include <stdio.h>
44 #include <dirent.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <signal.h>
48 #include <time.h>
49 #include <utime.h>
50 #include <sys/types.h>
51 #include <sys/sysmacros.h>
52 #include <sys/param.h>                  /* for PATH_MAX */
53
54
55 #ifdef BB_FEATURE_TAR_CREATE
56
57 static const char tar_usage[] =
58         "tar -[cxtvOf] [tarFileName] [FILE] ...\n\n"
59         "Create, extract, or list files from a tar file.\n\n"
60         "Options:\n"
61
62         "\tc=create, x=extract, t=list contents, v=verbose,\n"
63         "\tO=extract to stdout, f=tarfile or \"-\" for stdin\n";
64
65 #else
66
67 static const char tar_usage[] =
68         "tar -[xtvOf] [tarFileName] [FILE] ...\n\n"
69         "Extract, or list files stored in a tar file.  This\n"
70         "version of tar does not support creation of tar files.\n\n"
71         "Options:\n"
72
73         "\tx=extract, t=list contents, v=verbose,\n"
74         "\tO=extract to stdout, f=tarfile or \"-\" for stdin\n";
75
76 #endif
77
78
79 /* Tar file constants  */
80 #ifndef MAJOR
81 #define MAJOR(dev) (((dev)>>8)&0xff)
82 #define MINOR(dev) ((dev)&0xff)
83 #endif
84
85
86 /* POSIX tar Header Block, from POSIX 1003.1-1990  */
87 struct TarHeader
88 {
89                                 /* byte offset */
90         char name[100];               /*   0 */
91         char mode[8];                 /* 100 */
92         char uid[8];                  /* 108 */
93         char gid[8];                  /* 116 */
94         char size[12];                /* 124 */
95         char mtime[12];               /* 136 */
96         char chksum[8];               /* 148 */
97         char typeflag;                /* 156 */
98         char linkname[100];           /* 157 */
99         char magic[6];                /* 257 */
100         char version[2];              /* 263 */
101         char uname[32];               /* 265 */
102         char gname[32];               /* 297 */
103         char devmajor[8];             /* 329 */
104         char devminor[8];             /* 337 */
105         char prefix[155];             /* 345 */
106         /* padding                       500 */
107 };
108 typedef struct TarHeader TarHeader;
109
110
111 /* A few useful constants */
112 #define TAR_MAGIC          "ustar"        /* ustar and a null */
113 #define TAR_VERSION        "00"           /* 00 and no null */
114 #define TAR_MAGIC_LEN       6
115 #define TAR_VERSION_LEN     2
116 #define TAR_NAME_LEN        100
117 #define TAR_BLOCK_SIZE      512
118
119 /* A nice enum with all the possible tar file content types */
120 enum TarFileType 
121 {
122         REGTYPE  = '0',            /* regular file */
123         REGTYPE0 = '\0',           /* regular file (ancient bug compat)*/
124         LNKTYPE  = '1',            /* hard link */
125         SYMTYPE  = '2',            /* symbolic link */
126         CHRTYPE  = '3',            /* character special */
127         BLKTYPE  = '4',            /* block special */
128         DIRTYPE  = '5',            /* directory */
129         FIFOTYPE = '6',            /* FIFO special */
130         CONTTYPE = '7',            /* reserved */
131 };
132 typedef enum TarFileType TarFileType;
133
134 /* This struct ignores magic, non-numeric user name, 
135  * non-numeric group name, and the checksum, since
136  * these are all ignored by BusyBox tar. */ 
137 struct TarInfo
138 {
139         int              tarFd;          /* An open file descriptor for reading from the tarball */
140         char *           name;           /* File name */
141         mode_t           mode;           /* Unix mode, including device bits. */
142         uid_t            uid;            /* Numeric UID */
143         gid_t            gid;            /* Numeric GID */
144         size_t           size;           /* Size of file */
145         time_t           mtime;          /* Last-modified time */
146         enum TarFileType type;           /* Regular, directory, link, etc */
147         char *           linkname;       /* Name for symbolic and hard links */
148         long             devmajor;       /* Major number for special device */
149         long             devminor;       /* Minor number for special device */
150 };
151 typedef struct TarInfo TarInfo;
152
153 /* Static data  */
154 static const unsigned long TarChecksumOffset = (const unsigned long)&(((TarHeader *)0)->chksum);
155
156
157 /* Local procedures to restore files from a tar file.  */
158 static int readTarFile(const char* tarName, int extractFlag, int listFlag, 
159                 int tostdoutFlag, int verboseFlag);
160
161
162
163 #ifdef BB_FEATURE_TAR_CREATE
164 /* Local procedures to save files into a tar file.  */
165 static int writeTarFile(const char* tarName, int extractFlag, int listFlag, 
166                 int tostdoutFlag, int verboseFlag, int argc, char **argv);
167 static int putOctal(char *cp, int len, long value);
168
169 #endif
170
171
172 extern int tar_main(int argc, char **argv)
173 {
174         const char *tarName=NULL;
175         const char *options;
176         int listFlag     = FALSE;
177         int extractFlag  = FALSE;
178         int createFlag   = FALSE;
179         int verboseFlag  = FALSE;
180         int tostdoutFlag = FALSE;
181
182         argc--;
183         argv++;
184
185         if (argc < 1)
186                 usage(tar_usage);
187
188         /* Parse options  */
189         if (**argv == '-')
190                 options = (*argv++) + 1;
191         else
192                 options = (*argv++);
193         argc--;
194
195         for (; *options; options++) {
196                 switch (*options) {
197                 case 'f':
198                         if (tarName != NULL)
199                                 fatalError( "Only one 'f' option allowed\n");
200
201                         tarName = *argv++;
202                         if (tarName == NULL)
203                                 fatalError( "Option requires an argument: No file specified\n");
204                         argc--;
205
206                         break;
207
208                 case 't':
209                         if (extractFlag == TRUE || createFlag == TRUE)
210                                 goto flagError;
211                         listFlag = TRUE;
212                         break;
213
214                 case 'x':
215                         if (listFlag == TRUE || createFlag == TRUE)
216                                 goto flagError;
217                         extractFlag = TRUE;
218                         break;
219                 case 'c':
220                         if (extractFlag == TRUE || listFlag == TRUE)
221                                 goto flagError;
222                         createFlag = TRUE;
223                         break;
224
225                 case 'v':
226                         verboseFlag = TRUE;
227                         break;
228
229                 case 'O':
230                         tostdoutFlag = TRUE;
231                         tarName = "-";
232                         break;
233
234                 case '-':
235                         usage(tar_usage);
236                         break;
237
238                 default:
239                         fatalError( "Unknown tar flag '%c'\n" 
240                                         "Try `tar --help' for more information\n", *options);
241                 }
242         }
243
244         /* 
245          * Do the correct type of action supplying the rest of the
246          * command line arguments as the list of files to process.
247          */
248         if (createFlag == TRUE) {
249 #ifndef BB_FEATURE_TAR_CREATE
250                 fatalError( "This version of tar was not compiled with tar creation support.\n");
251 #else
252                 exit(writeTarFile(tarName, extractFlag, listFlag, tostdoutFlag, verboseFlag, argc, argv));
253 #endif
254         } else {
255                 exit(readTarFile(tarName, extractFlag, listFlag, tostdoutFlag, verboseFlag));
256         }
257
258   flagError:
259         fatalError( "Exactly one of 'c', 'x' or 't' must be specified\n");
260 }
261                                         
262 static void
263 tarExtractRegularFile(TarInfo *header, int extractFlag, int tostdoutFlag)
264 {
265         size_t  writeSize;
266         size_t  readSize;
267         size_t  actualWriteSz;
268         char    buffer[BUFSIZ];
269         size_t  size = header->size;
270         int outFd=fileno(stdout);
271
272         /* Open the file to be written, if a file is supposed to be written */
273         if (extractFlag==TRUE && tostdoutFlag==FALSE) {
274                 if ((outFd=open(header->name, O_CREAT|O_TRUNC|O_WRONLY, header->mode & ~S_IFMT)) < 0)
275                         errorMsg(io_error, header->name, strerror(errno)); 
276                 /* Create the path to the file, just in case it isn't there...
277                  * This should not screw up path permissions or anything. */
278                 createPath(header->name, 0777);
279         }
280
281         /* Write out the file, if we are supposed to be doing that */
282         while ( size > 0 ) {
283                 actualWriteSz=0;
284                 if ( size > sizeof(buffer) )
285                         writeSize = readSize = sizeof(buffer);
286                 else {
287                         int mod = size % 512;
288                         if ( mod != 0 )
289                                 readSize = size + (512 - mod);
290                         else
291                                 readSize = size;
292                         writeSize = size;
293                 }
294                 if ( (readSize = fullRead(header->tarFd, buffer, readSize)) <= 0 ) {
295                         /* Tarball seems to have a problem */
296                         errorMsg("tar: Unexpected EOF in archive\n"); 
297                         return;
298                 }
299                 if ( readSize < writeSize )
300                         writeSize = readSize;
301
302                 /* Write out the file, if we are supposed to be doing that */
303                 if (extractFlag==TRUE) {
304
305                         if ((actualWriteSz=fullWrite(outFd, buffer, writeSize)) != writeSize ) {
306                                 /* Output file seems to have a problem */
307                                 errorMsg(io_error, header->name, strerror(errno)); 
308                                 return;
309                         }
310                 }
311
312                 size -= actualWriteSz;
313         }
314
315         /* Now we are done writing the file out, so try 
316          * and fix up the permissions and whatnot */
317         if (extractFlag==TRUE && tostdoutFlag==FALSE) {
318                 struct utimbuf t;
319                 /* Now set permissions etc for the new file */
320                 fchown(outFd, header->uid, header->gid);
321                 fchmod(outFd, header->mode & ~S_IFMT);
322                 close(outFd);
323                 /* File must be closed before trying to change the date */
324                 t.actime = time(0);
325                 t.modtime = header->mtime;
326                 utime(header->name, &t);
327         }
328 }
329
330 static void
331 fixUpPermissions(TarInfo *header)
332 {
333         struct utimbuf t;
334         /* Now set permissions etc for the new file */
335         chown(header->name, header->uid, header->gid);
336         chmod(header->name, header->mode);
337         /* Reset the time */
338         t.actime = time(0);
339         t.modtime = header->mtime;
340         utime(header->name, &t);
341 }
342                                 
343 static void
344 tarExtractDirectory(TarInfo *header, int extractFlag, int tostdoutFlag)
345 {
346
347         if (extractFlag==FALSE || tostdoutFlag==TRUE)
348                 return;
349
350         if (createPath(header->name, header->mode) != TRUE) {
351                 errorMsg("Error creating directory '%s': %s", header->name, strerror(errno)); 
352                 return;  
353         }
354         /* make the final component, just in case it was
355          * omitted by createPath() (which will skip the
356          * directory if it doesn't have a terminating '/') */
357         if (mkdir(header->name, header->mode) == 0) {
358                 fixUpPermissions(header);
359         }
360 }
361
362 static void
363 tarExtractHardLink(TarInfo *header, int extractFlag, int tostdoutFlag)
364 {
365         if (extractFlag==FALSE || tostdoutFlag==TRUE)
366                 return;
367
368         if (link(header->linkname, header->name) < 0) {
369                 errorMsg("Error creating hard link '%s': %s\n", header->linkname, strerror(errno)); 
370                 return;
371         }
372
373         /* Now set permissions etc for the new directory */
374         fixUpPermissions(header);
375 }
376
377 static void
378 tarExtractSymLink(TarInfo *header, int extractFlag, int tostdoutFlag)
379 {
380         if (extractFlag==FALSE || tostdoutFlag==TRUE)
381                 return;
382
383 #ifdef  S_ISLNK
384         if (symlink(header->linkname, header->name) < 0) {
385                 errorMsg("Error creating symlink '%s': %s\n", header->linkname, strerror(errno)); 
386                 return;
387         }
388         /* Try to change ownership of the symlink.
389          * If libs doesn't support that, don't bother.
390          * Changing the pointed-to-file is the Wrong Thing(tm).
391          */
392 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
393         lchown(header->name, header->uid, header->gid);
394 #endif
395
396         /* Do not change permissions or date on symlink,
397          * since it changes the pointed to file instead.  duh. */
398 #else
399         fprintf(stderr, "Cannot create symbolic links\n");
400 #endif
401 }
402
403 static void
404 tarExtractSpecial(TarInfo *header, int extractFlag, int tostdoutFlag)
405 {
406         if (extractFlag==FALSE || tostdoutFlag==TRUE)
407                 return;
408
409         if (S_ISCHR(header->mode) || S_ISBLK(header->mode) || S_ISSOCK(header->mode)) {
410                 mknod(header->name, header->mode, makedev(header->devmajor, header->devminor));
411         } else if (S_ISFIFO(header->mode)) {
412                 mkfifo(header->name, header->mode);
413         } else {
414                 open(header->name, O_WRONLY | O_CREAT | O_TRUNC, header->mode);
415         }
416
417         /* Now set permissions etc for the new directory */
418         fixUpPermissions(header);
419 }
420
421 /* Read an octal value in a field of the specified width, with optional
422  * spaces on both sides of the number and with an optional null character
423  * at the end.  Returns -1 on an illegal format.  */
424 static long getOctal(const char *cp, int size)
425 {
426         long val = 0;
427
428         for(;(size > 0) && (*cp == ' '); cp++, size--);
429         if ((size == 0) || !isOctal(*cp))
430                 return -1;
431         for(; (size > 0) && isOctal(*cp); size--) {
432                 val = val * 8 + *cp++ - '0';
433         }
434         for (;(size > 0) && (*cp == ' '); cp++, size--);
435         if ((size > 0) && *cp)
436                 return -1;
437         return val;
438 }
439
440
441 /* Parse the tar header and fill in the nice struct with the details */
442 static int
443 readTarHeader(struct TarHeader *rawHeader, struct TarInfo *header)
444 {
445         int i;
446         long chksum, sum;
447         unsigned char *s = (unsigned char *)rawHeader;
448
449         header->name  = rawHeader->name;
450         header->mode  = getOctal(rawHeader->mode, sizeof(rawHeader->mode));
451         header->uid   =  getOctal(rawHeader->uid, sizeof(rawHeader->uid));
452         header->gid   =  getOctal(rawHeader->gid, sizeof(rawHeader->gid));
453         header->size  = getOctal(rawHeader->size, sizeof(rawHeader->size));
454         header->mtime = getOctal(rawHeader->mtime, sizeof(rawHeader->mtime));
455         chksum = getOctal(rawHeader->chksum, sizeof(rawHeader->chksum));
456         header->type  = rawHeader->typeflag;
457         header->linkname  = rawHeader->linkname;
458         header->devmajor  = getOctal(rawHeader->devmajor, sizeof(rawHeader->devmajor));
459         header->devminor  = getOctal(rawHeader->devminor, sizeof(rawHeader->devminor));
460
461         /* Check the checksum */
462         sum = ' ' * sizeof(rawHeader->chksum);
463         for ( i = TarChecksumOffset; i > 0; i-- )
464                 sum += *s++;
465         s += sizeof(rawHeader->chksum);       
466         for ( i = (512 - TarChecksumOffset - sizeof(rawHeader->chksum)); i > 0; i-- )
467                 sum += *s++;
468         if (sum == chksum )
469                 return ( TRUE);
470         return( FALSE);
471 }
472
473
474 /*
475  * Read a tar file and extract or list the specified files within it.
476  * If the list is empty than all files are extracted or listed.
477  */
478 static int readTarFile(const char* tarName, int extractFlag, int listFlag, 
479                 int tostdoutFlag, int verboseFlag)
480 {
481         int status, tarFd=0;
482         int errorFlag=FALSE;
483         TarHeader rawHeader;
484         TarInfo header;
485         int alreadyWarned=FALSE;
486         //int skipFileFlag=FALSE;
487
488         /* Open the tar file for reading.  */
489         if (!strcmp(tarName, "-"))
490                 tarFd = fileno(stdin);
491         else
492                 tarFd = open(tarName, O_RDONLY);
493         if (tarFd < 0) {
494                 errorMsg( "Error opening '%s': %s\n", tarName, strerror(errno));
495                 return ( FALSE);
496         }
497
498         /* Set the umask for this process so it doesn't 
499          * screw up permission setting for us later. */
500         umask(0);
501
502         /* Read the tar file, and iterate over it one file at a time */
503         while ( (status = fullRead(tarFd, (char*)&rawHeader, TAR_BLOCK_SIZE)) == TAR_BLOCK_SIZE ) {
504
505                 /* First, try to read the header */
506                 if ( readTarHeader(&rawHeader, &header) == FALSE ) {
507                         close( tarFd);
508                         if ( *(header.name) == '\0' ) {
509                                 goto endgame;
510                         } else {
511                                 errorFlag=TRUE;
512                                 errorMsg("Bad tar header, skipping\n");
513                                 continue;
514                         }
515                 }
516                 if ( *(header.name) == '\0' )
517                                 goto endgame;
518
519                 /* Check for and relativify any absolute paths */
520                 if ( *(header.name) == '/' ) {
521
522                         while (*(header.name) == '/')
523                                 ++*(header.name);
524
525                         if (alreadyWarned == FALSE) {
526                                 errorMsg("Absolute path detected, removing leading slashes\n");
527                                 alreadyWarned = TRUE;
528                         }
529                 }
530
531                 /* Special treatment if the list (-t) flag is on */
532                 if (verboseFlag == TRUE && extractFlag == FALSE) {
533                         int len, len1;
534                         char buf[35];
535                         struct tm *tm = localtime (&(header.mtime));
536
537                         len=printf("%s %d/%-d ", modeString(header.mode), header.uid, header.gid);
538                         if (header.type==CHRTYPE || header.type==BLKTYPE) {
539                                 len1=snprintf(buf, sizeof(buf), "%ld,%-ld ", 
540                                                 header.devmajor, header.devminor);
541                         } else {
542                                 len1=snprintf(buf, sizeof(buf), "%d ", header.size);
543                         }
544                         /* Jump through some hoops to make the columns match up */
545                         for(;(len+len1)<31;len++)
546                                 printf(" ");
547                         printf(buf);
548
549                         /* Use ISO 8610 time format */
550                         if (tm) { 
551                                 printf ("%04d-%02d-%02d %02d:%02d:%02d ", 
552                                                 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, 
553                                                 tm->tm_hour, tm->tm_min, tm->tm_sec);
554                         }
555                 }
556                 /* List contents if we are supposed to do that */
557                 if (verboseFlag == TRUE || listFlag == TRUE) {
558                         /* Now the normal listing */
559                         printf("%s", header.name);
560                         /* If this is a link, say so */
561                         if (header.type==LNKTYPE)
562                                 printf(" link to %s", header.linkname);
563                         else if (header.type==SYMTYPE)
564                                 printf(" -> %s", header.linkname);
565                         printf("\n");
566                 }
567
568 #if 0
569                 /* See if we want to restore this file or not */
570                 skipFileFlag=FALSE;
571                 if (wantFileName(outName) == FALSE) {
572                         skipFileFlag = TRUE;
573                 }
574 #endif
575
576                 /* If we got here, we can be certain we have a legitimate 
577                  * header to work with.  So work with it.  */
578                 switch ( header.type ) {
579                         case REGTYPE:
580                         case REGTYPE0:
581                                 /* If the name ends in a '/' then assume it is
582                                  * supposed to be a directory, and fall through */
583                                 if (header.name[strlen(header.name)-1] != '/') {
584                                         tarExtractRegularFile(&header, extractFlag, tostdoutFlag);
585                                         break;
586                                 }
587                         case DIRTYPE:
588                                 tarExtractDirectory( &header, extractFlag, tostdoutFlag);
589                                 break;
590                         case LNKTYPE:
591                                 tarExtractHardLink( &header, extractFlag, tostdoutFlag);
592                                 break;
593                         case SYMTYPE:
594                                 tarExtractSymLink( &header, extractFlag, tostdoutFlag);
595                                 break;
596                         case CHRTYPE:
597                         case BLKTYPE:
598                         case FIFOTYPE:
599                                 tarExtractSpecial( &header, extractFlag, tostdoutFlag);
600                                 break;
601                         default:
602                                 close( tarFd);
603                                 return( FALSE);
604                 }
605         }
606         close(tarFd);
607         if (status > 0) {
608                 /* Bummer - we read a partial header */
609                 errorMsg( "Error reading '%s': %s\n", tarName, strerror(errno));
610                 return ( FALSE);
611         }
612         else 
613                 return( status);
614
615         /* Stuff to do when we are done */
616 endgame:
617         close( tarFd);
618         if ( *(header.name) == '\0' ) {
619                 if (errorFlag==FALSE)
620                         return( TRUE);
621         } 
622         return( FALSE);
623 }
624
625
626 #ifdef BB_FEATURE_TAR_CREATE
627
628 /* Put an octal string into the specified buffer.
629  * The number is zero and space padded and possibly null padded.
630  * Returns TRUE if successful.  */ 
631 static int putOctal (char *cp, int len, long value)
632 {
633         int tempLength;
634         char *tempString;
635         char tempBuffer[32];
636
637         /* Create a string of the specified length with an initial space,
638          * leading zeroes and the octal number, and a trailing null.  */
639         tempString = tempBuffer;
640
641         sprintf (tempString, " %0*lo", len - 2, value);
642
643         tempLength = strlen (tempString) + 1;
644
645         /* If the string is too large, suppress the leading space.  */
646         if (tempLength > len) {
647                 tempLength--;
648                 tempString++;
649         }
650
651         /* If the string is still too large, suppress the trailing null.  */
652         if (tempLength > len)
653                 tempLength--;
654
655         /* If the string is still too large, fail.  */
656         if (tempLength > len)
657                 return FALSE;
658
659         /* Copy the string to the field.  */
660         memcpy (cp, tempString, len);
661
662         return TRUE;
663 }
664
665 /* Write out a tar header for the specified file */
666 static int
667 writeTarHeader(struct TarHeader *rawHeader, struct TarInfo *header)
668 {
669         int i;
670         long chksum, sum;
671         unsigned char *s = (unsigned char *)rawHeader;
672
673         struct TarHeader header;
674
675         strcpy(header.name, fileName); 
676         putOctal(header.mode, sizeof(header.mode), statbuf->st_mode & 0777);
677         putOctal(header.uid, sizeof(header.uid), statbuf->st_uid);
678         putOctal(header.gid, sizeof(header.gid), statbuf->st_gid);
679         putOctal(header.size, sizeof(header.size), statbuf->st_size);
680         putOctal(header.mtime, sizeof(header.mtime), statbuf->st_mtime);
681
682         if (S_ISLNK(statbuf.st_mode)) {
683                 header.type  = LNKTYPE;
684                 // Handle SYMTYPE
685         } else if (S_ISDIR(statbuf.st_mode)) {
686                 header.type  = DIRTYPE;
687         } else if (S_ISCHR(statbuf.st_mode)) {
688                 header.type  = CHRTYPE;
689         } else if (S_ISBLK(statbuf.st_mode)) {
690                 header.type  = BLKTYPE;
691         } else if (S_ISFIFO(statbuf.st_mode)) {
692                 header.type  = FIFOTYPE;
693         } else if (S_ISSOCK(statbuf.st_mode)) {
694                 header.type  = S_ISSOCK;
695         } else if (S_ISLNK(statbuf.st_mode)) {
696                 header.type  = LNKTYPE;
697         } else if (S_ISLNK(statbuf.st_mode)) {
698                 header.type  = REGTYPE;
699         }
700 #if 0   
701         header->linkname  = rawHeader->linkname;
702         header->devmajor  = getOctal(rawHeader->devmajor, sizeof(rawHeader->devmajor));
703         header->devminor  = getOctal(rawHeader->devminor, sizeof(rawHeader->devminor));
704
705         /* Write out the checksum */
706         chksum = getOctal(rawHeader->chksum, sizeof(rawHeader->chksum));
707 #endif
708
709         return ( TRUE);
710 }
711
712
713 static int fileAction(const char *fileName, struct stat *statbuf, void* userData)
714 {
715         int *tarFd=(int*)userData;
716         dprintf(*tarFd, "%s\n", fileName);
717         return (TRUE);
718 }
719
720 static int writeTarFile(const char* tarName, int extractFlag, int listFlag, 
721                 int tostdoutFlag, int verboseFlag, int argc, char **argv)
722 {
723         int tarFd=-1;
724         //int errorFlag=FALSE;
725         //TarHeader rawHeader;
726         //TarInfo header;
727         //int alreadyWarned=FALSE;
728         //int skipFileFlag=FALSE;
729         struct stat tarballStat;
730         dev_t tarDev = 0;
731         ino_t tarInode = 0;
732
733         /* Make sure there is at least one file to tar up.  */
734         if (argc <= 0)
735                 fatalError("tar: Cowardly refusing to create an empty archive\n");
736
737         /* Open the tar file for writing.  */
738         if (tostdoutFlag == TRUE)
739                 tarFd = fileno(stdout);
740         else
741                 tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
742         if (tarFd < 0) {
743                 errorMsg( "Error opening '%s': %s\n", tarName, strerror(errno));
744                 return ( FALSE);
745         }
746         /* Store the device and inode of the tarball, so we can be sure
747          * not to try and include it into itself....  */
748         if (fstat(tarFd, &tarballStat) < 0)
749                 fatalError(io_error, tarName, strerror(errno)); 
750         tarDev = tarballStat.st_dev;
751         tarInode = tarballStat.st_ino;
752
753         /* Set the umask for this process so it doesn't 
754          * screw up permission setting for us later. */
755         umask(0);
756
757         /* Read the directory/files and iterate over them one at a time */
758         while (argc-- > 0) {
759                 if (recursiveAction(*argv++, TRUE, FALSE, FALSE,
760                                         fileAction, fileAction, (void*) &tarFd) == FALSE) {
761                         exit(FALSE);
762                 }
763         }
764
765         close(tarFd);
766         return( TRUE);
767 }
768
769
770 #endif
771