Skip to content

Commit 197ee8c

Browse files
author
Linus Torvalds
committed
Make "write_cache()" and friends available as generic routines.
This is needed for the change to make "read-tree" just read into the cache (and then you do a "checkout-cache" to update your current dir contents).
1 parent 8d3af1d commit 197ee8c

File tree

3 files changed

+75
-73
lines changed

3 files changed

+75
-73
lines changed

cache.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ unsigned int active_nr, active_alloc;
7373

7474
/* Initialize and use the cache information */
7575
extern int read_cache(void);
76+
extern int write_cache(int newfd, struct cache_entry **cache, int entries);
7677
extern int cache_name_pos(const char *name, int namelen);
78+
extern int add_cache_entry(struct cache_entry *ce);
79+
extern int remove_file_from_cache(char *path);
7780
extern int cache_match_stat(struct cache_entry *ce, struct stat *st);
7881

7982
#define MTIME_CHANGED 0x0001
@@ -97,8 +100,8 @@ extern int write_sha1_file(char *buf, unsigned len);
97100
extern int check_sha1_signature(unsigned char *sha1, void *buf, unsigned long size);
98101

99102
/* Convert to/from hex/sha1 representation */
100-
extern int get_sha1_hex(char *hex, unsigned char *sha1);
101-
extern char *sha1_to_hex(unsigned char *sha1); /* static buffer! */
103+
extern int get_sha1_hex(const char *hex, unsigned char *sha1);
104+
extern char *sha1_to_hex(const unsigned char *sha1); /* static buffer result! */
102105

103106
/* General helper functions */
104107
extern void usage(const char *err);

read-cache.c

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ static unsigned hexval(char c)
2626
return ~0;
2727
}
2828

29-
int get_sha1_hex(char *hex, unsigned char *sha1)
29+
int get_sha1_hex(const char *hex, unsigned char *sha1)
3030
{
3131
int i;
3232
for (i = 0; i < 20; i++) {
@@ -39,7 +39,7 @@ int get_sha1_hex(char *hex, unsigned char *sha1)
3939
return 0;
4040
}
4141

42-
char * sha1_to_hex(unsigned char *sha1)
42+
char * sha1_to_hex(const unsigned char *sha1)
4343
{
4444
static char buffer[50];
4545
static const char hex[] = "0123456789abcdef";
@@ -281,6 +281,44 @@ int cache_name_pos(const char *name, int namelen)
281281
return first;
282282
}
283283

284+
int remove_file_from_cache(char *path)
285+
{
286+
int pos = cache_name_pos(path, strlen(path));
287+
if (pos < 0) {
288+
pos = -pos-1;
289+
active_nr--;
290+
if (pos < active_nr)
291+
memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos - 1) * sizeof(struct cache_entry *));
292+
}
293+
return 0;
294+
}
295+
296+
int add_cache_entry(struct cache_entry *ce)
297+
{
298+
int pos;
299+
300+
pos = cache_name_pos(ce->name, ce->namelen);
301+
302+
/* existing match? Just replace it */
303+
if (pos < 0) {
304+
active_cache[-pos-1] = ce;
305+
return 0;
306+
}
307+
308+
/* Make sure the array is big enough .. */
309+
if (active_nr == active_alloc) {
310+
active_alloc = alloc_nr(active_alloc);
311+
active_cache = realloc(active_cache, active_alloc * sizeof(struct cache_entry *));
312+
}
313+
314+
/* Add it in.. */
315+
active_nr++;
316+
if (active_nr > pos)
317+
memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
318+
active_cache[pos] = ce;
319+
return 0;
320+
}
321+
284322
static int verify_hdr(struct cache_header *hdr, unsigned long size)
285323
{
286324
SHA_CTX c;
@@ -354,3 +392,33 @@ int read_cache(void)
354392
return error("verify header failed");
355393
}
356394

395+
int write_cache(int newfd, struct cache_entry **cache, int entries)
396+
{
397+
SHA_CTX c;
398+
struct cache_header hdr;
399+
int i;
400+
401+
hdr.signature = CACHE_SIGNATURE;
402+
hdr.version = 1;
403+
hdr.entries = entries;
404+
405+
SHA1_Init(&c);
406+
SHA1_Update(&c, &hdr, offsetof(struct cache_header, sha1));
407+
for (i = 0; i < entries; i++) {
408+
struct cache_entry *ce = cache[i];
409+
int size = ce_size(ce);
410+
SHA1_Update(&c, ce, size);
411+
}
412+
SHA1_Final(hdr.sha1, &c);
413+
414+
if (write(newfd, &hdr, sizeof(hdr)) != sizeof(hdr))
415+
return -1;
416+
417+
for (i = 0; i < entries; i++) {
418+
struct cache_entry *ce = cache[i];
419+
int size = ce_size(ce);
420+
if (write(newfd, ce, size) != size)
421+
return -1;
422+
}
423+
return 0;
424+
}

update-cache.c

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,6 @@
55
*/
66
#include "cache.h"
77

8-
static int remove_file_from_cache(char *path)
9-
{
10-
int pos = cache_name_pos(path, strlen(path));
11-
if (pos < 0) {
12-
pos = -pos-1;
13-
active_nr--;
14-
if (pos < active_nr)
15-
memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos - 1) * sizeof(struct cache_entry *));
16-
}
17-
return 0;
18-
}
19-
20-
static int add_cache_entry(struct cache_entry *ce)
21-
{
22-
int pos;
23-
24-
pos = cache_name_pos(ce->name, ce->namelen);
25-
26-
/* existing match? Just replace it */
27-
if (pos < 0) {
28-
active_cache[-pos-1] = ce;
29-
return 0;
30-
}
31-
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 *));
36-
}
37-
38-
/* Add it in.. */
39-
active_nr++;
40-
if (active_nr > pos)
41-
memmove(active_cache + pos + 1, active_cache + pos, (active_nr - pos - 1) * sizeof(ce));
42-
active_cache[pos] = ce;
43-
return 0;
44-
}
45-
468
static int index_fd(const char *path, int namelen, struct cache_entry *ce, int fd, struct stat *st)
479
{
4810
z_stream stream;
@@ -126,37 +88,6 @@ static int add_file_to_cache(char *path)
12688
return add_cache_entry(ce);
12789
}
12890

129-
static int write_cache(int newfd, struct cache_entry **cache, int entries)
130-
{
131-
SHA_CTX c;
132-
struct cache_header hdr;
133-
int i;
134-
135-
hdr.signature = CACHE_SIGNATURE;
136-
hdr.version = 1;
137-
hdr.entries = entries;
138-
139-
SHA1_Init(&c);
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);
145-
}
146-
SHA1_Final(hdr.sha1, &c);
147-
148-
if (write(newfd, &hdr, sizeof(hdr)) != sizeof(hdr))
149-
return -1;
150-
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)
155-
return -1;
156-
}
157-
return 0;
158-
}
159-
16091
/*
16192
* We fundamentally don't like some paths: we don't want
16293
* dot or dot-dot anywhere, and in fact, we don't even want

0 commit comments

Comments
 (0)