Initial revision of "git", the information manager from hell
[git/graveyard.git] / cache.h
blob98a32a9ad39883c6d05a000a68511d4b1ee2b3c7
1 #ifndef CACHE_H
2 #define CACHE_H
4 #include <stdio.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <stddef.h>
8 #include <stdlib.h>
9 #include <stdarg.h>
10 #include <errno.h>
11 #include <sys/mman.h>
13 #include <openssl/sha.h>
14 #include <zlib.h>
17 * Basic data structures for the directory cache
19 * NOTE NOTE NOTE! This is all in the native CPU byte format. It's
20 * not even trying to be portable. It's trying to be efficient. It's
21 * just a cache, after all.
24 #define CACHE_SIGNATURE 0x44495243 /* "DIRC" */
25 struct cache_header {
26 unsigned int signature;
27 unsigned int version;
28 unsigned int entries;
29 unsigned char sha1[20];
33 * The "cache_time" is just the low 32 bits of the
34 * time. It doesn't matter if it overflows - we only
35 * check it for equality in the 32 bits we save.
37 struct cache_time {
38 unsigned int sec;
39 unsigned int nsec;
43 * dev/ino/uid/gid/size are also just tracked to the low 32 bits
44 * Again - this is just a (very strong in practice) heuristic that
45 * the inode hasn't changed.
47 struct cache_entry {
48 struct cache_time ctime;
49 struct cache_time mtime;
50 unsigned int st_dev;
51 unsigned int st_ino;
52 unsigned int st_mode;
53 unsigned int st_uid;
54 unsigned int st_gid;
55 unsigned int st_size;
56 unsigned char sha1[20];
57 unsigned short namelen;
58 unsigned char name[0];
61 const char *sha1_file_directory;
62 struct cache_entry **active_cache;
63 unsigned int active_nr, active_alloc;
65 #define DB_ENVIRONMENT "SHA1_FILE_DIRECTORY"
66 #define DEFAULT_DB_ENVIRONMENT ".dircache/objects"
68 #define cache_entry_size(len) ((offsetof(struct cache_entry,name) + (len) + 8) & ~7)
69 #define ce_size(ce) cache_entry_size((ce)->namelen)
71 #define alloc_nr(x) (((x)+16)*3/2)
73 /* Initialize the cache information */
74 extern int read_cache(void);
76 /* Return a statically allocated filename matching the sha1 signature */
77 extern char *sha1_file_name(unsigned char *sha1);
79 /* Write a memory buffer out to the sha file */
80 extern int write_sha1_buffer(unsigned char *sha1, void *buf, unsigned int size);
82 /* Read and unpack a sha1 file into memory, write memory to a sha1 file */
83 extern void * read_sha1_file(unsigned char *sha1, char *type, unsigned long *size);
84 extern int write_sha1_file(char *buf, unsigned len);
86 /* Convert to/from hex/sha1 representation */
87 extern int get_sha1_hex(char *hex, unsigned char *sha1);
88 extern char *sha1_to_hex(unsigned char *sha1); /* static buffer! */
90 /* General helper functions */
91 extern void usage(const char *err);
93 #endif /* CACHE_H */