From 9ba412631e57deb365d5e7fb8b8c2fbf9bfe27b0 Mon Sep 17 00:00:00 2001 From: "graham.gower" Date: Thu, 26 Nov 2009 01:42:27 +0000 Subject: [PATCH] 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 --- libopkg/file_util.c | 127 +++++++++++++++++++++-------------------- libopkg/opkg_conf.c | 3 - libopkg/opkg_install.c | 1 - libopkg/pkg.c | 1 - libopkg/str_util.c | 9 --- libopkg/str_util.h | 1 - libopkg/user.c | 1 - 7 files changed, 64 insertions(+), 79 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) diff --git a/libopkg/opkg_conf.c b/libopkg/opkg_conf.c index f6d6868..193ae5c 100644 --- a/libopkg/opkg_conf.c +++ b/libopkg/opkg_conf.c @@ -540,14 +540,11 @@ static int opkg_conf_parse_file(opkg_conf_t *conf, const char *filename, break; } - str_chomp(line); - if (regexec(&comment_re, line, 0, 0, 0) == 0) { goto NEXT_LINE; } if (regexec(&valid_line_re, line, regmatch_size, regmatch, 0) == REG_NOMATCH) { - str_chomp(line); fprintf(stderr, "%s:%d: Ignoring invalid line: `%s'\n", filename, line_num, line); goto NEXT_LINE; diff --git a/libopkg/opkg_install.c b/libopkg/opkg_install.c index 6003f4f..e848d99 100644 --- a/libopkg/opkg_install.c +++ b/libopkg/opkg_install.c @@ -268,7 +268,6 @@ unpack_pkg_control_files(opkg_conf_t *conf, pkg_t *pkg) if (cf_name == NULL) { break; } - str_chomp(cf_name); if (cf_name[0] == '\0') { continue; } diff --git a/libopkg/pkg.c b/libopkg/pkg.c index 111ac7a..58e92a0 100644 --- a/libopkg/pkg.c +++ b/libopkg/pkg.c @@ -1184,7 +1184,6 @@ pkg_get_installed_files(opkg_conf_t *conf, pkg_t *pkg) if (line == NULL) { break; } - str_chomp(line); file_name = line; if (pkg->state_status == SS_NOT_INSTALLED || pkg->dest == NULL) { diff --git a/libopkg/str_util.c b/libopkg/str_util.c index d583b7a..58fdb9c 100644 --- a/libopkg/str_util.c +++ b/libopkg/str_util.c @@ -37,15 +37,6 @@ int str_ends_with(const char *str, const char *suffix) return (strcmp(str + str_len - suffix_len, suffix) == 0); } -int str_chomp(char *str) -{ - if (str[strlen(str) - 1] == '\n') { - str[strlen(str) - 1] = '\0'; - return 1; - } - return 0; -} - int str_tolower(char *str) { while (*str) { diff --git a/libopkg/str_util.h b/libopkg/str_util.h index 832654f..02861e6 100644 --- a/libopkg/str_util.h +++ b/libopkg/str_util.h @@ -20,7 +20,6 @@ int str_starts_with(const char *str, const char *prefix); int str_ends_with(const char *str, const char *suffix); -int str_chomp(char *str); int str_tolower(char *str); int str_toupper(char *str); diff --git a/libopkg/user.c b/libopkg/user.c index 26f6f0e..e04f04d 100644 --- a/libopkg/user.c +++ b/libopkg/user.c @@ -37,7 +37,6 @@ char *get_user_response(const char *format, ...) if (response == NULL) return NULL; - str_chomp(response); str_tolower(response); return response; -- 2.25.1