diff options
author | graham.gower <graham.gower@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358> | 2009-11-25 20:42:27 (EST) |
---|---|---|
committer | graham.gower <graham.gower@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358> | 2009-11-25 20:42:27 (EST) |
commit | 9ba412631e57deb365d5e7fb8b8c2fbf9bfe27b0 (patch) | |
tree | 3d6a8bc8a98b9e193b474cb6cb74e470e20e1dc4 /libopkg/file_util.c | |
parent | df364b323b478922b8aedab497ee6e6659dbc5f4 (diff) |
file_util.c cleanups. Remove redundant str_chomp from str_util.c.
git-svn-id: http://opkg.googlecode.com/svn/trunk@382 e8e0d7a0-c8d9-11dd-a880-a1081c7ac358
Diffstat (limited to 'libopkg/file_util.c')
-rw-r--r-- | libopkg/file_util.c | 127 |
1 files changed, 64 insertions, 63 deletions
diff --git a/libopkg/file_util.c b/libopkg/file_util.c index 08c801a..64e2bd3 100644 --- a/libopkg/file_util.c +++ b/libopkg/file_util.c @@ -30,30 +30,26 @@ #include "sha256.h" #endif -int file_exists(const char *file_name) +int +file_exists(const char *file_name) { - int err; - struct stat stat_buf; + struct stat st; + + if (stat(file_name, &st) == -1) + return 0; - err = stat(file_name, &stat_buf); - if (err == 0) { return 1; - } else { - return 0; - } } -int file_is_dir(const char *file_name) +int +file_is_dir(const char *file_name) { - int err; - struct stat stat_buf; + struct stat st; - err = stat(file_name, &stat_buf); - if (err) { - return 0; - } + if (stat(file_name, &st) == -1) + return 0; - return S_ISDIR(stat_buf.st_mode); + return S_ISDIR(st.st_mode); } /* read a single line from a file, stopping at a newline or EOF. @@ -63,68 +59,73 @@ int file_is_dir(const char *file_name) Return value is NULL if the file is at EOF when called. */ -#define FILE_READ_LINE_BUF_SIZE 1024 -char *file_read_line_alloc(FILE *file) +char * +file_read_line_alloc(FILE *fp) { - char buf[FILE_READ_LINE_BUF_SIZE]; - int buf_len; - char *line = NULL; - int line_size = 0; - - memset(buf, 0, FILE_READ_LINE_BUF_SIZE); - while (fgets(buf, FILE_READ_LINE_BUF_SIZE, file)) { - buf_len = strlen(buf); - if (line) { - line_size += buf_len; - line = xrealloc(line, line_size); - strcat(line, buf); - } else { - line_size = buf_len + 1; - line = xstrdup(buf); - } - if (buf[buf_len - 1] == '\n') { - break; + char buf[BUFSIZ]; + int buf_len; + char *line = NULL; + int line_size = 0; + + buf[0] = '\0'; + + while (fgets(buf, BUFSIZ, fp)) { + buf_len = strlen(buf); + if (line) { + line_size += buf_len; + line = xrealloc(line, line_size+1); + strncat(line, buf, line_size); + } else { + line_size = buf_len + 1; + line = xstrdup(buf); + } + if (buf[buf_len - 1] == '\n') { + buf[buf_len -1] = '\0'; + break; + } } - } - return line; + return line; } -int file_move(const char *src, const char *dest) +int +file_move(const char *src, const char *dest) { - int err; - - err = rename(src, dest); - - if (err && errno == EXDEV) { - err = file_copy(src, dest); - unlink(src); - } else if (err) { - fprintf(stderr, "%s: ERROR: failed to rename %s to %s: %s\n", - __FUNCTION__, src, dest, strerror(errno)); - } + int err; + + err = rename(src, dest); + if (err == -1) { + if (errno == EXDEV) { + /* src & dest live on different file systems */ + err = file_copy(src, dest); + if (err == 0) + unlink(src); + } else { + fprintf(stderr, "%s: rename(%s, %s): %s\n", + __FUNCTION__, src, dest, strerror(errno)); + } + } - return err; + return err; } -/* I put these here to keep libbb dependencies from creeping all over - the opkg code */ -int file_copy(const char *src, const char *dest) +int +file_copy(const char *src, const char *dest) { - int err; + int err; - err = copy_file(src, dest, FILEUTILS_FORCE | FILEUTILS_PRESERVE_STATUS); - if (err) { - fprintf(stderr, "%s: ERROR: failed to copy %s to %s\n", - __FUNCTION__, src, dest); - } + err = copy_file(src, dest, FILEUTILS_FORCE | FILEUTILS_PRESERVE_STATUS); + if (err) + fprintf(stderr, "%s: copy_file(%s, %s)\n", + __FUNCTION__, src, dest); - return err; + return err; } -int file_mkdir_hier(const char *path, long mode) +int +file_mkdir_hier(const char *path, long mode) { - return make_directory(path, mode, FILEUTILS_RECUR); + return make_directory(path, mode, FILEUTILS_RECUR); } char *file_md5sum_alloc(const char *file_name) |