2005-06-27 07:27:56 +04:00
|
|
|
#ifndef CSUM_FILE_H
|
|
|
|
#define CSUM_FILE_H
|
|
|
|
|
2007-10-31 00:06:21 +03:00
|
|
|
struct progress;
|
|
|
|
|
2005-06-27 07:27:56 +04:00
|
|
|
/* A SHA1-protected file */
|
|
|
|
struct sha1file {
|
2007-11-05 06:54:50 +03:00
|
|
|
int fd;
|
2011-02-03 04:29:01 +03:00
|
|
|
int check_fd;
|
2007-11-05 06:54:50 +03:00
|
|
|
unsigned int offset;
|
2008-10-01 22:05:20 +04:00
|
|
|
git_SHA_CTX ctx;
|
2007-11-05 06:15:41 +03:00
|
|
|
off_t total;
|
2007-10-31 00:06:21 +03:00
|
|
|
struct progress *tp;
|
2007-11-05 06:54:50 +03:00
|
|
|
const char *name;
|
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 09:06:31 +04:00
|
|
|
int do_crc;
|
|
|
|
uint32_t crc32;
|
2005-06-27 07:27:56 +04:00
|
|
|
unsigned char buffer[8192];
|
|
|
|
};
|
|
|
|
|
2011-11-18 04:26:54 +04:00
|
|
|
/* Checkpoint */
|
|
|
|
struct sha1file_checkpoint {
|
|
|
|
off_t offset;
|
|
|
|
git_SHA_CTX ctx;
|
|
|
|
};
|
|
|
|
|
|
|
|
extern void sha1file_checkpoint(struct sha1file *, struct sha1file_checkpoint *);
|
|
|
|
extern int sha1file_truncate(struct sha1file *, struct sha1file_checkpoint *);
|
|
|
|
|
2008-05-30 19:42:16 +04:00
|
|
|
/* sha1close flags */
|
|
|
|
#define CSUM_CLOSE 1
|
|
|
|
#define CSUM_FSYNC 2
|
|
|
|
|
2005-06-28 22:10:06 +04:00
|
|
|
extern struct sha1file *sha1fd(int fd, const char *name);
|
2011-02-03 04:29:01 +03:00
|
|
|
extern struct sha1file *sha1fd_check(const char *name);
|
2007-10-31 00:06:21 +03:00
|
|
|
extern struct sha1file *sha1fd_throughput(int fd, const char *name, struct progress *tp);
|
2008-05-30 19:42:16 +04:00
|
|
|
extern int sha1close(struct sha1file *, unsigned char *, unsigned int);
|
2014-01-10 22:33:09 +04:00
|
|
|
extern void sha1write(struct sha1file *, const void *, unsigned int);
|
2008-10-10 06:08:51 +04:00
|
|
|
extern void sha1flush(struct sha1file *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 09:06:31 +04:00
|
|
|
extern void crc32_begin(struct sha1file *);
|
|
|
|
extern uint32_t crc32_end(struct sha1file *);
|
2005-06-27 07:27:56 +04:00
|
|
|
|
2014-11-27 08:24:01 +03:00
|
|
|
static inline void sha1write_u8(struct sha1file *f, uint8_t data)
|
|
|
|
{
|
|
|
|
sha1write(f, &data, sizeof(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void sha1write_be32(struct sha1file *f, uint32_t data)
|
|
|
|
{
|
|
|
|
data = htonl(data);
|
|
|
|
sha1write(f, &data, sizeof(data));
|
|
|
|
}
|
|
|
|
|
2005-06-27 07:27:56 +04:00
|
|
|
#endif
|