2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
8 static int cache_name_compare(const char *name1
, int len1
, const char *name2
, int len2
)
10 int len
= len1
< len2
? len1
: len2
;
13 cmp
= memcmp(name1
, name2
, len
);
23 static int cache_name_pos(const char *name
, int namelen
)
29 while (last
> first
) {
30 int next
= (last
+ first
) >> 1;
31 struct cache_entry
*ce
= active_cache
[next
];
32 int cmp
= cache_name_compare(name
, namelen
, ce
->name
, ce
->namelen
);
44 static int remove_file_from_cache(char *path
)
46 int pos
= cache_name_pos(path
, strlen(path
));
51 memmove(active_cache
+ pos
, active_cache
+ pos
+ 1, (active_nr
- pos
- 1) * sizeof(struct cache_entry
*));
56 static int add_cache_entry(struct cache_entry
*ce
)
60 pos
= cache_name_pos(ce
->name
, ce
->namelen
);
62 /* existing match? Just replace it */
64 active_cache
[-pos
-1] = ce
;
68 /* Make sure the array is big enough .. */
69 if (active_nr
== active_alloc
) {
70 active_alloc
= alloc_nr(active_alloc
);
71 active_cache
= realloc(active_cache
, active_alloc
* sizeof(struct cache_entry
*));
77 memmove(active_cache
+ pos
+ 1, active_cache
+ pos
, (active_nr
- pos
- 1) * sizeof(ce
));
78 active_cache
[pos
] = ce
;
82 static int index_fd(const char *path
, int namelen
, struct cache_entry
*ce
, int fd
, struct stat
*st
)
85 int max_out_bytes
= namelen
+ st
->st_size
+ 200;
86 void *out
= malloc(max_out_bytes
);
87 void *metadata
= malloc(namelen
+ 200);
88 void *in
= mmap(NULL
, st
->st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
92 if (!out
|| (int)(long)in
== -1)
95 memset(&stream
, 0, sizeof(stream
));
96 deflateInit(&stream
, Z_BEST_COMPRESSION
);
99 * ASCII size + nul byte
101 stream
.next_in
= metadata
;
102 stream
.avail_in
= 1+sprintf(metadata
, "blob %lu", (unsigned long) st
->st_size
);
103 stream
.next_out
= out
;
104 stream
.avail_out
= max_out_bytes
;
105 while (deflate(&stream
, 0) == Z_OK
)
112 stream
.avail_in
= st
->st_size
;
113 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
119 SHA1_Update(&c
, out
, stream
.total_out
);
120 SHA1_Final(ce
->sha1
, &c
);
122 return write_sha1_buffer(ce
->sha1
, out
, stream
.total_out
);
125 static int add_file_to_cache(char *path
)
128 struct cache_entry
*ce
;
132 fd
= open(path
, O_RDONLY
);
135 return remove_file_from_cache(path
);
138 if (fstat(fd
, &st
) < 0) {
142 namelen
= strlen(path
);
143 size
= cache_entry_size(namelen
);
146 memcpy(ce
->name
, path
, namelen
);
147 ce
->ctime
.sec
= st
.st_ctime
;
148 ce
->ctime
.nsec
= st
.st_ctim
.tv_nsec
;
149 ce
->mtime
.sec
= st
.st_mtime
;
150 ce
->mtime
.nsec
= st
.st_mtim
.tv_nsec
;
151 ce
->st_dev
= st
.st_dev
;
152 ce
->st_ino
= st
.st_ino
;
153 ce
->st_mode
= st
.st_mode
;
154 ce
->st_uid
= st
.st_uid
;
155 ce
->st_gid
= st
.st_gid
;
156 ce
->st_size
= st
.st_size
;
157 ce
->namelen
= namelen
;
159 if (index_fd(path
, namelen
, ce
, fd
, &st
) < 0)
162 return add_cache_entry(ce
);
165 static int write_cache(int newfd
, struct cache_entry
**cache
, int entries
)
168 struct cache_header hdr
;
171 hdr
.signature
= CACHE_SIGNATURE
;
173 hdr
.entries
= entries
;
176 SHA1_Update(&c
, &hdr
, offsetof(struct cache_header
, sha1
));
177 for (i
= 0; i
< entries
; i
++) {
178 struct cache_entry
*ce
= cache
[i
];
179 int size
= ce_size(ce
);
180 SHA1_Update(&c
, ce
, size
);
182 SHA1_Final(hdr
.sha1
, &c
);
184 if (write(newfd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
187 for (i
= 0; i
< entries
; i
++) {
188 struct cache_entry
*ce
= cache
[i
];
189 int size
= ce_size(ce
);
190 if (write(newfd
, ce
, size
) != size
)
197 * We fundamentally don't like some paths: we don't want
198 * dot or dot-dot anywhere, and in fact, we don't even want
199 * any other dot-files (.dircache or anything else). They
200 * are hidden, for chist sake.
202 * Also, we don't want double slashes or slashes at the
203 * end that can make pathnames ambiguous.
205 static int verify_path(char *path
)
216 if (c
!= '/' && c
!= '.' && c
!= '\0')
224 int main(int argc
, char **argv
)
226 int i
, newfd
, entries
;
228 entries
= read_cache();
230 perror("cache corrupted");
234 newfd
= open(".dircache/index.lock", O_RDWR
| O_CREAT
| O_EXCL
, 0600);
236 perror("unable to create new cachefile");
239 for (i
= 1 ; i
< argc
; i
++) {
240 char *path
= argv
[i
];
241 if (!verify_path(path
)) {
242 fprintf(stderr
, "Ignoring path %s\n", argv
[i
]);
245 if (add_file_to_cache(path
)) {
246 fprintf(stderr
, "Unable to add %s to database\n", path
);
250 if (!write_cache(newfd
, active_cache
, active_nr
) && !rename(".dircache/index.lock", ".dircache/index"))
253 unlink(".dircache/index.lock");