Browse Source

Make mkdirs behave more like 'mkdir -p' and make btpd create the directory

hierarchy to a specified torrent content directory if neccessary.
master
Richard Nyberg 18 years ago
parent
commit
cd8a3d5ecd
3 changed files with 20 additions and 15 deletions
  1. +1
    -1
      btpd/torrent.c
  2. +18
    -13
      misc/subr.c
  3. +1
    -1
      misc/subr.h

+ 1
- 1
btpd/torrent.c View File

@@ -95,7 +95,7 @@ torrent_start(struct tlib *tl)
if (tl->dir == NULL) if (tl->dir == NULL)
return IPC_EBADTENT; return IPC_EBADTENT;


if (mkdir(tl->dir, 0777) != 0 && errno != EEXIST) {
if (mkdirs(tl->dir, 0777) != 0 && errno != EEXIST) {
btpd_log(BTPD_L_ERROR, "torrent '%s': " btpd_log(BTPD_L_ERROR, "torrent '%s': "
"failed to create content dir '%s' (%s).\n", "failed to create content dir '%s' (%s).\n",
tl->name, tl->dir, strerror(errno)); tl->name, tl->dir, strerror(errno));


+ 18
- 13
misc/subr.c View File

@@ -97,24 +97,24 @@ set_blocking(int fd)
} }


int int
mkdirs(char *path)
mkdirs(char *path, int mode)
{ {
int err = 0; int err = 0;
char *spos = strchr(path + 1, '/'); // Must ignore the root
char *spos = strchr(path + 1, '/'); // Skip leading '/'


while (spos != NULL) { while (spos != NULL) {
*spos = '\0'; *spos = '\0';
err = mkdir(path, 0777);
err = mkdir(path, mode);
*spos = '/'; *spos = '/';


if (err != 0 && errno != EEXIST) {
err = errno;
break;
}
if (err != 0 && errno != EEXIST)
return errno;


spos = strchr(spos + 1, '/'); spos = strchr(spos + 1, '/');
} }
return err;
if (mkdir(path, mode) != 0)
return errno;
return 0;
} }


int int
@@ -130,11 +130,16 @@ vaopen(int *res, int flags, const char *fmt, va_list ap)
again: again:
fd = open(path, flags, 0666); fd = open(path, flags, 0666);
if (fd < 0 && errno == ENOENT && (flags & O_CREAT) != 0 && !didmkdirs) { if (fd < 0 && errno == ENOENT && (flags & O_CREAT) != 0 && !didmkdirs) {
if (mkdirs(path) == 0) {
didmkdirs = 1;
goto again;
} else
return errno;
char *rs = rindex(path, '/');
if (rs != NULL) {
*rs = '\0';
if (mkdirs(path, 0777) == 0) {
*rs = '/';
didmkdirs = 1;
goto again;
}
}
return errno;
} }


if (fd >= 0) { if (fd >= 0) {


+ 1
- 1
misc/subr.h View File

@@ -10,7 +10,7 @@
int set_nonblocking(int fd); int set_nonblocking(int fd);
int set_blocking(int fd); int set_blocking(int fd);


int mkdirs(char *path);
int mkdirs(char *path, int mode);


__attribute__((format (printf, 3, 0))) __attribute__((format (printf, 3, 0)))
int vaopen(int *resfd, int flags, const char *fmt, va_list ap); int vaopen(int *resfd, int flags, const char *fmt, va_list ap);


Loading…
Cancel
Save