Initial revision of "git", the information manager from hell
[git.git] / cache.h
1 #ifndef CACHE_H
2 #define CACHE_H
3
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>
12
13 #include <openssl/sha.h>
14 #include <zlib.h>
15
16 /*
17  * Basic data structures for the directory cache
18  *
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.
22  */
23
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];
30 };
31
32 /*
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.
36  */
37 struct cache_time {
38         unsigned int sec;
39         unsigned int nsec;
40 };
41
42 /*
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.
46  */
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];
59 };
60
61 const char *sha1_file_directory;
62 struct cache_entry **active_cache;
63 unsigned int active_nr, active_alloc;
64
65 #define DB_ENVIRONMENT "SHA1_FILE_DIRECTORY"
66 #define DEFAULT_DB_ENVIRONMENT ".dircache/objects"
67
68 #define cache_entry_size(len) ((offsetof(struct cache_entry,name) + (len) + 8) & ~7)
69 #define ce_size(ce) cache_entry_size((ce)->namelen)
70
71 #define alloc_nr(x) (((x)+16)*3/2)
72
73 /* Initialize the cache information */
74 extern int read_cache(void);
75
76 /* Return a statically allocated filename matching the sha1 signature */
77 extern char *sha1_file_name(unsigned char *sha1);
78
79 /* Write a memory buffer out to the sha file */
80 extern int write_sha1_buffer(unsigned char *sha1, void *buf, unsigned int size);
81
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);
85
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! */
89
90 /* General helper functions */
91 extern void usage(const char *err);
92
93 #endif /* CACHE_H */