@matheusbsbs / matheus-bagliano / commits / b721e01f6e
Fork 0

Use the same move_temp_to_file in git-http-fetch.

The http commit walker cannot use the same temporary file creation code because it needs to use predictable temporary filename for partial fetch continuation purposes, but the code to move the temporary file to the final location should be usable from the ordinary object creation codepath. Export move_temp_to_file from sha1_file.c and use it, while losing the custom relink_or_rename function from http-fetch.c. Also the temporary object file creation part needs to make sure the leading path exists, in preparation of the really lazy fan-out directory creation. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Oct 10, 2005 at 23:22 UTC b721e01f6efd84b04c6946553cd566f2707c964d
3 files changed +20 -31
cache.h
+1
@@ -223,6 +223,7 @@ extern int read_tree(void *buffer, unsigned long size, int stage, const char **p
223 extern int write_sha1_from_fd(const unsigned char *sha1, int fd, char *buffer,
224 size_t bufsize, size_t *bufposn);
225 extern int write_sha1_to_fd(int fd, const unsigned char *sha1);
226 +extern int move_temp_to_file(const char *tmpfile, char *filename);
227
228 extern int has_sha1_pack(const unsigned char *sha1);
229 extern int has_sha1_file(const unsigned char *sha1);
http-fetch.c
+18 -30
@@ -138,25 +138,6 @@ static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
138 return size;
139 }
140
141 -int relink_or_rename(char *old, char *new) {
142 - int ret;
143 -
144 - ret = link(old, new);
145 - if (ret < 0) {
146 - /* Same Coda hack as in write_sha1_file(sha1_file.c) */
147 - ret = errno;
148 - if (ret == EXDEV && !rename(old, new))
149 - return 0;
150 - }
151 - unlink(old);
152 - if (ret) {
153 - if (ret != EEXIST)
154 - return ret;
155 - }
156 -
157 - return 0;
158 -}
159 -
141 #ifdef USE_CURL_MULTI
142 void process_curl_messages();
143 void process_request_queue();
@@ -295,6 +276,20 @@ void start_request(struct transfer_request *request)
276
277 request->local = open(request->tmpfile,
278 O_WRONLY | O_CREAT | O_EXCL, 0666);
279 + /* This could have failed due to the "lazy directory creation";
280 + * try to mkdir the last path component.
281 + */
282 + if (request->local < 0 && errno == ENOENT) {
283 + char *dir = strrchr(request->tmpfile, '/');
284 + if (dir) {
285 + *dir = 0;
286 + mkdir(request->tmpfile, 0777);
287 + *dir = '/';
288 + }
289 + request->local = open(request->tmpfile,
290 + O_WRONLY | O_CREAT | O_EXCL, 0666);
291 + }
292 +
293 if (request->local < 0) {
294 request->state = ABORTED;
295 error("Couldn't create temporary file %s for %s: %s\n",
@@ -408,7 +403,7 @@ void finish_request(struct transfer_request *request)
403 return;
404 }
405 request->rename =
411 - relink_or_rename(request->tmpfile, request->filename);
406 + move_temp_to_file(request->tmpfile, request->filename);
407
408 if (request->rename == 0)
409 pull_say("got %s\n", sha1_to_hex(request->sha1));
@@ -542,7 +537,6 @@ static int fetch_index(struct alt_base *repo, unsigned char *sha1)
537 char *filename;
538 char *url;
539 char tmpfile[PATH_MAX];
545 - int ret;
540 long prev_posn = 0;
541 char range[RANGE_HEADER_SIZE];
542 struct curl_slist *range_header = NULL;
@@ -599,12 +593,7 @@ static int fetch_index(struct alt_base *repo, unsigned char *sha1)
593
594 fclose(indexfile);
595
602 - ret = relink_or_rename(tmpfile, filename);
603 - if (ret)
604 - return error("unable to write index filename %s: %s",
605 - filename, strerror(ret));
606 -
607 - return 0;
596 + return move_temp_to_file(tmpfile, filename);
597 }
598
599 static int setup_index(struct alt_base *repo, unsigned char *sha1)
@@ -869,10 +858,9 @@ static int fetch_pack(struct alt_base *repo, unsigned char *sha1)
858
859 fclose(packfile);
860
872 - ret = relink_or_rename(tmpfile, filename);
861 + ret = move_temp_to_file(tmpfile, filename);
862 if (ret)
874 - return error("unable to write pack filename %s: %s",
875 - filename, strerror(ret));
863 + return ret;
864
865 lst = &repo->packs;
866 while (*lst != target)
sha1_file.c
+1 -1
@@ -1287,7 +1287,7 @@ static int link_temp_to_file(const char *tmpfile, char *filename)
1287 /*
1288 * Move the just written object into its final resting place
1289 */
1290 -static int move_temp_to_file(const char *tmpfile, char *filename)
1290 +int move_temp_to_file(const char *tmpfile, char *filename)
1291 {
1292 int ret = link_temp_to_file(tmpfile, filename);
1293 if (ret) {