2 * Copyright (c) 2005, 2006 Rene Scharfe
11 #define RECORDSIZE (512)
12 #define BLOCKSIZE (RECORDSIZE * 20)
14 static const char tar_tree_usage
[] = "git-tar-tree <key> [basedir]";
16 static char block
[BLOCKSIZE
];
17 static unsigned long offset
;
19 static time_t archive_time
;
21 /* tries hard to write, either succeeds or dies in the attempt */
22 static void reliable_write(void *buf
, unsigned long size
)
25 long ret
= xwrite(1, buf
, size
);
29 die("git-tar-tree: %s", strerror(errno
));
31 die("git-tar-tree: disk full?");
38 /* writes out the whole block, but only if it is full */
39 static void write_if_needed(void)
41 if (offset
== BLOCKSIZE
) {
42 reliable_write(block
, BLOCKSIZE
);
47 /* acquire the next record from the buffer; user must call write_if_needed() */
48 static char *get_record(void)
50 char *p
= block
+ offset
;
51 memset(p
, 0, RECORDSIZE
);
57 * The end of tar archives is marked by 1024 nul bytes and after that
58 * follows the rest of the block (if any).
60 static void write_trailer(void)
73 * queues up writes, so that all our write(2) calls write exactly one
74 * full block; pads writes to RECORDSIZE
76 static void write_blocked(void *buf
, unsigned long size
)
81 unsigned long chunk
= BLOCKSIZE
- offset
;
84 memcpy(block
+ offset
, buf
, chunk
);
90 while (size
>= BLOCKSIZE
) {
91 reliable_write(buf
, BLOCKSIZE
);
96 memcpy(block
+ offset
, buf
, size
);
99 tail
= offset
% RECORDSIZE
;
101 memset(block
+ offset
, 0, RECORDSIZE
- tail
);
102 offset
+= RECORDSIZE
- tail
;
107 static void strbuf_append_string(struct strbuf
*sb
, const char *s
)
109 int slen
= strlen(s
);
110 int total
= sb
->len
+ slen
;
111 if (total
> sb
->alloc
) {
112 sb
->buf
= xrealloc(sb
->buf
, total
);
115 memcpy(sb
->buf
+ sb
->len
, s
, slen
);
120 * pax extended header records have the format "%u %s=%s\n". %u contains
121 * the size of the whole string (including the %u), the first %s is the
122 * keyword, the second one is the value. This function constructs such a
123 * string and appends it to a struct strbuf.
125 static void strbuf_append_ext_header(struct strbuf
*sb
, const char *keyword
,
126 const char *value
, unsigned int valuelen
)
132 len
= 1 + 1 + strlen(keyword
) + 1 + valuelen
+ 1;
133 for (tmp
= len
; tmp
> 9; tmp
/= 10)
136 total
= sb
->len
+ len
;
137 if (total
> sb
->alloc
) {
138 sb
->buf
= xrealloc(sb
->buf
, total
);
143 p
+= sprintf(p
, "%u %s=", len
, keyword
);
144 memcpy(p
, value
, valuelen
);
150 static unsigned int ustar_header_chksum(const struct ustar_header
*header
)
152 char *p
= (char *)header
;
153 unsigned int chksum
= 0;
154 while (p
< header
->chksum
)
156 chksum
+= sizeof(header
->chksum
) * ' ';
157 p
+= sizeof(header
->chksum
);
158 while (p
< (char *)header
+ sizeof(struct ustar_header
))
163 static int get_path_prefix(const struct strbuf
*path
, int maxlen
)
168 while (i
> 0 && path
->buf
[i
] != '/')
173 static void write_entry(const unsigned char *sha1
, struct strbuf
*path
,
174 unsigned int mode
, void *buffer
, unsigned long size
)
176 struct ustar_header header
;
177 struct strbuf ext_header
;
179 memset(&header
, 0, sizeof(header
));
180 ext_header
.buf
= NULL
;
181 ext_header
.len
= ext_header
.alloc
= 0;
184 *header
.typeflag
= TYPEFLAG_GLOBAL_HEADER
;
186 strcpy(header
.name
, "pax_global_header");
188 *header
.typeflag
= TYPEFLAG_EXT_HEADER
;
190 sprintf(header
.name
, "%s.paxheader", sha1_to_hex(sha1
));
193 *header
.typeflag
= TYPEFLAG_DIR
;
195 } else if (S_ISLNK(mode
)) {
196 *header
.typeflag
= TYPEFLAG_LNK
;
198 } else if (S_ISREG(mode
)) {
199 *header
.typeflag
= TYPEFLAG_REG
;
200 mode
|= (mode
& 0100) ? 0777 : 0666;
202 error("unsupported file mode: 0%o (SHA1: %s)",
203 mode
, sha1_to_hex(sha1
));
206 if (path
->len
> sizeof(header
.name
)) {
207 int plen
= get_path_prefix(path
, sizeof(header
.prefix
));
208 int rest
= path
->len
- plen
- 1;
209 if (plen
> 0 && rest
<= sizeof(header
.name
)) {
210 memcpy(header
.prefix
, path
->buf
, plen
);
211 memcpy(header
.name
, path
->buf
+ plen
+ 1, rest
);
213 sprintf(header
.name
, "%s.data",
215 strbuf_append_ext_header(&ext_header
, "path",
216 path
->buf
, path
->len
);
219 memcpy(header
.name
, path
->buf
, path
->len
);
222 if (S_ISLNK(mode
) && buffer
) {
223 if (size
> sizeof(header
.linkname
)) {
224 sprintf(header
.linkname
, "see %s.paxheader",
226 strbuf_append_ext_header(&ext_header
, "linkpath",
229 memcpy(header
.linkname
, buffer
, size
);
232 sprintf(header
.mode
, "%07o", mode
& 07777);
233 sprintf(header
.size
, "%011lo", S_ISREG(mode
) ? size
: 0);
234 sprintf(header
.mtime
, "%011lo", archive_time
);
236 /* XXX: should we provide more meaningful info here? */
237 sprintf(header
.uid
, "%07o", 0);
238 sprintf(header
.gid
, "%07o", 0);
239 strncpy(header
.uname
, "git", 31);
240 strncpy(header
.gname
, "git", 31);
241 sprintf(header
.devmajor
, "%07o", 0);
242 sprintf(header
.devminor
, "%07o", 0);
244 memcpy(header
.magic
, "ustar", 6);
245 memcpy(header
.version
, "00", 2);
247 sprintf(header
.chksum
, "%07o", ustar_header_chksum(&header
));
249 if (ext_header
.len
> 0) {
250 write_entry(sha1
, NULL
, 0, ext_header
.buf
, ext_header
.len
);
251 free(ext_header
.buf
);
253 write_blocked(&header
, sizeof(header
));
254 if (S_ISREG(mode
) && buffer
&& size
> 0)
255 write_blocked(buffer
, size
);
258 static void write_global_extended_header(const unsigned char *sha1
)
260 struct strbuf ext_header
;
261 ext_header
.buf
= NULL
;
262 ext_header
.len
= ext_header
.alloc
= 0;
263 strbuf_append_ext_header(&ext_header
, "comment", sha1_to_hex(sha1
), 40);
264 write_entry(NULL
, NULL
, 0, ext_header
.buf
, ext_header
.len
);
265 free(ext_header
.buf
);
268 static void traverse_tree(struct tree_desc
*tree
, struct strbuf
*path
)
270 int pathlen
= path
->len
;
274 const unsigned char *sha1
;
278 unsigned long eltsize
;
280 sha1
= tree_entry_extract(tree
, &name
, &mode
);
281 update_tree_entry(tree
);
283 eltbuf
= read_sha1_file(sha1
, elttype
, &eltsize
);
285 die("cannot read %s", sha1_to_hex(sha1
));
288 strbuf_append_string(path
, name
);
290 strbuf_append_string(path
, "/");
292 write_entry(sha1
, path
, mode
, eltbuf
, eltsize
);
295 struct tree_desc subtree
;
296 subtree
.buf
= eltbuf
;
297 subtree
.size
= eltsize
;
298 traverse_tree(&subtree
, path
);
304 int main(int argc
, char **argv
)
306 unsigned char sha1
[20], tree_sha1
[20];
307 struct commit
*commit
;
308 struct tree_desc tree
;
309 struct strbuf current_path
;
311 current_path
.buf
= xmalloc(PATH_MAX
);
312 current_path
.alloc
= PATH_MAX
;
313 current_path
.len
= current_path
.eof
= 0;
315 setup_git_directory();
316 git_config(git_default_config
);
320 strbuf_append_string(¤t_path
, argv
[2]);
321 strbuf_append_string(¤t_path
, "/");
324 if (get_sha1(argv
[1], sha1
) < 0)
325 usage(tar_tree_usage
);
328 usage(tar_tree_usage
);
331 commit
= lookup_commit_reference_gently(sha1
, 1);
333 write_global_extended_header(commit
->object
.sha1
);
334 archive_time
= commit
->date
;
336 archive_time
= time(NULL
);
338 tree
.buf
= read_object_with_reference(sha1
, "tree", &tree
.size
,
341 die("not a reference to a tag, commit or tree object: %s",
344 if (current_path
.len
> 0)
345 write_entry(tree_sha1
, ¤t_path
, 040777, NULL
, 0);
346 traverse_tree(&tree
, ¤t_path
);
348 free(current_path
.buf
);