git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/5] archive-tar: turn write_tar_entry into blob-writing only

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Apr 30, 2012, 04:57 UTC
Message-ID
<1335761837-12482-2-git-send-email-pclouds@gmail.com>
In-Reply-To
<1335761837-12482-1-git-send-email-pclouds@gmail.com>
Before this patch write_tar_entry() can:
 - write global header
   by write_global_extended_header() calling write_tar_entry with
   with both sha1 and path == NULL
 - write extended header for symlinks, by write_tar_entry() calling
   itself with sha1 != NULL and path == NULL
 - write a normal blob. In this case both sha1 and path are valid.

After this patch, the first two call sites are modified to write the header without calling write_tar_entry(). The function is now for writing blobs only. This simplifies handling when write_tar_entry() learns about large blobs.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 archive-tar.c |   78 ++++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 52 insertions(+), 26 deletions(-)
diff --git a/archive-tar.c b/archive-tar.c
index 20af005..1727ab9 100644
--- a/archive-tar.c
+++ b/archive-tar.c
@@ -123,6 +123,43 @@ static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen)
 	return i;
 }
 
+static void prepare_header(struct archiver_args *args,
+			   struct ustar_header *header,
+			   unsigned int mode, unsigned long size)
+{
+	sprintf(header->mode, "%07o", mode & 07777);
+	sprintf(header->size, "%011lo", S_ISREG(mode) ? size : 0);
+	sprintf(header->mtime, "%011lo", (unsigned long) args->time);
+
+	sprintf(header->uid, "%07o", 0);
+	sprintf(header->gid, "%07o", 0);
+	strlcpy(header->uname, "root", sizeof(header->uname));
+	strlcpy(header->gname, "root", sizeof(header->gname));
+	sprintf(header->devmajor, "%07o", 0);
+	sprintf(header->devminor, "%07o", 0);
+
+	memcpy(header->magic, "ustar", 6);
+	memcpy(header->version, "00", 2);
+
+	sprintf(header->chksum, "%07o", ustar_header_chksum(header));
+}
+
+static int write_extended_header(struct archiver_args *args,
+				 const unsigned char *sha1,
+				 const void *buffer, unsigned long size)
+{
+	struct ustar_header header;
+	unsigned int mode;
+	memset(&header, 0, sizeof(header));
+	*header.typeflag = TYPEFLAG_EXT_HEADER;
+	mode = 0100666;
+	sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
+	prepare_header(args, &header, mode, size);
+	write_blocked(&header, sizeof(header));
+	write_blocked(buffer, size);
+	return 0;
+}
+
 static int write_tar_entry(struct archiver_args *args,
 		const unsigned char *sha1, const char *path, size_t pathlen,
 		unsigned int mode, void *buffer, unsigned long size)
@@ -134,13 +171,9 @@ static int write_tar_entry(struct archiver_args *args,
 	memset(&header, 0, sizeof(header));
 
 	if (!sha1) {
-		*header.typeflag = TYPEFLAG_GLOBAL_HEADER;
-		mode = 0100666;
-		strcpy(header.name, "pax_global_header");
+		die("BUG: sha1 == NULL is not supported");
 	} else if (!path) {
-		*header.typeflag = TYPEFLAG_EXT_HEADER;
-		mode = 0100666;
-		sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
+		die("BUG: path == NULL is not supported");
 	} else {
 		if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
 			*header.typeflag = TYPEFLAG_DIR;
@@ -182,25 +215,11 @@ static int write_tar_entry(struct archiver_args *args,
 			memcpy(header.linkname, buffer, size);
 	}
 
-	sprintf(header.mode, "%07o", mode & 07777);
-	sprintf(header.size, "%011lo", S_ISREG(mode) ? size : 0);
-	sprintf(header.mtime, "%011lo", (unsigned long) args->time);
-
-	sprintf(header.uid, "%07o", 0);
-	sprintf(header.gid, "%07o", 0);
-	strlcpy(header.uname, "root", sizeof(header.uname));
-	strlcpy(header.gname, "root", sizeof(header.gname));
-	sprintf(header.devmajor, "%07o", 0);
-	sprintf(header.devminor, "%07o", 0);
-
-	memcpy(header.magic, "ustar", 6);
-	memcpy(header.version, "00", 2);
-
-	sprintf(header.chksum, "%07o", ustar_header_chksum(&header));
+	prepare_header(args, &header, mode, size);
 
 	if (ext_header.len > 0) {
-		err = write_tar_entry(args, sha1, NULL, 0, 0, ext_header.buf,
-				ext_header.len);
+		err = write_extended_header(args, sha1, ext_header.buf,
+					    ext_header.len);
 		if (err)
 			return err;
 	}
@@ -215,11 +234,18 @@ static int write_global_extended_header(struct archiver_args *args)
 {
 	const unsigned char *sha1 = args->commit_sha1;
 	struct strbuf ext_header = STRBUF_INIT;
-	int err;
+	struct ustar_header header;
+	unsigned int mode;
+	int err = 0;
 
 	strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
-	err = write_tar_entry(args, NULL, NULL, 0, 0, ext_header.buf,
-			ext_header.len);
+	memset(&header, 0, sizeof(header));
+	*header.typeflag = TYPEFLAG_GLOBAL_HEADER;
+	mode = 0100666;
+	strcpy(header.name, "pax_global_header");
+	prepare_header(args, &header, mode, ext_header.len);
+	write_blocked(&header, sizeof(header));
+	write_blocked(ext_header.buf, ext_header.len);
 	strbuf_release(&ext_header);
 	return err;
 }
-- 
1.7.8.36.g69ee2
Previous: Nguyễn Thái Ngọc DuyNext: Junio C Hamano
Message 2 of 23 in “Large file support for git-archive”
  1. 0/5 Large file support for git-archiveNguyễn Thái Ngọc Duy, Apr 30, 2012
  2. 1/5 archive-tar: turn write_tar_entry into blob-writing onlyNguyễn Thái Ngọc Duy, Apr 30, 2012
  3. Junio C HamanoApr 30, 2012
  4. René ScharfeApr 30, 2012
  5. 2/5 archive-tar: unindent write_tar_entry by one levelNguyễn Thái Ngọc Duy, Apr 30, 2012
  6. 3/5 archive: delegate blob reading to backendNguyễn Thái Ngọc Duy, Apr 30, 2012
  7. René ScharfeApr 30, 2012
  8. 4/5 archive-tar: stream large blobs to tar fileNguyễn Thái Ngọc Duy, Apr 30, 2012
  9. Junio C HamanoApr 30, 2012
  10. René ScharfeApr 30, 2012
  11. Junio C HamanoApr 30, 2012
  12. René ScharfeApr 30, 2012
  13. 5/5 archive-zip: stream large blobs into zip fileNguyễn Thái Ngọc Duy, Apr 30, 2012
  14. Junio C HamanoApr 30, 2012
  15. René ScharfeApr 30, 2012
  16. streaming: void pointer instead of char pointerRené Scharfe, Apr 30, 2012
  17. archive-zip: remove uncompressed_sizeRené Scharfe, Apr 30, 2012
  18. archive-zip: factor out helpers for writing sizes and CRCRené Scharfe, Apr 30, 2012
  19. archive-zip: streaming for stored filesRené Scharfe, Apr 30, 2012
  20. archive-zip: streaming for deflated filesRené Scharfe, Apr 30, 2012
  21. Junio C HamanoApr 30, 2012
  22. René ScharfeApr 30, 2012
  23. Nguyen Thai Ngoc DuyMay 1, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.