Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * csum-file.c |
| 3 | * |
| 4 | * Copyright (C) 2005 Linus Torvalds |
| 5 | * |
| 6 | * Simple file write infrastructure for writing SHA1-summed |
| 7 | * files. Useful when you write a file that you want to be |
| 8 | * able to verify hasn't been messed with afterwards. |
| 9 | */ |
| 10 | #include "cache.h" |
Nicolas Pitre | 2a128d6 | 2007-10-30 17:06:21 -0400 | [diff] [blame] | 11 | #include "progress.h" |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 12 | #include "csum-file.h" |
| 13 | |
Shawn O. Pearce | e782e12 | 2008-10-10 08:39:20 -0700 | [diff] [blame^] | 14 | static void flush(struct sha1file *f, void * buf, unsigned int count) |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 15 | { |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 16 | for (;;) { |
Junio C Hamano | 1c15afb | 2005-12-19 16:18:28 -0800 | [diff] [blame] | 17 | int ret = xwrite(f->fd, buf, count); |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 18 | if (ret > 0) { |
Nicolas Pitre | 218558a | 2007-11-04 22:15:41 -0500 | [diff] [blame] | 19 | f->total += ret; |
| 20 | display_throughput(f->tp, f->total); |
Florian Forster | 1d7f171 | 2006-06-18 17:18:09 +0200 | [diff] [blame] | 21 | buf = (char *) buf + ret; |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 22 | count -= ret; |
| 23 | if (count) |
| 24 | continue; |
David Rientjes | 78b713a | 2006-08-14 13:32:01 -0700 | [diff] [blame] | 25 | return; |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 26 | } |
| 27 | if (!ret) |
Linus Torvalds | e180884 | 2005-06-26 22:01:46 -0700 | [diff] [blame] | 28 | die("sha1 file '%s' write error. Out of diskspace", f->name); |
Linus Torvalds | e180884 | 2005-06-26 22:01:46 -0700 | [diff] [blame] | 29 | die("sha1 file '%s' write error (%s)", f->name, strerror(errno)); |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 30 | } |
| 31 | } |
| 32 | |
Nicolas Pitre | 838cd34 | 2008-10-09 22:08:51 -0400 | [diff] [blame] | 33 | void sha1flush(struct sha1file *f) |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 34 | { |
| 35 | unsigned offset = f->offset; |
Linus Torvalds | 4c81b03 | 2008-05-30 08:42:16 -0700 | [diff] [blame] | 36 | |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 37 | if (offset) { |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 38 | git_SHA1_Update(&f->ctx, f->buffer, offset); |
Shawn O. Pearce | e782e12 | 2008-10-10 08:39:20 -0700 | [diff] [blame^] | 39 | flush(f, f->buffer, offset); |
Dana L. How | f021536 | 2007-05-13 11:28:19 -0700 | [diff] [blame] | 40 | f->offset = 0; |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 41 | } |
Nicolas Pitre | 838cd34 | 2008-10-09 22:08:51 -0400 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | int sha1close(struct sha1file *f, unsigned char *result, unsigned int flags) |
| 45 | { |
| 46 | int fd; |
| 47 | |
| 48 | sha1flush(f); |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 49 | git_SHA1_Final(f->buffer, &f->ctx); |
Nicolas Pitre | ac0463e | 2008-08-29 16:08:00 -0400 | [diff] [blame] | 50 | if (result) |
| 51 | hashcpy(result, f->buffer); |
Linus Torvalds | 4c81b03 | 2008-05-30 08:42:16 -0700 | [diff] [blame] | 52 | if (flags & (CSUM_CLOSE | CSUM_FSYNC)) { |
Nicolas Pitre | 7ba502c | 2007-10-16 21:55:48 -0400 | [diff] [blame] | 53 | /* write checksum and close fd */ |
Shawn O. Pearce | e782e12 | 2008-10-10 08:39:20 -0700 | [diff] [blame^] | 54 | flush(f, f->buffer, 20); |
Linus Torvalds | 4c81b03 | 2008-05-30 08:42:16 -0700 | [diff] [blame] | 55 | if (flags & CSUM_FSYNC) |
| 56 | fsync_or_die(f->fd, f->name); |
Nicolas Pitre | 7ba502c | 2007-10-16 21:55:48 -0400 | [diff] [blame] | 57 | if (close(f->fd)) |
| 58 | die("%s: sha1 file error on close (%s)", |
| 59 | f->name, strerror(errno)); |
| 60 | fd = 0; |
| 61 | } else |
| 62 | fd = f->fd; |
Sergey Vlasov | 7bf058f | 2005-08-08 22:46:13 +0400 | [diff] [blame] | 63 | free(f); |
Nicolas Pitre | 7ba502c | 2007-10-16 21:55:48 -0400 | [diff] [blame] | 64 | return fd; |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | int sha1write(struct sha1file *f, void *buf, unsigned int count) |
| 68 | { |
| 69 | while (count) { |
| 70 | unsigned offset = f->offset; |
| 71 | unsigned left = sizeof(f->buffer) - offset; |
| 72 | unsigned nr = count > left ? left : count; |
Nicolas Pitre | a8032d1 | 2008-09-02 10:22:20 -0400 | [diff] [blame] | 73 | void *data; |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 74 | |
Nicolas Pitre | a8032d1 | 2008-09-02 10:22:20 -0400 | [diff] [blame] | 75 | if (f->do_crc) |
| 76 | f->crc32 = crc32(f->crc32, buf, nr); |
| 77 | |
| 78 | if (nr == sizeof(f->buffer)) { |
| 79 | /* process full buffer directly without copy */ |
| 80 | data = buf; |
| 81 | } else { |
| 82 | memcpy(f->buffer + offset, buf, nr); |
| 83 | data = f->buffer; |
| 84 | } |
| 85 | |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 86 | count -= nr; |
| 87 | offset += nr; |
Florian Forster | 1d7f171 | 2006-06-18 17:18:09 +0200 | [diff] [blame] | 88 | buf = (char *) buf + nr; |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 89 | left -= nr; |
| 90 | if (!left) { |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 91 | git_SHA1_Update(&f->ctx, data, offset); |
Shawn O. Pearce | e782e12 | 2008-10-10 08:39:20 -0700 | [diff] [blame^] | 92 | flush(f, data, offset); |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 93 | offset = 0; |
| 94 | } |
| 95 | f->offset = offset; |
| 96 | } |
| 97 | return 0; |
| 98 | } |
| 99 | |
Linus Torvalds | 4397f01 | 2005-06-28 11:10:06 -0700 | [diff] [blame] | 100 | struct sha1file *sha1fd(int fd, const char *name) |
| 101 | { |
Nicolas Pitre | 2a128d6 | 2007-10-30 17:06:21 -0400 | [diff] [blame] | 102 | return sha1fd_throughput(fd, name, NULL); |
| 103 | } |
| 104 | |
| 105 | struct sha1file *sha1fd_throughput(int fd, const char *name, struct progress *tp) |
| 106 | { |
Nicolas Pitre | ec640ed | 2007-11-04 22:54:50 -0500 | [diff] [blame] | 107 | struct sha1file *f = xmalloc(sizeof(*f)); |
Linus Torvalds | 4397f01 | 2005-06-28 11:10:06 -0700 | [diff] [blame] | 108 | f->fd = fd; |
Linus Torvalds | 4397f01 | 2005-06-28 11:10:06 -0700 | [diff] [blame] | 109 | f->offset = 0; |
Nicolas Pitre | 218558a | 2007-11-04 22:15:41 -0500 | [diff] [blame] | 110 | f->total = 0; |
Nicolas Pitre | 2a128d6 | 2007-10-30 17:06:21 -0400 | [diff] [blame] | 111 | f->tp = tp; |
Nicolas Pitre | ec640ed | 2007-11-04 22:54:50 -0500 | [diff] [blame] | 112 | f->name = name; |
Nicolas Pitre | 78d1e84 | 2007-04-09 01:06:31 -0400 | [diff] [blame] | 113 | f->do_crc = 0; |
Nicolas Pitre | 9126f00 | 2008-10-01 14:05:20 -0400 | [diff] [blame] | 114 | git_SHA1_Init(&f->ctx); |
Linus Torvalds | 4397f01 | 2005-06-28 11:10:06 -0700 | [diff] [blame] | 115 | return f; |
| 116 | } |
| 117 | |
Nicolas Pitre | 78d1e84 | 2007-04-09 01:06:31 -0400 | [diff] [blame] | 118 | void crc32_begin(struct sha1file *f) |
| 119 | { |
| 120 | f->crc32 = crc32(0, Z_NULL, 0); |
| 121 | f->do_crc = 1; |
| 122 | } |
Linus Torvalds | c38138c | 2005-06-26 20:27:56 -0700 | [diff] [blame] | 123 | |
Nicolas Pitre | 78d1e84 | 2007-04-09 01:06:31 -0400 | [diff] [blame] | 124 | uint32_t crc32_end(struct sha1file *f) |
| 125 | { |
| 126 | f->do_crc = 0; |
| 127 | return f->crc32; |
| 128 | } |