2005-06-27 11:27:56 +08:00
|
|
|
/*
|
|
|
|
* csum-file.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2005 Linus Torvalds
|
|
|
|
*
|
|
|
|
* Simple file write infrastructure for writing SHA1-summed
|
|
|
|
* files. Useful when you write a file that you want to be
|
|
|
|
* able to verify hasn't been messed with afterwards.
|
|
|
|
*/
|
|
|
|
#include "cache.h"
|
2007-10-31 05:06:21 +08:00
|
|
|
#include "progress.h"
|
2005-06-27 11:27:56 +08:00
|
|
|
#include "csum-file.h"
|
|
|
|
|
2006-08-15 04:32:01 +08:00
|
|
|
static void sha1flush(struct sha1file *f, unsigned int count)
|
2005-06-27 11:27:56 +08:00
|
|
|
{
|
|
|
|
void *buf = f->buffer;
|
|
|
|
|
|
|
|
for (;;) {
|
2005-12-20 08:18:28 +08:00
|
|
|
int ret = xwrite(f->fd, buf, count);
|
2005-06-27 11:27:56 +08:00
|
|
|
if (ret > 0) {
|
2007-10-31 05:06:21 +08:00
|
|
|
display_throughput(f->tp, ret);
|
2006-06-18 23:18:09 +08:00
|
|
|
buf = (char *) buf + ret;
|
2005-06-27 11:27:56 +08:00
|
|
|
count -= ret;
|
|
|
|
if (count)
|
|
|
|
continue;
|
2006-08-15 04:32:01 +08:00
|
|
|
return;
|
2005-06-27 11:27:56 +08:00
|
|
|
}
|
|
|
|
if (!ret)
|
2005-06-27 13:01:46 +08:00
|
|
|
die("sha1 file '%s' write error. Out of diskspace", f->name);
|
|
|
|
die("sha1 file '%s' write error (%s)", f->name, strerror(errno));
|
2005-06-27 11:27:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-14 02:28:19 +08:00
|
|
|
int sha1close(struct sha1file *f, unsigned char *result, int final)
|
2005-06-27 11:27:56 +08:00
|
|
|
{
|
2007-10-17 09:55:48 +08:00
|
|
|
int fd;
|
2005-06-27 11:27:56 +08:00
|
|
|
unsigned offset = f->offset;
|
|
|
|
if (offset) {
|
|
|
|
SHA1_Update(&f->ctx, f->buffer, offset);
|
|
|
|
sha1flush(f, offset);
|
2007-05-14 02:28:19 +08:00
|
|
|
f->offset = 0;
|
2005-06-27 11:27:56 +08:00
|
|
|
}
|
2007-10-17 09:55:48 +08:00
|
|
|
if (final) {
|
|
|
|
/* write checksum and close fd */
|
|
|
|
SHA1_Final(f->buffer, &f->ctx);
|
|
|
|
if (result)
|
|
|
|
hashcpy(result, f->buffer);
|
|
|
|
sha1flush(f, 20);
|
|
|
|
if (close(f->fd))
|
|
|
|
die("%s: sha1 file error on close (%s)",
|
|
|
|
f->name, strerror(errno));
|
|
|
|
fd = 0;
|
|
|
|
} else
|
|
|
|
fd = f->fd;
|
2005-08-09 02:46:13 +08:00
|
|
|
free(f);
|
2007-10-17 09:55:48 +08:00
|
|
|
return fd;
|
2005-06-27 11:27:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int sha1write(struct sha1file *f, void *buf, unsigned int count)
|
|
|
|
{
|
compute a CRC32 for each object as stored in a pack
The most important optimization for performance when repacking is the
ability to reuse data from a previous pack as is and bypass any delta
or even SHA1 computation by simply copying the raw data from one pack
to another directly.
The problem with this is that any data corruption within a copied object
would go unnoticed and the new (repacked) pack would be self-consistent
with its own checksum despite containing a corrupted object. This is a
real issue that already happened at least once in the past.
In some attempt to prevent this, we validate the copied data by inflating
it and making sure no error is signaled by zlib. But this is still not
perfect as a significant portion of a pack content is made of object
headers and references to delta base objects which are not deflated and
therefore not validated when repacking actually making the pack data reuse
still not as safe as it could be.
Of course a full SHA1 validation could be performed, but that implies
full data inflating and delta replaying which is extremely costly, which
cost the data reuse optimization was designed to avoid in the first place.
So the best solution to this is simply to store a CRC32 of the raw pack
data for each object in the pack index. This way any object in a pack can
be validated before being copied as is in another pack, including header
and any other non deflated data.
Why CRC32 instead of a faster checksum like Adler32? Quoting Wikipedia:
Jonathan Stone discovered in 2001 that Adler-32 has a weakness for very
short messages. He wrote "Briefly, the problem is that, for very short
packets, Adler32 is guaranteed to give poor coverage of the available
bits. Don't take my word for it, ask Mark Adler. :-)" The problem is
that sum A does not wrap for short messages. The maximum value of A for
a 128-byte message is 32640, which is below the value 65521 used by the
modulo operation. An extended explanation can be found in RFC 3309,
which mandates the use of CRC32 instead of Adler-32 for SCTP, the
Stream Control Transmission Protocol.
In the context of a GIT pack, we have lots of small objects, especially
deltas, which are likely to be quite small and in a size range for which
Adler32 is dimed not to be sufficient. Another advantage of CRC32 is the
possibility for recovery from certain types of small corruptions like
single bit errors which are the most probable type of corruptions.
OK what this patch does is to compute the CRC32 of each object written to
a pack within pack-objects. It is not written to the index yet and it is
obviously not validated when reusing pack data yet either.
Signed-off-by: Nicolas Pitre <nico@cam.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2007-04-09 13:06:31 +08:00
|
|
|
if (f->do_crc)
|
|
|
|
f->crc32 = crc32(f->crc32, buf, count);
|
2005-06-27 11:27:56 +08:00
|
|
|
while (count) {
|
|
|
|
unsigned offset = f->offset;
|
|
|
|
unsigned left = sizeof(f->buffer) - offset;
|
|
|
|
unsigned nr = count > left ? left : count;
|
|
|
|
|
|
|
|
memcpy(f->buffer + offset, buf, nr);
|
|
|
|
count -= nr;
|
|
|
|
offset += nr;
|
2006-06-18 23:18:09 +08:00
|
|
|
buf = (char *) buf + nr;
|
2005-06-27 11:27:56 +08:00
|
|
|
left -= nr;
|
|
|
|
if (!left) {
|
|
|
|
SHA1_Update(&f->ctx, f->buffer, offset);
|
|
|
|
sha1flush(f, offset);
|
|
|
|
offset = 0;
|
|
|
|
}
|
|
|
|
f->offset = offset;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-06-29 02:10:06 +08:00
|
|
|
struct sha1file *sha1fd(int fd, const char *name)
|
2007-10-31 05:06:21 +08:00
|
|
|
{
|
|
|
|
return sha1fd_throughput(fd, name, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sha1file *sha1fd_throughput(int fd, const char *name, struct progress *tp)
|
2005-06-29 02:10:06 +08:00
|
|
|
{
|
|
|
|
struct sha1file *f;
|
|
|
|
unsigned len;
|
|
|
|
|
|
|
|
f = xmalloc(sizeof(*f));
|
|
|
|
|
|
|
|
len = strlen(name);
|
|
|
|
if (len >= PATH_MAX)
|
|
|
|
die("you wascally wabbit, you");
|
|
|
|
f->namelen = len;
|
|
|
|
memcpy(f->name, name, len+1);
|
|
|
|
|
|
|
|
f->fd = fd;
|
|
|
|
f->error = 0;
|
|
|
|
f->offset = 0;
|
2007-10-31 05:06:21 +08:00
|
|
|
f->tp = tp;
|
compute a CRC32 for each object as stored in a pack
The most important optimization for performance when repacking is the
ability to reuse data from a previous pack as is and bypass any delta
or even SHA1 computation by simply copying the raw data from one pack
to another directly.
The problem with this is that any data corruption within a copied object
would go unnoticed and the new (repacked) pack would be self-consistent
with its own checksum despite containing a corrupted object. This is a
real issue that already happened at least once in the past.
In some attempt to prevent this, we validate the copied data by inflating
it and making sure no error is signaled by zlib. But this is still not
perfect as a significant portion of a pack content is made of object
headers and references to delta base objects which are not deflated and
therefore not validated when repacking actually making the pack data reuse
still not as safe as it could be.
Of course a full SHA1 validation could be performed, but that implies
full data inflating and delta replaying which is extremely costly, which
cost the data reuse optimization was designed to avoid in the first place.
So the best solution to this is simply to store a CRC32 of the raw pack
data for each object in the pack index. This way any object in a pack can
be validated before being copied as is in another pack, including header
and any other non deflated data.
Why CRC32 instead of a faster checksum like Adler32? Quoting Wikipedia:
Jonathan Stone discovered in 2001 that Adler-32 has a weakness for very
short messages. He wrote "Briefly, the problem is that, for very short
packets, Adler32 is guaranteed to give poor coverage of the available
bits. Don't take my word for it, ask Mark Adler. :-)" The problem is
that sum A does not wrap for short messages. The maximum value of A for
a 128-byte message is 32640, which is below the value 65521 used by the
modulo operation. An extended explanation can be found in RFC 3309,
which mandates the use of CRC32 instead of Adler-32 for SCTP, the
Stream Control Transmission Protocol.
In the context of a GIT pack, we have lots of small objects, especially
deltas, which are likely to be quite small and in a size range for which
Adler32 is dimed not to be sufficient. Another advantage of CRC32 is the
possibility for recovery from certain types of small corruptions like
single bit errors which are the most probable type of corruptions.
OK what this patch does is to compute the CRC32 of each object written to
a pack within pack-objects. It is not written to the index yet and it is
obviously not validated when reusing pack data yet either.
Signed-off-by: Nicolas Pitre <nico@cam.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2007-04-09 13:06:31 +08:00
|
|
|
f->do_crc = 0;
|
2005-06-29 02:10:06 +08:00
|
|
|
SHA1_Init(&f->ctx);
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
compute a CRC32 for each object as stored in a pack
The most important optimization for performance when repacking is the
ability to reuse data from a previous pack as is and bypass any delta
or even SHA1 computation by simply copying the raw data from one pack
to another directly.
The problem with this is that any data corruption within a copied object
would go unnoticed and the new (repacked) pack would be self-consistent
with its own checksum despite containing a corrupted object. This is a
real issue that already happened at least once in the past.
In some attempt to prevent this, we validate the copied data by inflating
it and making sure no error is signaled by zlib. But this is still not
perfect as a significant portion of a pack content is made of object
headers and references to delta base objects which are not deflated and
therefore not validated when repacking actually making the pack data reuse
still not as safe as it could be.
Of course a full SHA1 validation could be performed, but that implies
full data inflating and delta replaying which is extremely costly, which
cost the data reuse optimization was designed to avoid in the first place.
So the best solution to this is simply to store a CRC32 of the raw pack
data for each object in the pack index. This way any object in a pack can
be validated before being copied as is in another pack, including header
and any other non deflated data.
Why CRC32 instead of a faster checksum like Adler32? Quoting Wikipedia:
Jonathan Stone discovered in 2001 that Adler-32 has a weakness for very
short messages. He wrote "Briefly, the problem is that, for very short
packets, Adler32 is guaranteed to give poor coverage of the available
bits. Don't take my word for it, ask Mark Adler. :-)" The problem is
that sum A does not wrap for short messages. The maximum value of A for
a 128-byte message is 32640, which is below the value 65521 used by the
modulo operation. An extended explanation can be found in RFC 3309,
which mandates the use of CRC32 instead of Adler-32 for SCTP, the
Stream Control Transmission Protocol.
In the context of a GIT pack, we have lots of small objects, especially
deltas, which are likely to be quite small and in a size range for which
Adler32 is dimed not to be sufficient. Another advantage of CRC32 is the
possibility for recovery from certain types of small corruptions like
single bit errors which are the most probable type of corruptions.
OK what this patch does is to compute the CRC32 of each object written to
a pack within pack-objects. It is not written to the index yet and it is
obviously not validated when reusing pack data yet either.
Signed-off-by: Nicolas Pitre <nico@cam.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2007-04-09 13:06:31 +08:00
|
|
|
void crc32_begin(struct sha1file *f)
|
|
|
|
{
|
|
|
|
f->crc32 = crc32(0, Z_NULL, 0);
|
|
|
|
f->do_crc = 1;
|
|
|
|
}
|
2005-06-27 11:27:56 +08:00
|
|
|
|
compute a CRC32 for each object as stored in a pack
The most important optimization for performance when repacking is the
ability to reuse data from a previous pack as is and bypass any delta
or even SHA1 computation by simply copying the raw data from one pack
to another directly.
The problem with this is that any data corruption within a copied object
would go unnoticed and the new (repacked) pack would be self-consistent
with its own checksum despite containing a corrupted object. This is a
real issue that already happened at least once in the past.
In some attempt to prevent this, we validate the copied data by inflating
it and making sure no error is signaled by zlib. But this is still not
perfect as a significant portion of a pack content is made of object
headers and references to delta base objects which are not deflated and
therefore not validated when repacking actually making the pack data reuse
still not as safe as it could be.
Of course a full SHA1 validation could be performed, but that implies
full data inflating and delta replaying which is extremely costly, which
cost the data reuse optimization was designed to avoid in the first place.
So the best solution to this is simply to store a CRC32 of the raw pack
data for each object in the pack index. This way any object in a pack can
be validated before being copied as is in another pack, including header
and any other non deflated data.
Why CRC32 instead of a faster checksum like Adler32? Quoting Wikipedia:
Jonathan Stone discovered in 2001 that Adler-32 has a weakness for very
short messages. He wrote "Briefly, the problem is that, for very short
packets, Adler32 is guaranteed to give poor coverage of the available
bits. Don't take my word for it, ask Mark Adler. :-)" The problem is
that sum A does not wrap for short messages. The maximum value of A for
a 128-byte message is 32640, which is below the value 65521 used by the
modulo operation. An extended explanation can be found in RFC 3309,
which mandates the use of CRC32 instead of Adler-32 for SCTP, the
Stream Control Transmission Protocol.
In the context of a GIT pack, we have lots of small objects, especially
deltas, which are likely to be quite small and in a size range for which
Adler32 is dimed not to be sufficient. Another advantage of CRC32 is the
possibility for recovery from certain types of small corruptions like
single bit errors which are the most probable type of corruptions.
OK what this patch does is to compute the CRC32 of each object written to
a pack within pack-objects. It is not written to the index yet and it is
obviously not validated when reusing pack data yet either.
Signed-off-by: Nicolas Pitre <nico@cam.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
2007-04-09 13:06:31 +08:00
|
|
|
uint32_t crc32_end(struct sha1file *f)
|
|
|
|
{
|
|
|
|
f->do_crc = 0;
|
|
|
|
return f->crc32;
|
|
|
|
}
|