From ccc4feb579265266d0a4a73c0c9443ecc0c26ce3 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Fri, 15 Apr 2005 10:44:27 -0700 Subject: [PATCH] Convert the index file reading/writing to use network byte order. This allows using a git tree over NFS with different byte order, and makes it possible to just copy a fully populated repository and have the end result immediately usable (needing just a refresh to update the stat information). --- cache.h | 31 ++++++++++++++++----------- checkout-cache.c | 2 +- read-cache.c | 56 +++++++++++++++++++++++++----------------------- read-tree.c | 4 ++-- show-diff.c | 2 +- update-cache.c | 25 ++++++++++----------- write-tree.c | 4 ++-- 7 files changed, 64 insertions(+), 60 deletions(-) diff --git a/cache.h b/cache.h index 35b26c7d8..5b3cd95aa 100644 --- a/cache.h +++ b/cache.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -24,9 +25,9 @@ #define CACHE_SIGNATURE 0x44495243 /* "DIRC" */ struct cache_header { - unsigned int signature; - unsigned int version; - unsigned int entries; + unsigned int hdr_signature; + unsigned int hdr_version; + unsigned int hdr_entries; unsigned char sha1[20]; }; @@ -44,18 +45,21 @@ struct cache_time { * dev/ino/uid/gid/size are also just tracked to the low 32 bits * Again - this is just a (very strong in practice) heuristic that * the inode hasn't changed. + * + * We save the fields in big-endian order to allow using the + * index file over NFS transparently. */ struct cache_entry { - struct cache_time ctime; - struct cache_time mtime; - unsigned int st_dev; - unsigned int st_ino; - unsigned int st_mode; - unsigned int st_uid; - unsigned int st_gid; - unsigned int st_size; + struct cache_time ce_ctime; + struct cache_time ce_mtime; + unsigned int ce_dev; + unsigned int ce_ino; + unsigned int ce_mode; + unsigned int ce_uid; + unsigned int ce_gid; + unsigned int ce_size; unsigned char sha1[20]; - unsigned short namelen; + unsigned short ce_namelen; char name[0]; }; @@ -67,7 +71,8 @@ unsigned int active_nr, active_alloc; #define DEFAULT_DB_ENVIRONMENT ".git/objects" #define cache_entry_size(len) ((offsetof(struct cache_entry,name) + (len) + 8) & ~7) -#define ce_size(ce) cache_entry_size((ce)->namelen) +#define ce_namelen(ce) ntohs((ce)->ce_namelen) +#define ce_size(ce) cache_entry_size(ce_namelen(ce)) #define alloc_nr(x) (((x)+16)*3/2) diff --git a/checkout-cache.c b/checkout-cache.c index fa7864590..8d5e4cd14 100644 --- a/checkout-cache.c +++ b/checkout-cache.c @@ -77,7 +77,7 @@ static int write_entry(struct cache_entry *ce) return error("checkout-cache: unable to read sha1 file of %s (%s)", ce->name, sha1_to_hex(ce->sha1)); } - fd = create_file(ce->name, ce->st_mode); + fd = create_file(ce->name, ntohl(ce->ce_mode)); if (fd < 0) { free(new); return error("checkout-cache: unable to create %s (%s)", diff --git a/read-cache.c b/read-cache.c index 8e90d2c3b..4173b4e45 100644 --- a/read-cache.c +++ b/read-cache.c @@ -283,30 +283,32 @@ int cache_match_stat(struct cache_entry *ce, struct stat *st) { unsigned int changed = 0; - /* nsec seems unreliable - not all filesystems support it, so - * as long as it is in the inode cache you get right nsec - * but after it gets flushed, you get zero nsec. */ - if (ce->mtime.sec != (unsigned int)st->st_mtim.tv_sec -#ifdef NSEC - || ce->mtime.nsec != (unsigned int)st->st_mtim.tv_nsec -#endif - ) + if (ce->ce_mtime.sec != htonl(st->st_mtime)) changed |= MTIME_CHANGED; - if (ce->ctime.sec != (unsigned int)st->st_ctim.tv_sec + if (ce->ce_ctime.sec != htonl(st->st_ctime)) + changed |= CTIME_CHANGED; + #ifdef NSEC - || ce->ctime.nsec != (unsigned int)st->st_ctim.tv_nsec -#endif - ) + /* + * nsec seems unreliable - not all filesystems support it, so + * as long as it is in the inode cache you get right nsec + * but after it gets flushed, you get zero nsec. + */ + if (ce->ce_mtime.nsec != htonl(st->st_mtim.tv_nsec) + changed |= MTIME_CHANGED; + if (ce->ce_ctime.nsec != htonl(st->st_ctim.tv_nsec) changed |= CTIME_CHANGED; - if (ce->st_uid != (unsigned int)st->st_uid || - ce->st_gid != (unsigned int)st->st_gid) +#endif + + if (ce->ce_uid != htonl(st->st_uid) || + ce->ce_gid != htonl(st->st_gid)) changed |= OWNER_CHANGED; - if (ce->st_mode != (unsigned int)st->st_mode) + if (ce->ce_mode != htonl(st->st_mode)) changed |= MODE_CHANGED; - if (ce->st_dev != (unsigned int)st->st_dev || - ce->st_ino != (unsigned int)st->st_ino) + if (ce->ce_dev != htonl(st->st_dev) || + ce->ce_ino != htonl(st->st_ino)) changed |= INODE_CHANGED; - if (ce->st_size != (unsigned int)st->st_size) + if (ce->ce_size != htonl(st->st_size)) changed |= DATA_CHANGED; return changed; } @@ -335,7 +337,7 @@ int cache_name_pos(const char *name, int namelen) while (last > first) { int next = (last + first) >> 1; struct cache_entry *ce = active_cache[next]; - int cmp = cache_name_compare(name, namelen, ce->name, ce->namelen); + int cmp = cache_name_compare(name, namelen, ce->name, ce_namelen(ce)); if (!cmp) return next; if (cmp < 0) { @@ -362,7 +364,7 @@ int add_cache_entry(struct cache_entry *ce, int ok_to_add) { int pos; - pos = cache_name_pos(ce->name, ce->namelen); + pos = cache_name_pos(ce->name, ce_namelen(ce)); /* existing match? Just replace it */ if (pos >= 0) { @@ -393,9 +395,9 @@ static int verify_hdr(struct cache_header *hdr, unsigned long size) SHA_CTX c; unsigned char sha1[20]; - if (hdr->signature != CACHE_SIGNATURE) + if (hdr->hdr_signature != htonl(CACHE_SIGNATURE)) return error("bad signature"); - if (hdr->version != 1) + if (hdr->hdr_version != htonl(1)) return error("bad version"); SHA1_Init(&c); SHA1_Update(&c, hdr, offsetof(struct cache_header, sha1)); @@ -443,12 +445,12 @@ int read_cache(void) if (verify_hdr(hdr, size) < 0) goto unmap; - active_nr = hdr->entries; + active_nr = ntohl(hdr->hdr_entries); active_alloc = alloc_nr(active_nr); active_cache = calloc(active_alloc, sizeof(struct cache_entry *)); offset = sizeof(*hdr); - for (i = 0; i < hdr->entries; i++) { + for (i = 0; i < active_nr; i++) { struct cache_entry *ce = map + offset; offset = offset + ce_size(ce); active_cache[i] = ce; @@ -467,9 +469,9 @@ int write_cache(int newfd, struct cache_entry **cache, int entries) struct cache_header hdr; int i; - hdr.signature = CACHE_SIGNATURE; - hdr.version = 1; - hdr.entries = entries; + hdr.hdr_signature = htonl(CACHE_SIGNATURE); + hdr.hdr_version = htonl(1); + hdr.hdr_entries = htonl(entries); SHA1_Init(&c); SHA1_Update(&c, &hdr, offsetof(struct cache_header, sha1)); diff --git a/read-tree.c b/read-tree.c index 9aec97fa7..5c6588da4 100644 --- a/read-tree.c +++ b/read-tree.c @@ -13,8 +13,8 @@ static int read_one_entry(unsigned char *sha1, const char *base, int baselen, co memset(ce, 0, size); - ce->st_mode = mode; - ce->namelen = baselen + len; + ce->ce_mode = htonl(mode); + ce->ce_namelen = htons(baselen + len); memcpy(ce->name, base, baselen); memcpy(ce->name + baselen, pathname, len+1); memcpy(ce->sha1, sha1, 20); diff --git a/show-diff.c b/show-diff.c index 1286dd121..a15e9b4cf 100644 --- a/show-diff.c +++ b/show-diff.c @@ -90,7 +90,7 @@ int main(int argc, char **argv) changed = cache_match_stat(ce, &st); if (!changed) continue; - printf("%.*s: ", ce->namelen, ce->name); + printf("%.*s: ", ce_namelen(ce), ce->name); for (n = 0; n < 20; n++) printf("%02x", ce->sha1[n]); printf("\n"); diff --git a/update-cache.c b/update-cache.c index 6d37c55a2..85d0591c8 100644 --- a/update-cache.c +++ b/update-cache.c @@ -68,18 +68,17 @@ static int index_fd(const char *path, int namelen, struct cache_entry *ce, int f */ static void fill_stat_cache_info(struct cache_entry *ce, struct stat *st) { - ce->ctime.sec = st->st_ctime; + ce->ce_ctime.sec = htonl(st->st_ctime); + ce->ce_mtime.sec = htonl(st->st_mtime); #ifdef NSEC - ce->ctime.nsec = st->st_ctim.tv_nsec; + ce->ce_ctime.nsec = htonl(st->st_ctim.tv_nsec); + ce->ce_mtime.nsec = htonl(st->st_mtim.tv_nsec); #endif - ce->mtime.sec = st->st_mtime; -#ifdef NSEC - ce->mtime.nsec = st->st_mtim.tv_nsec; -#endif - ce->st_dev = st->st_dev; - ce->st_ino = st->st_ino; - ce->st_uid = st->st_uid; - ce->st_gid = st->st_gid; + ce->ce_dev = htonl(st->st_dev); + ce->ce_ino = htonl(st->st_ino); + ce->ce_uid = htonl(st->st_uid); + ce->ce_gid = htonl(st->st_gid); + ce->ce_size = htonl(st->st_size); } static int add_file_to_cache(char *path) @@ -107,9 +106,8 @@ static int add_file_to_cache(char *path) memset(ce, 0, size); memcpy(ce->name, path, namelen); fill_stat_cache_info(ce, &st); - ce->st_mode = st.st_mode; - ce->st_size = st.st_size; - ce->namelen = namelen; + ce->ce_mode = htonl(st.st_mode); + ce->ce_namelen = htons(namelen); if (index_fd(path, namelen, ce, fd, &st) < 0) return -1; @@ -190,7 +188,6 @@ static struct cache_entry *refresh_entry(struct cache_entry *ce) updated = malloc(size); memcpy(updated, ce, size); fill_stat_cache_info(updated, &st); - updated->st_size = st.st_size; return updated; } diff --git a/write-tree.c b/write-tree.c index 881c8c2b5..e2bbbd261 100644 --- a/write-tree.c +++ b/write-tree.c @@ -45,7 +45,7 @@ static int write_tree(struct cache_entry **cachep, int maxentries, const char *b do { struct cache_entry *ce = cachep[nr]; const char *pathname = ce->name, *filename, *dirname; - int pathlen = ce->namelen, entrylen; + int pathlen = ce_namelen(ce), entrylen; unsigned char *sha1; unsigned int mode; @@ -54,7 +54,7 @@ static int write_tree(struct cache_entry **cachep, int maxentries, const char *b break; sha1 = ce->sha1; - mode = ce->st_mode; + mode = ntohl(ce->ce_mode); /* Do we have _further_ subdirectories? */ filename = pathname + baselen; -- 2.26.2