libopkg: make MD5 support optional
[oweals/opkg-lede.git] / libopkg / file_util.c
index 64e2bd3ec58ebcbad8b7bc0abaa1adc46ced0597..c54903c30b66907d126cd6ebb5ed0b58759e0f34 100644 (file)
@@ -1,7 +1,8 @@
 /* file_util.c - convenience routines for common stat operations
 
-   Carl D. Worth
+   Copyright (C) 2009 Ubiq Technologies <graham.gower@gmail.com>
 
+   Carl D. Worth
    Copyright (C) 2001 University of Southern California
 
    This program is free software; you can redistribute it and/or
    General Public License for more details.
 */
 
-#include "includes.h"
+#include "config.h"
+
+#include <stdio.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <dirent.h>
+#include <unistd.h>
 
 #include "sprintf_alloc.h"
 #include "file_util.h"
+#ifdef HAVE_MD5
 #include "md5.h"
+#endif
 #include "libbb/libbb.h"
-#undef strlen
 
 #if defined HAVE_SHA256
 #include "sha256.h"
@@ -63,14 +68,20 @@ char *
 file_read_line_alloc(FILE *fp)
 {
        char buf[BUFSIZ];
-       int buf_len;
+       unsigned int buf_len;
        char *line = NULL;
-       int line_size = 0;
+       unsigned int line_size = 0;
+       int got_nl = 0;
 
        buf[0] = '\0';
 
        while (fgets(buf, BUFSIZ, fp)) {
                buf_len = strlen(buf);
+               if (buf[buf_len - 1] == '\n') {
+                       buf_len--;
+                       buf[buf_len] = '\0';
+                       got_nl = 1;
+               }
                if (line) {
                        line_size += buf_len;
                        line = xrealloc(line, line_size+1);
@@ -79,10 +90,8 @@ file_read_line_alloc(FILE *fp)
                        line_size = buf_len + 1;
                        line = xstrdup(buf);
                }
-               if (buf[buf_len - 1] == '\n') {
-                       buf[buf_len -1] = '\0';
+               if (got_nl)
                        break;
-               }
        }
 
        return line;
@@ -101,8 +110,8 @@ file_move(const char *src, const char *dest)
                        if (err == 0)
                                unlink(src);
                } else {
-                       fprintf(stderr, "%s: rename(%s, %s): %s\n",
-                               __FUNCTION__, src, dest, strerror(errno));
+                       opkg_perror(ERROR, "Failed to rename %s to %s",
+                               src, dest);
                }
        }
 
@@ -116,8 +125,8 @@ file_copy(const char *src, const char *dest)
 
        err = copy_file(src, dest, FILEUTILS_FORCE | FILEUTILS_PRESERVE_STATUS);
        if (err)
-               fprintf(stderr, "%s: copy_file(%s, %s)\n",
-                               __FUNCTION__, src, dest);
+               opkg_msg(ERROR, "Failed to copy file %s to %s.\n",
+                               src, dest);
 
        return err;
 }
@@ -128,6 +137,7 @@ file_mkdir_hier(const char *path, long mode)
        return make_directory(path, mode, FILEUTILS_RECUR);
 }
 
+#ifdef HAVE_MD5
 char *file_md5sum_alloc(const char *file_name)
 {
     static const int md5sum_bin_len = 16;
@@ -149,16 +159,14 @@ char *file_md5sum_alloc(const char *file_name)
 
     file = fopen(file_name, "r");
     if (file == NULL) {
-       fprintf(stderr, "%s: Failed to open file %s: %s\n",
-               __FUNCTION__, file_name, strerror(errno));
+       opkg_perror(ERROR, "Failed to open file %s", file_name);
        free(md5sum_hex);
        return NULL;
     }
 
     err = md5_stream(file, md5sum_bin);
     if (err) {
-       fprintf(stderr, "%s: ERROR computing md5sum for %s: %s\n",
-               __FUNCTION__, file_name, strerror(err));
+       opkg_msg(ERROR, "Could't compute md5sum for %s.\n", file_name);
        fclose(file);
        free(md5sum_hex);
        return NULL;
@@ -170,11 +178,12 @@ char *file_md5sum_alloc(const char *file_name)
        md5sum_hex[i*2] = bin2hex[md5sum_bin[i] >> 4];
        md5sum_hex[i*2+1] = bin2hex[md5sum_bin[i] & 0xf];
     }
-    
+
     md5sum_hex[md5sum_hex_len] = '\0';
-    
+
     return md5sum_hex;
 }
+#endif
 
 #ifdef HAVE_SHA256
 char *file_sha256sum_alloc(const char *file_name)
@@ -198,16 +207,14 @@ char *file_sha256sum_alloc(const char *file_name)
 
     file = fopen(file_name, "r");
     if (file == NULL) {
-       fprintf(stderr, "%s: Failed to open file %s: %s\n",
-               __FUNCTION__, file_name, strerror(errno));
+       opkg_perror(ERROR, "Failed to open file %s", file_name);
        free(sha256sum_hex);
        return NULL;
     }
 
     err = sha256_stream(file, sha256sum_bin);
     if (err) {
-       fprintf(stderr, "%s: ERROR computing sha256sum for %s: %s\n",
-               __FUNCTION__, file_name, strerror(err));
+       opkg_msg(ERROR, "Could't compute sha256sum for %s.\n", file_name);
        fclose(file);
        free(sha256sum_hex);
        return NULL;
@@ -219,9 +226,9 @@ char *file_sha256sum_alloc(const char *file_name)
        sha256sum_hex[i*2] = bin2hex[sha256sum_bin[i] >> 4];
        sha256sum_hex[i*2+1] = bin2hex[sha256sum_bin[i] & 0xf];
     }
-    
+
     sha256sum_hex[sha256sum_hex_len] = '\0';
-    
+
     return sha256sum_hex;
 }
 
@@ -235,14 +242,19 @@ rm_r(const char *path)
        DIR *dir;
        struct dirent *dent;
 
+       if (path == NULL) {
+               opkg_perror(ERROR, "Missing directory parameter");
+               return -1;
+       }
+
        dir = opendir(path);
        if (dir == NULL) {
-               perror_msg("%s: opendir(%s)", __FUNCTION__, path);
+               opkg_perror(ERROR, "Failed to open dir %s", path);
                return -1;
        }
 
        if (fchdir(dirfd(dir)) == -1) {
-               perror_msg("%s: fchdir(%s)", __FUNCTION__, path);
+               opkg_perror(ERROR, "Failed to change to dir %s", path);
                closedir(dir);
                return -1;
        }
@@ -251,8 +263,8 @@ rm_r(const char *path)
                errno = 0;
                if ((dent = readdir(dir)) == NULL) {
                        if (errno) {
-                               perror_msg("%s: readdir(%s)",
-                                               __FUNCTION__, path);
+                               opkg_perror(ERROR, "Failed to read dir %s",
+                                               path);
                                ret = -1;
                        }
                        break;
@@ -271,8 +283,8 @@ rm_r(const char *path)
                {
                        struct stat st;
                        if ((ret = lstat(dent->d_name, &st)) == -1) {
-                               perror_msg("%s: lstat(%s)",
-                                               __FUNCTION__, dent->d_name);
+                               opkg_perror(ERROR, "Failed to lstat %s",
+                                               dent->d_name);
                                break;
                        }
                        if (S_ISDIR(st.st_mode)) {
@@ -283,25 +295,24 @@ rm_r(const char *path)
                }
 
                if ((ret = unlink(dent->d_name)) == -1) {
-                       perror_msg("%s: unlink(%s)",
-                                       __FUNCTION__, dent->d_name);
+                       opkg_perror(ERROR, "Failed to unlink %s", dent->d_name);
                        break;
                }
        }
 
        if (chdir("..") == -1) {
                ret = -1;
-               perror_msg("%s: chdir(%s/..)", __FUNCTION__, path);
+               opkg_perror(ERROR, "Failed to change to dir %s/..", path);
        }
 
        if (rmdir(path) == -1 ) {
                ret = -1;
-               perror_msg("%s: rmdir(%s)", __FUNCTION__, path);
+               opkg_perror(ERROR, "Failed to remove dir %s", path);
        }
 
        if (closedir(dir) == -1) {
                ret = -1;
-               perror_msg("%s: closedir(%s)", __FUNCTION__, path);
+               opkg_perror(ERROR, "Failed to close dir %s", path);
        }
 
        return ret;