2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
8 static int remove_file_from_cache(char *path
)
10 int pos
= cache_name_pos(path
, strlen(path
));
15 memmove(active_cache
+ pos
, active_cache
+ pos
+ 1, (active_nr
- pos
- 1) * sizeof(struct cache_entry
*));
20 static int add_cache_entry(struct cache_entry
*ce
)
24 pos
= cache_name_pos(ce
->name
, ce
->namelen
);
26 /* existing match? Just replace it */
28 active_cache
[-pos
-1] = ce
;
32 /* Make sure the array is big enough .. */
33 if (active_nr
== active_alloc
) {
34 active_alloc
= alloc_nr(active_alloc
);
35 active_cache
= realloc(active_cache
, active_alloc
* sizeof(struct cache_entry
*));
41 memmove(active_cache
+ pos
+ 1, active_cache
+ pos
, (active_nr
- pos
- 1) * sizeof(ce
));
42 active_cache
[pos
] = ce
;
46 static int index_fd(const char *path
, int namelen
, struct cache_entry
*ce
, int fd
, struct stat
*st
)
49 int max_out_bytes
= namelen
+ st
->st_size
+ 200;
50 void *out
= malloc(max_out_bytes
);
51 void *metadata
= malloc(namelen
+ 200);
52 void *in
= mmap(NULL
, st
->st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
56 if (!out
|| (int)(long)in
== -1)
59 memset(&stream
, 0, sizeof(stream
));
60 deflateInit(&stream
, Z_BEST_COMPRESSION
);
63 * ASCII size + nul byte
65 stream
.next_in
= metadata
;
66 stream
.avail_in
= 1+sprintf(metadata
, "blob %lu", (unsigned long) st
->st_size
);
67 stream
.next_out
= out
;
68 stream
.avail_out
= max_out_bytes
;
69 while (deflate(&stream
, 0) == Z_OK
)
76 stream
.avail_in
= st
->st_size
;
77 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
83 SHA1_Update(&c
, out
, stream
.total_out
);
84 SHA1_Final(ce
->sha1
, &c
);
86 return write_sha1_buffer(ce
->sha1
, out
, stream
.total_out
);
89 static int add_file_to_cache(char *path
)
92 struct cache_entry
*ce
;
96 fd
= open(path
, O_RDONLY
);
99 return remove_file_from_cache(path
);
102 if (fstat(fd
, &st
) < 0) {
106 namelen
= strlen(path
);
107 size
= cache_entry_size(namelen
);
110 memcpy(ce
->name
, path
, namelen
);
111 ce
->ctime
.sec
= st
.st_ctime
;
112 ce
->ctime
.nsec
= st
.st_ctim
.tv_nsec
;
113 ce
->mtime
.sec
= st
.st_mtime
;
114 ce
->mtime
.nsec
= st
.st_mtim
.tv_nsec
;
115 ce
->st_dev
= st
.st_dev
;
116 ce
->st_ino
= st
.st_ino
;
117 ce
->st_mode
= st
.st_mode
;
118 ce
->st_uid
= st
.st_uid
;
119 ce
->st_gid
= st
.st_gid
;
120 ce
->st_size
= st
.st_size
;
121 ce
->namelen
= namelen
;
123 if (index_fd(path
, namelen
, ce
, fd
, &st
) < 0)
126 return add_cache_entry(ce
);
129 static int write_cache(int newfd
, struct cache_entry
**cache
, int entries
)
132 struct cache_header hdr
;
135 hdr
.signature
= CACHE_SIGNATURE
;
137 hdr
.entries
= entries
;
140 SHA1_Update(&c
, &hdr
, offsetof(struct cache_header
, sha1
));
141 for (i
= 0; i
< entries
; i
++) {
142 struct cache_entry
*ce
= cache
[i
];
143 int size
= ce_size(ce
);
144 SHA1_Update(&c
, ce
, size
);
146 SHA1_Final(hdr
.sha1
, &c
);
148 if (write(newfd
, &hdr
, sizeof(hdr
)) != sizeof(hdr
))
151 for (i
= 0; i
< entries
; i
++) {
152 struct cache_entry
*ce
= cache
[i
];
153 int size
= ce_size(ce
);
154 if (write(newfd
, ce
, size
) != size
)
161 * We fundamentally don't like some paths: we don't want
162 * dot or dot-dot anywhere, and in fact, we don't even want
163 * any other dot-files (.dircache or anything else). They
164 * are hidden, for chist sake.
166 * Also, we don't want double slashes or slashes at the
167 * end that can make pathnames ambiguous.
169 static int verify_path(char *path
)
180 if (c
!= '/' && c
!= '.' && c
!= '\0')
188 int main(int argc
, char **argv
)
190 int i
, newfd
, entries
;
192 entries
= read_cache();
194 perror("cache corrupted");
198 newfd
= open(".dircache/index.lock", O_RDWR
| O_CREAT
| O_EXCL
, 0600);
200 perror("unable to create new cachefile");
203 for (i
= 1 ; i
< argc
; i
++) {
204 char *path
= argv
[i
];
205 if (!verify_path(path
)) {
206 fprintf(stderr
, "Ignoring path %s\n", argv
[i
]);
209 if (add_file_to_cache(path
)) {
210 fprintf(stderr
, "Unable to add %s to database\n", path
);
214 if (!write_cache(newfd
, active_cache
, active_nr
) && !rename(".dircache/index.lock", ".dircache/index"))
217 unlink(".dircache/index.lock");