Skip to content

Commit 3607c27

Browse files
author
Linus Torvalds
committed
Allow zero-sized files to be checked in.
The kernel may not want it, but others probably do. Noted (again) by Junio Hamano.
1 parent 771364a commit 3607c27

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

update-cache.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@ static int allow_add = 0, allow_remove = 0;
1717
static int index_fd(const char *path, int namelen, struct cache_entry *ce, int fd, struct stat *st)
1818
{
1919
z_stream stream;
20-
int max_out_bytes = namelen + st->st_size + 200;
20+
unsigned long size = st->st_size;
21+
int max_out_bytes = namelen + size + 200;
2122
void *out = malloc(max_out_bytes);
2223
void *metadata = malloc(namelen + 200);
23-
void *in = mmap(NULL, st->st_size, PROT_READ, MAP_PRIVATE, fd, 0);
24+
void *in;
2425
SHA_CTX c;
2526

27+
in = "";
28+
if (size)
29+
in = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2630
close(fd);
2731
if (!out || (int)(long)in == -1)
2832
return -1;
@@ -34,7 +38,7 @@ static int index_fd(const char *path, int namelen, struct cache_entry *ce, int f
3438
* ASCII size + nul byte
3539
*/
3640
stream.next_in = metadata;
37-
stream.avail_in = 1+sprintf(metadata, "blob %lu", (unsigned long) st->st_size);
41+
stream.avail_in = 1+sprintf(metadata, "blob %lu", size);
3842
stream.next_out = out;
3943
stream.avail_out = max_out_bytes;
4044
while (deflate(&stream, 0) == Z_OK)
@@ -44,7 +48,7 @@ static int index_fd(const char *path, int namelen, struct cache_entry *ce, int f
4448
* File content
4549
*/
4650
stream.next_in = in;
47-
stream.avail_in = st->st_size;
51+
stream.avail_in = size;
4852
while (deflate(&stream, Z_FINISH) == Z_OK)
4953
/*nothing */;
5054

0 commit comments

Comments
 (0)