mirror of
https://github.com/rkd77/elinks.git
synced 2024-12-04 14:46:47 -05:00
Merge code from create_bittorrent_path() and mkalldirs(). Suggested by
Jonas. Simpler and better.
This commit is contained in:
parent
c39a30ca49
commit
9dc2a7ffb7
@ -585,28 +585,9 @@ remove_bittorrent_peer_from_piece_cache(struct bittorrent_peer_connection *peer)
|
|||||||
static enum bittorrent_state
|
static enum bittorrent_state
|
||||||
create_bittorrent_path(unsigned char *path)
|
create_bittorrent_path(unsigned char *path)
|
||||||
{
|
{
|
||||||
int pos;
|
int ret = mkalldirs(path);
|
||||||
|
|
||||||
if (!*path) return BITTORRENT_STATE_ERROR;
|
return (ret ? BITTORRENT_STATE_ERROR : BITTORRENT_STATE_OK);
|
||||||
|
|
||||||
for (pos = 1; path[pos]; pos++) {
|
|
||||||
unsigned char separator = path[pos];
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
if (!dir_sep(separator))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
path[pos] = 0;
|
|
||||||
|
|
||||||
ret = mkdir(path, S_IREAD | S_IWRITE | S_IEXEC);
|
|
||||||
|
|
||||||
path[pos] = separator;
|
|
||||||
|
|
||||||
if (ret < 0 && errno != EEXIST)
|
|
||||||
return BITTORRENT_STATE_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
return BITTORRENT_STATE_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Complementary to the above rmdir()s each directory in the path. */
|
/* Complementary to the above rmdir()s each directory in the path. */
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -581,82 +582,39 @@ get_directory_entries(unsigned char *dirname, int get_hidden)
|
|||||||
return entries;
|
return entries;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Create DIRECTORY.
|
/* Recursively create directories in a path. The last element in the path is
|
||||||
* If some of the pathname components of DIRECTORY
|
* taken to be a filename, and simply ignored */
|
||||||
* are missing, create them first. In case any mkdir() call fails,
|
|
||||||
* return its error status. Returns 0 on successful completion.
|
|
||||||
*
|
|
||||||
* The behaviour of this function should be identical to the behaviour
|
|
||||||
* of `mkdir -p' on systems where mkdir supports the `-p' option. */
|
|
||||||
static int
|
|
||||||
make_directory(const unsigned char *directory)
|
|
||||||
{
|
|
||||||
int i, len, ret, quit = 0;
|
|
||||||
unsigned char *dir;
|
|
||||||
|
|
||||||
/* Make a copy of dir, to be able to write to it. Otherwise, the
|
|
||||||
* function is unsafe if called with a read-only char *argument. */
|
|
||||||
len = strlen(directory) + 1;
|
|
||||||
dir = fmem_alloc(len);
|
|
||||||
if (!dir) return -1;
|
|
||||||
memcpy(dir, directory, len);
|
|
||||||
|
|
||||||
/* If the first character of dir is '/', skip it (and thus enable
|
|
||||||
* creation of absolute-pathname directories. */
|
|
||||||
for (i = dir_sep(*dir); 1; ++i) {
|
|
||||||
unsigned char sep;
|
|
||||||
|
|
||||||
for (; dir[i] && !dir_sep(dir[i]); i++);
|
|
||||||
if (!dir[i]) quit = 1;
|
|
||||||
|
|
||||||
sep = dir[i];
|
|
||||||
dir[i] = '\0';
|
|
||||||
/* Check whether the directory already exists. Allow creation of
|
|
||||||
* intermediate directories to fail, as the initial path components
|
|
||||||
* are not necessarily directories! */
|
|
||||||
if (!file_exists(dir))
|
|
||||||
ret = mkdir(dir, 0777);
|
|
||||||
else
|
|
||||||
ret = 0;
|
|
||||||
if (quit) break;
|
|
||||||
|
|
||||||
dir[i] = sep;
|
|
||||||
}
|
|
||||||
|
|
||||||
fmem_free(dir);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Create all the necessary directories for PATH (a file).
|
|
||||||
* Calls make_directory() internally. */
|
|
||||||
int
|
int
|
||||||
mkalldirs(const unsigned char *path)
|
mkalldirs(const unsigned char *path)
|
||||||
{
|
{
|
||||||
const unsigned char *p;
|
int pos, len, ret = 0;
|
||||||
unsigned char *dir;
|
unsigned char *p;
|
||||||
int ret, len;
|
|
||||||
|
|
||||||
p = path + strlen (path);
|
if (!*path) return -1;
|
||||||
for (; !dir_sep(*p) && p != path; p--);
|
|
||||||
|
|
||||||
/* Don't create if it's just a file. */
|
/* Make a copy of path, to be able to write to it. Otherwise, the
|
||||||
if (p == path && !dir_sep(*p))
|
* function is unsafe if called with a read-only char *argument. */
|
||||||
return 0;
|
len = strlen(path) + 1;
|
||||||
|
p = fmem_alloc(len);
|
||||||
|
if (!p) return -1;
|
||||||
|
memcpy(p, path, len);
|
||||||
|
|
||||||
len = p - path;
|
for (pos = 1; p[pos]; pos++) {
|
||||||
dir = fmem_alloc(len + 1);
|
unsigned char separator = p[pos];
|
||||||
if (!dir) return -1;
|
|
||||||
memcpy(dir, path, len);
|
|
||||||
dir[len] = '\0';
|
|
||||||
|
|
||||||
if (file_is_dir(dir)) {
|
if (!dir_sep(separator))
|
||||||
ret = 0;
|
continue;
|
||||||
} else {
|
|
||||||
ret = make_directory(dir);
|
p[pos] = 0;
|
||||||
|
|
||||||
|
ret = mkdir(p, S_IREAD | S_IWRITE | S_IEXEC);
|
||||||
|
|
||||||
|
p[pos] = separator;
|
||||||
|
|
||||||
|
if (ret < 0 && errno != EEXIST)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
fmem_free(dir);
|
fmem_free(p);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,8 @@ unsigned char *file_read_line(unsigned char *line, size_t *linesize,
|
|||||||
* restore previous umask(). */
|
* restore previous umask(). */
|
||||||
int safe_mkstemp(unsigned char *template);
|
int safe_mkstemp(unsigned char *template);
|
||||||
|
|
||||||
/* Create all the necessary directories for PATH (a file). */
|
/* Recursively create directories in a path. The last element in the path is
|
||||||
|
* taken to be a filename, and simply ignored */
|
||||||
int mkalldirs(const unsigned char *path);
|
int mkalldirs(const unsigned char *path);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user