2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
9 * Default to not allowing changes to the list of files. The
10 * tool doesn't actually care, but this makes it harder to add
11 * files to the revision control by mistake by doing something
12 * like "update-cache *" and suddenly having all the object
13 * files be revision controlled.
15 static int allow_add
= 0, allow_remove
= 0;
17 static int index_fd(const char *path
, int namelen
, struct cache_entry
*ce
, int fd
, struct stat
*st
)
20 int max_out_bytes
= namelen
+ st
->st_size
+ 200;
21 void *out
= malloc(max_out_bytes
);
22 void *metadata
= malloc(namelen
+ 200);
23 void *in
= mmap(NULL
, st
->st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
27 if (!out
|| (int)(long)in
== -1)
30 memset(&stream
, 0, sizeof(stream
));
31 deflateInit(&stream
, Z_BEST_COMPRESSION
);
34 * ASCII size + nul byte
36 stream
.next_in
= metadata
;
37 stream
.avail_in
= 1+sprintf(metadata
, "blob %lu", (unsigned long) st
->st_size
);
38 stream
.next_out
= out
;
39 stream
.avail_out
= max_out_bytes
;
40 while (deflate(&stream
, 0) == Z_OK
)
47 stream
.avail_in
= st
->st_size
;
48 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
54 SHA1_Update(&c
, out
, stream
.total_out
);
55 SHA1_Final(ce
->sha1
, &c
);
57 return write_sha1_buffer(ce
->sha1
, out
, stream
.total_out
);
60 static int add_file_to_cache(char *path
)
63 struct cache_entry
*ce
;
67 fd
= open(path
, O_RDONLY
);
69 if (errno
== ENOENT
) {
71 return remove_file_from_cache(path
);
75 if (fstat(fd
, &st
) < 0) {
79 namelen
= strlen(path
);
80 size
= cache_entry_size(namelen
);
83 memcpy(ce
->name
, path
, namelen
);
84 ce
->ctime
.sec
= st
.st_ctime
;
85 ce
->ctime
.nsec
= st
.st_ctim
.tv_nsec
;
86 ce
->mtime
.sec
= st
.st_mtime
;
87 ce
->mtime
.nsec
= st
.st_mtim
.tv_nsec
;
88 ce
->st_dev
= st
.st_dev
;
89 ce
->st_ino
= st
.st_ino
;
90 ce
->st_mode
= st
.st_mode
;
91 ce
->st_uid
= st
.st_uid
;
92 ce
->st_gid
= st
.st_gid
;
93 ce
->st_size
= st
.st_size
;
94 ce
->namelen
= namelen
;
96 if (index_fd(path
, namelen
, ce
, fd
, &st
) < 0)
99 return add_cache_entry(ce
, allow_add
);
102 static void refresh_entry(struct cache_entry
*ce
)
105 * This is really not the right way to do it, but
106 * add_file_to_cache() does do the right thing.
108 * We should really just update the cache
109 * entry in-place, I think. With this approach we
110 * end up allocating a new one, searching for where
111 * to insert it etc etc crud.
113 add_file_to_cache(ce
->name
);
116 static void refresh_cache(void)
120 for (i
= 0; i
< active_nr
; i
++)
121 refresh_entry(active_cache
[i
]);
125 * We fundamentally don't like some paths: we don't want
126 * dot or dot-dot anywhere, and in fact, we don't even want
127 * any other dot-files (.dircache or anything else). They
128 * are hidden, for chist sake.
130 * Also, we don't want double slashes or slashes at the
131 * end that can make pathnames ambiguous.
133 static int verify_path(char *path
)
144 if (c
!= '/' && c
!= '.' && c
!= '\0')
152 int main(int argc
, char **argv
)
154 int i
, newfd
, entries
;
155 int allow_options
= 1;
157 entries
= read_cache();
159 perror("cache corrupted");
163 newfd
= open(".dircache/index.lock", O_RDWR
| O_CREAT
| O_EXCL
, 0600);
165 perror("unable to create new cachefile");
168 for (i
= 1 ; i
< argc
; i
++) {
169 char *path
= argv
[i
];
171 if (allow_options
&& *path
== '-') {
172 if (!strcmp(path
, "--")) {
176 if (!strcmp(path
, "--add")) {
180 if (!strcmp(path
, "--remove")) {
184 if (!strcmp(path
, "--refresh")) {
188 usage("unknown option %s", path
);
190 if (!verify_path(path
)) {
191 fprintf(stderr
, "Ignoring path %s\n", argv
[i
]);
194 if (add_file_to_cache(path
)) {
195 fprintf(stderr
, "Unable to add %s to database\n", path
);
199 if (!write_cache(newfd
, active_cache
, active_nr
) && !rename(".dircache/index.lock", ".dircache/index"))
202 unlink(".dircache/index.lock");