3 Creates a tar archive. */
9 static void usage (void);
10 static bool make_tar_archive (const char *archive_name,
11 char *files[], size_t file_cnt);
14 main (int argc, char *argv[])
19 return (make_tar_archive (argv[1], argv + 2, argc - 2)
20 ? EXIT_SUCCESS : EXIT_FAILURE);
26 printf ("tar, tar archive creator\n"
27 "Usage: tar ARCHIVE FILE...\n"
28 "where ARCHIVE is the tar archive to create\n"
29 " and FILE... is a list of files or directories to put into it.\n"
30 "(ARCHIVE itself will not be included in the archive, even if it\n"
31 "is in a directory to be archived.)\n");
35 static bool archive_file (char file_name[], size_t file_name_size,
36 int archive_fd, bool *write_error);
38 static bool archive_ordinary_file (const char *file_name, int file_fd,
39 int archive_fd, bool *write_error);
40 static bool archive_directory (char file_name[], size_t file_name_size,
41 int file_fd, int archive_fd, bool *write_error);
42 static bool write_header (const char *file_name,
43 char type_flag, int size, unsigned mode,
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, '0', file_size, 0644, archive_fd, write_error))
134 while (file_size > 0)
136 static char buf[512];
137 int chunk_size = file_size > 512 ? 512 : file_size;
138 int read_retval = read (file_fd, buf, chunk_size);
139 int bytes_read = read_retval > 0 ? read_retval : 0;
141 if (bytes_read != chunk_size && !read_error)
143 printf ("%s: read error\n", file_name);
148 memset (buf + bytes_read, 0, 512 - bytes_read);
149 if (!do_write (archive_fd, buf, 512, write_error))
152 file_size -= chunk_size;
159 archive_directory (char file_name[], size_t file_name_size, int file_fd,
160 int archive_fd, bool *write_error)
165 dir_len = strlen (file_name);
166 if (dir_len + 1 + READDIR_MAX_LEN + 1 > file_name_size)
168 printf ("%s: file name too long\n", file_name);
172 if (!write_header (file_name, '5', 0, 0755, archive_fd, write_error))
175 file_name[dir_len] = '/';
176 while (readdir (file_fd, &file_name[dir_len + 1]))
177 if (!archive_file (file_name, file_name_size, archive_fd, write_error))
179 file_name[dir_len] = '\0';
185 write_header (const char *file_name,
186 char type_flag, int size, unsigned mode,
187 int archive_fd, bool *write_error)
189 static char header[512];
193 memset (header, 0, sizeof header);
195 /* Drop confusing and possibly dangerous prefixes from
197 while (*file_name == '/'
198 || !memcmp (file_name, "./", 2)
199 || !memcmp (file_name, "../", 3))
200 file_name = strchr (file_name, '/') + 1;
201 if (*file_name == '\0')
203 /* Dropped *everything* from FILE_NAME.
204 Should only be possible for a directory. */
205 ASSERT (type_flag == '5');
208 else if (strlen (file_name) > 99)
210 printf ("%s: file name too long\n", file_name);
214 /* Fill in header except for final checksum. */
215 strlcpy (header, file_name, 100); /* name */
216 snprintf (header + 100, 8, "%07o", mode); /* mode */
217 strlcpy (header + 108, "0000000", 8); /* uid */
218 strlcpy (header + 116, "0000000", 8); /* gid */
219 snprintf (header + 124, 12, "%011o", size); /* size */
220 snprintf (header + 136, 12, "%011o", 1136102400); /* mtime (2006-01-01) */
221 memset (header + 148, ' ', 8); /* chksum */
222 header[156] = type_flag; /* typeflag */
223 strlcpy (header + 257, "ustar", 6); /* magic */
224 strlcpy (header + 263, "00", 3); /* version */
226 /* Compute and fill in final checksum. */
228 for (i = 0; i < 512; i++)
229 chksum += (uint8_t) header[i];
230 snprintf (header + 148, 8, "%07o", chksum);
233 return do_write (archive_fd, header, 512, write_error);
237 do_write (int fd, const char *buffer, int size, bool *write_error)
239 if (write (fd, buffer, size) == size)
245 printf ("error writing archive\n");