sync with 0.8.3cvs3
[claws.git] / src / utils.c
index ed8314c3d249e0c36dbe7ce1a1ef61db5dfe43e0..ae249671532c7d61a7b9c5305e514ad2a3776e86 100644 (file)
@@ -1575,6 +1575,9 @@ gboolean file_exist(const gchar *file, gboolean allow_fifo)
 {
        struct stat s;
 
+       if (file == NULL)
+               return FALSE;
+
        if (stat(file, &s) < 0) {
                if (ENOENT != errno) FILE_OP_ERROR(file, "stat");
                return FALSE;
@@ -1590,6 +1593,9 @@ gboolean is_dir_exist(const gchar *dir)
 {
        struct stat s;
 
+       if (dir == NULL)
+               return FALSE;
+
        if (stat(dir, &s) < 0) {
                if (ENOENT != errno) FILE_OP_ERROR(dir, "stat");
                return FALSE;
@@ -1605,6 +1611,9 @@ gboolean is_file_entry_exist(const gchar *file)
 {
        struct stat s;
 
+       if (file == NULL)
+               return FALSE;
+
        if (stat(file, &s) < 0) {
                if (ENOENT != errno) FILE_OP_ERROR(file, "stat");
                return FALSE;
@@ -1685,11 +1694,13 @@ gint remove_all_files(const gchar *dir)
 
        if (chdir(dir) < 0) {
                FILE_OP_ERROR(dir, "chdir");
+               g_free(prev_dir);
                return -1;
        }
 
        if ((dp = opendir(".")) == NULL) {
                FILE_OP_ERROR(dir, "opendir");
+               g_free(prev_dir);
                return -1;
        }
 
@@ -1726,11 +1737,13 @@ gint remove_numbered_files(const gchar *dir, guint first, guint last)
 
        if (chdir(dir) < 0) {
                FILE_OP_ERROR(dir, "chdir");
+               g_free(prev_dir);
                return -1;
        }
 
        if ((dp = opendir(".")) == NULL) {
                FILE_OP_ERROR(dir, "opendir");
+               g_free(prev_dir);
                return -1;
        }
 
@@ -1775,11 +1788,13 @@ gint remove_expired_files(const gchar *dir, guint hours)
 
        if (chdir(dir) < 0) {
                FILE_OP_ERROR(dir, "chdir");
+               g_free(prev_dir);
                return -1;
        }
 
        if ((dp = opendir(".")) == NULL) {
                FILE_OP_ERROR(dir, "opendir");
+               g_free(prev_dir);
                return -1;
        }
 
@@ -2089,6 +2104,64 @@ gint move_file(const gchar *src, const gchar *dest, gboolean overwrite)
        return 0;
 }
 
+gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
+{
+       FILE *dest_fp;
+       gint n_read;
+       gint bytes_left, to_read;
+       gchar buf[BUFSIZ];
+       gboolean err = FALSE;
+
+       if (fseek(fp, offset, SEEK_SET) < 0) {
+               perror("fseek");
+               return -1;
+       }
+
+       if ((dest_fp = fopen(dest, "wb")) == NULL) {
+               FILE_OP_ERROR(dest, "fopen");
+               return -1;
+       }
+
+       if (change_file_mode_rw(dest_fp, dest) < 0) {
+               FILE_OP_ERROR(dest, "chmod");
+               g_warning("can't change file mode\n");
+       }
+
+       bytes_left = length;
+       to_read = MIN(bytes_left, sizeof(buf));
+
+       while ((n_read = fread(buf, sizeof(gchar), to_read, fp)) > 0) {
+               if (n_read < to_read && ferror(fp))
+                       break;
+               if (fwrite(buf, n_read, 1, dest_fp) < 1) {
+                       g_warning(_("writing to %s failed.\n"), dest);
+                       fclose(dest_fp);
+                       unlink(dest);
+                       return -1;
+               }
+               bytes_left -= n_read;
+               if (bytes_left == 0)
+                       break;
+               to_read = MIN(bytes_left, sizeof(buf));
+       }
+
+       if (ferror(fp)) {
+               perror("fread");
+               err = TRUE;
+       }
+       if (fclose(dest_fp) == EOF) {
+               FILE_OP_ERROR(dest, "fclose");
+               err = TRUE;
+       }
+
+       if (err) {
+               unlink(dest);
+               return -1;
+       }
+
+       return 0;
+}
+
 /* convert line endings into CRLF. If the last line doesn't end with
  * linebreak, add it.
  */