1248
return sha1_file_name(sha1);
1249
}
1250
1251
+/*
1252
+ * Link the tempfile to the final place, possibly creating the
1253
+ * last directory level as you do so.
1254
+ *
1255
+ * Returns the errno on failure, 0 on success.
1256
+ */
1257
+static int link_temp_to_file(const char *tmpfile, char *filename)
1258
+{
1259
+ int ret;
1260
+
1261
+ if (!link(tmpfile, filename))
1262
+ return 0;
1263
+
1264
+ /*
1265
+ * Try to mkdir the last path component if that failed
1266
+ * with an ENOENT.
1267
+ *
1268
+ * Re-try the "link()" regardless of whether the mkdir
1269
+ * succeeds, since a race might mean that somebody
1270
+ * else succeeded.
1271
+ */
1272
+ ret = errno;
1273
+ if (ret == ENOENT) {
1274
+ char *dir = strrchr(filename, '/');
1275
+ if (dir) {
1276
+ *dir = 0;
1277
+ mkdir(filename, 0777);
1278
+ *dir = '/';
1279
+ if (!link(tmpfile, filename))
1280
+ return 0;
1281
+ ret = errno;
1282
+ }
1283
+ }
1284
+ return ret;
1285
+}
1286
+
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)
1291
+{
1292
+ int ret = link_temp_to_file(tmpfile, filename);
1293
+ if (ret) {
1294
+ /*
1295
+ * Coda hack - coda doesn't like cross-directory links,
1296
+ * so we fall back to a rename, which will mean that it
1297
+ * won't be able to check collisions, but that's not a
1298
+ * big deal.
1299
+ *
1300
+ * When this succeeds, we just return 0. We have nothing
1301
+ * left to unlink.
1302
+ */
1303
+ if (ret == EXDEV && !rename(tmpfile, filename))
1304
+ return 0;
1305
+ }
1306
+ unlink(tmpfile);
1307
+ if (ret) {
1308
+ if (ret != EEXIST) {
1309
+ fprintf(stderr, "unable to write sha1 filename %s: %s", filename, strerror(ret));
1310
+ return -1;
1311
+ }
1312
+ /* FIXME!!! Collision check here ? */
1313
+ }
1314
+
1315
+ return 0;
1316
+}
1317
+
1318
int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
1319
{
1320
int size;
1324
char *filename;
1325
static char tmpfile[PATH_MAX];
1326
unsigned char hdr[50];
1260
- int fd, hdrlen, ret;
1327
+ int fd, hdrlen;
1328
1329
/* Normally if we have it in the pack then we do not bother writing
1330
* it out into .git/objects/??/?{38} file.
1387
close(fd);
1388
free(compressed);
1389
1323
- ret = link(tmpfile, filename);
1324
- if (ret < 0) {
1325
- ret = errno;
1326
-
1327
- /*
1328
- * Coda hack - coda doesn't like cross-directory links,
1329
- * so we fall back to a rename, which will mean that it
1330
- * won't be able to check collisions, but that's not a
1331
- * big deal.
1332
- *
1333
- * When this succeeds, we just return 0. We have nothing
1334
- * left to unlink.
1335
- */
1336
- if (ret == EXDEV && !rename(tmpfile, filename))
1337
- return 0;
1338
- }
1339
- unlink(tmpfile);
1340
- if (ret) {
1341
- if (ret != EEXIST) {
1342
- fprintf(stderr, "unable to write sha1 filename %s: %s", filename, strerror(ret));
1343
- return -1;
1344
- }
1345
- /* FIXME!!! Collision check here ? */
1346
- }
1347
-
1348
- return 0;
1390
+ return move_temp_to_file(tmpfile, filename);
1391
}
1392
1393
int write_sha1_to_fd(int fd, const unsigned char *sha1)
1462
int write_sha1_from_fd(const unsigned char *sha1, int fd, char *buffer,
1463
size_t bufsize, size_t *bufposn)
1464
{
1423
- char *filename = sha1_file_name(sha1);
1424
-
1465
+ char tmpfile[PATH_MAX];
1466
int local;
1467
z_stream stream;
1468
unsigned char real_sha1[20];
1470
int ret;
1471
SHA_CTX c;
1472
1432
- local = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0666);
1473
+ snprintf(tmpfile, sizeof(tmpfile), "%s/obj_XXXXXX", get_object_directory());
1474
1475
+ local = mkstemp(tmpfile);
1476
if (local < 0)
1435
- return error("Couldn't open %s\n", filename);
1477
+ return error("Couldn't open %s for %s\n", tmpfile, sha1_to_hex(sha1));
1478
1479
memset(&stream, 0, sizeof(stream));
1480
1504
size = read(fd, buffer + *bufposn, bufsize - *bufposn);
1505
if (size <= 0) {
1506
close(local);
1465
- unlink(filename);
1507
+ unlink(tmpfile);
1508
if (!size)
1509
return error("Connection closed?");
1510
perror("Reading from connection");
1517
close(local);
1518
SHA1_Final(real_sha1, &c);
1519
if (ret != Z_STREAM_END) {
1478
- unlink(filename);
1520
+ unlink(tmpfile);
1521
return error("File %s corrupted", sha1_to_hex(sha1));
1522
}
1523
if (memcmp(sha1, real_sha1, 20)) {
1482
- unlink(filename);
1524
+ unlink(tmpfile);
1525
return error("File %s has bad hash\n", sha1_to_hex(sha1));
1526
}
1485
-
1486
- return 0;
1527
+
1528
+ return move_temp_to_file(tmpfile, sha1_file_name(sha1));
1529
}
1530
1531
int has_pack_index(const unsigned char *sha1)