Coverity fixes
[claws.git] / src / partial_download.c
index 7377046dff76fe1a3d3494e64091b1798fed9b95..d7b0ce695921b78c83992b9c81da238ed5144e3d 100644 (file)
@@ -1,11 +1,11 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Claws Mail team
- * This file (C) 2004 Colin Leroy
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2012 Colin Leroy <colin@colino.net> 
+ * and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -14,8 +14,8 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 /* Partial download:
@@ -44,6 +44,7 @@
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #include <glib.h>
@@ -71,7 +72,6 @@ int partial_msg_in_uidl_list(MsgInfo *msginfo)
        gchar uidl[POPBUFSIZE];
        time_t recv_time;
        time_t now;
-       gint partial_recv;
        gchar *sanitized_uid = NULL;
        
        if (!msginfo->extradata)
@@ -111,7 +111,6 @@ int partial_msg_in_uidl_list(MsgInfo *msginfo)
                gchar tmp[POPBUFSIZE];
                strretchomp(buf);
                recv_time = RECV_TIME_NONE;
-               partial_recv = POP3_TOTALLY_RECEIVED;
                
                if (sscanf(buf, "%s\t%ld\t%s", uidl, (long int *) &recv_time, 
                           tmp) < 2) {
@@ -214,8 +213,15 @@ static int partial_uidl_mark_mail(MsgInfo *msginfo, int download)
                        }
                }
                if (strcmp(tinfo->extradata->partial_recv, uidl)) {
-                       fprintf(fpnew, "%s\t%ld\t%s\n", 
-                               uidl, (long int) recv_time, partial_recv);
+                       if (fprintf(fpnew, "%s\t%ld\t%s\n", 
+                               uidl, (long int) recv_time, partial_recv) < 0) {
+                               FILE_OP_ERROR(pathnew, "fprintf");
+                               fclose(fpnew);
+                               fclose(fp);
+                               g_free(path);
+                               g_free(pathnew);
+                               goto bail;
+                       }
                } else {
                        gchar *stat = NULL;
                        if (download == POP3_PARTIAL_DLOAD_DLOAD) {
@@ -230,12 +236,25 @@ static int partial_uidl_mark_mail(MsgInfo *msginfo, int download)
                        else if (download == POP3_PARTIAL_DLOAD_DELE)
                                stat = g_strdup("0");
                        
-                       fprintf(fpnew, "%s\t%ld\t%s\n", 
-                               uidl, (long int) recv_time, stat);
+                       if (fprintf(fpnew, "%s\t%ld\t%s\n", 
+                               uidl, (long int) recv_time, stat) < 0) {
+                               FILE_OP_ERROR(pathnew, "fprintf");
+                               fclose(fpnew);
+                               fclose(fp);
+                               g_free(path);
+                               g_free(pathnew);
+                               goto bail;
+                       }
                        g_free(stat);
                }
        }
-       fclose(fpnew);
+       if (fclose(fpnew) == EOF) {
+               FILE_OP_ERROR(pathnew, "fclose");
+               fclose(fp);
+               g_free(path);
+               g_free(pathnew);
+               goto bail;
+       }
        fclose(fp);
 
        move_file(pathnew, path, TRUE);
@@ -255,26 +274,49 @@ static int partial_uidl_mark_mail(MsgInfo *msginfo, int download)
                goto bail;
        }
        
-       fprintf(fpnew, "SC-Marked-For-Download: %d\n", 
-                       download);
+       if (fprintf(fpnew, "SC-Marked-For-Download: %d\n", 
+                       download) < 0) {
+               FILE_OP_ERROR(pathnew, "fprintf");
+               fclose(fpnew);
+               fclose(fp);
+               g_free(pathnew);
+               goto bail;
+       }
        while (fgets(buf, sizeof(buf)-1, fp) != NULL) {
                if(strlen(buf) > strlen("SC-Marked-For-Download: x\n")
                && !strncmp(buf, "SC-Marked-For-Download:", 
                            strlen("SC-Marked-For-Download:"))) {
-                       fprintf(fpnew, "%s", 
-                        buf+strlen("SC-Marked-For-Download: x\n"));
+                       if (fprintf(fpnew, "%s", 
+                        buf+strlen("SC-Marked-For-Download: x\n")) < 0) {
+                               FILE_OP_ERROR(pathnew, "fprintf");
+                               fclose(fpnew);
+                               fclose(fp);
+                               g_free(pathnew);
+                               goto bail;
+                       }
                        continue;
                } else if (strlen(buf) == strlen("SC-Marked-For-Download: x\n")
                && !strncmp(buf, "SC-Marked-For-Download:", 
                            strlen("SC-Marked-For-Download:"))) {
                        continue;
                }
-               fprintf(fpnew, "%s", buf);
+               if (fprintf(fpnew, "%s", buf) < 0) {
+                       FILE_OP_ERROR(pathnew, "fprintf");
+                       fclose(fpnew);
+                       fclose(fp);
+                       g_free(pathnew);
+                       goto bail;
+               }
        }
-       fclose(fpnew);
+       if (fclose(fpnew) == EOF) {
+               FILE_OP_ERROR(pathnew, "fclose");
+               fclose(fp);
+               g_free(pathnew);
+               goto bail;
+       }
+
        fclose(fp);
-       g_unlink(filename);
-       rename(pathnew, filename);
+       rename_force(pathnew, filename);
        g_free(pathnew);
        msginfo->planned_download = download;
        msgcache_update_msg(msginfo->folder->cache, msginfo);
@@ -309,7 +351,7 @@ void partial_delete_old(const gchar *file)
        int num = 0;
        FolderItem *item = NULL;
 
-       debug_print("too big message updated,should remove %s\n", file);
+       debug_print("too big message updated, should remove %s\n", file?file:"(null)");
 
        if (snum) {
                snum++;
@@ -341,7 +383,6 @@ gchar *partial_get_filename(const gchar *server, const gchar *login,
        gchar uidl[POPBUFSIZE];
        time_t recv_time;
        time_t now;
-       gint partial_recv;
        gchar *sanitized_uid = g_strdup(login); 
 
        subst_for_filename(sanitized_uid);
@@ -371,7 +412,6 @@ gchar *partial_get_filename(const gchar *server, const gchar *login,
                gchar tmp[POPBUFSIZE];
                strretchomp(buf);
                recv_time = RECV_TIME_NONE;
-               partial_recv = POP3_TOTALLY_RECEIVED;
                
                if (sscanf(buf, "%s\t%ld\t%s", uidl, (long int *) &recv_time, 
                           tmp) < 2) {