3 Creates a tar archive. */
10 static void usage (void);
11 static bool make_tar_archive (const char *archive_name,
12 char *files[], size_t file_cnt);
15 main (int argc, char *argv[])
20 return (make_tar_archive (argv[1], argv + 2, argc - 2)
21 ? EXIT_SUCCESS : EXIT_FAILURE);
27 printf ("tar, tar archive creator\n"
28 "Usage: tar ARCHIVE FILE...\n"
29 "where ARCHIVE is the tar archive to create\n"
30 " and FILE... is a list of files or directories to put into it.\n"
31 "(ARCHIVE itself will not be included in the archive, even if it\n"
32 "is in a directory to be archived.)\n");
36 static bool archive_file (char file_name[], size_t file_name_size,
37 int archive_fd, bool *write_error);
39 static bool archive_ordinary_file (const char *file_name, int file_fd,
40 int archive_fd, bool *write_error);
41 static bool archive_directory (char file_name[], size_t file_name_size,
42 int file_fd, int archive_fd, bool *write_error);
43 static bool write_header (const char *file_name, enum ustar_type, int size,
44 int archive_fd, bool *write_error);
46 static bool do_write (int fd, const char *buffer, int size, bool *write_error);
49 make_tar_archive (const char *archive_name, char *files[], size_t file_cnt)
51 static const char zeros[512];
54 bool write_error = false;
57 if (!create (archive_name, 0))
59 printf ("%s: create failed\n", archive_name);
62 archive_fd = open (archive_name);
65 printf ("%s: open failed\n", archive_name);
69 for (i = 0; i < file_cnt; i++)
73 strlcpy (file_name, files[i], sizeof file_name);
74 if (!archive_file (file_name, sizeof file_name,
75 archive_fd, &write_error))
79 if (!do_write (archive_fd, zeros, 512, &write_error)
80 || !do_write (archive_fd, zeros, 512, &write_error))
89 archive_file (char file_name[], size_t file_name_size,
90 int archive_fd, bool *write_error)
92 int file_fd = open (file_name);
97 if (inumber (file_fd) != inumber (archive_fd))
100 success = archive_ordinary_file (file_name, file_fd,
101 archive_fd, write_error);
103 success = archive_directory (file_name, file_name_size, file_fd,
104 archive_fd, write_error);
108 /* Nothing to do: don't try to archive the archive file. */
118 printf ("%s: open failed\n", file_name);
124 archive_ordinary_file (const char *file_name, int file_fd,
125 int archive_fd, bool *write_error)
127 bool read_error = false;
129 int file_size = filesize (file_fd);
131 if (!write_header (file_name, USTAR_REGULAR, file_size,
132 archive_fd, write_error))
135 while (file_size > 0)
137 static char buf[512];
138 int chunk_size = file_size > 512 ? 512 : file_size;
139 int read_retval = read (file_fd, buf, chunk_size);
140 int bytes_read = read_retval > 0 ? read_retval : 0;
142 if (bytes_read != chunk_size && !read_error)
144 printf ("%s: read error\n", file_name);
149 memset (buf + bytes_read, 0, 512 - bytes_read);
150 if (!do_write (archive_fd, buf, 512, write_error))
153 file_size -= chunk_size;
160 archive_directory (char file_name[], size_t file_name_size, int file_fd,
161 int archive_fd, bool *write_error)
166 dir_len = strlen (file_name);
167 if (dir_len + 1 + READDIR_MAX_LEN + 1 > file_name_size)
169 printf ("%s: file name too long\n", file_name);
173 if (!write_header (file_name, USTAR_DIRECTORY, 0, archive_fd, write_error))
176 file_name[dir_len] = '/';
177 while (readdir (file_fd, &file_name[dir_len + 1]))
178 if (!archive_file (file_name, file_name_size, archive_fd, write_error))
180 file_name[dir_len] = '\0';
186 write_header (const char *file_name, enum ustar_type type, int size,
187 int archive_fd, bool *write_error)
189 static char header[512];
190 return (ustar_make_header (file_name, type, size, header)
191 && do_write (archive_fd, header, 512, write_error));
195 do_write (int fd, const char *buffer, int size, bool *write_error)
197 if (write (fd, buffer, size) == size)
203 printf ("error writing archive\n");