#include <string.h>
#include <errno.h>
#include <sys/mman.h>
+#include <netinet/in.h>
#include <openssl/sha.h>
#include <zlib.h>
#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];
};
* 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];
};
#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)
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)",
{
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;
}
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) {
{
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) {
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));
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;
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));
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);
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");
*/
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)
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;
updated = malloc(size);
memcpy(updated, ce, size);
fill_stat_cache_info(updated, &st);
- updated->st_size = st.st_size;
return updated;
}
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;
break;
sha1 = ce->sha1;
- mode = ce->st_mode;
+ mode = ntohl(ce->ce_mode);
/* Do we have _further_ subdirectories? */
filename = pathname + baselen;