Use sha1_file.c's mkdir-like routine in apply.c.
As far as I can see, create_subdirectories() in apply.c just duplicates the functionality of safe_create_leading_directories() from sha1_file.c. The former has a warm, fuzzy const parameter, but that's not important. The potential problem with EEXIST and creating directories should never occur here, but will be removed by future safe_create_leading_directories() changes. Other uses of EEXIST in apply.c should be fine barring intentionally malicious behavior. Signed-off-by: Jason Riedy <ejr@cs.berkeley.edu> Signed-off-by: Junio C Hamano <junkio@cox.net>
Jason Riedy committed
Feb 3, 2006 at 22:50 UTC
8361e1d4d343a33a60145129dcf8c08ec7de84f2
1 file changed
+4
-21
apply.c
+4
-21
@@ -1564,24 +1564,6 @@ static void add_index_file(const char *path, unsigned mode, void *buf, unsigned
1564
die("unable to add cache entry for %s", path);
1565
}
1566
1567
-static void create_subdirectories(const char *path)
1568
-{
1569
- int len = strlen(path);
1570
- char *buf = xmalloc(len + 1);
1571
- const char *slash = path;
1572
-
1573
- while ((slash = strchr(slash+1, '/')) != NULL) {
1574
- len = slash - path;
1575
- memcpy(buf, path, len);
1576
- buf[len] = 0;
1577
- if (mkdir(buf, 0777) < 0) {
1578
- if (errno != EEXIST)
1579
- break;
1580
- }
1581
- }
1582
- free(buf);
1583
-}
1584
-
1567
static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
1568
{
1569
int fd;
@@ -1610,13 +1592,14 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
1592
* which is true 99% of the time anyway. If they don't,
1593
* we create them and try again.
1594
*/
1613
-static void create_one_file(const char *path, unsigned mode, const char *buf, unsigned long size)
1595
+static void create_one_file(char *path, unsigned mode, const char *buf, unsigned long size)
1596
{
1597
if (!try_create_file(path, mode, buf, size))
1598
return;
1599
1600
if (errno == ENOENT) {
1619
- create_subdirectories(path);
1601
+ if (safe_create_leading_directories(path))
1602
+ return;
1603
if (!try_create_file(path, mode, buf, size))
1604
return;
1605
}
@@ -1643,7 +1626,7 @@ static void create_one_file(const char *path, unsigned mode, const char *buf, un
1626
1627
static void create_file(struct patch *patch)
1628
{
1646
- const char *path = patch->new_name;
1629
+ char *path = patch->new_name;
1630
unsigned mode = patch->new_mode;
1631
unsigned long size = patch->resultsize;
1632
char *buf = patch->result;