Merge branch 'master' of ssh://git.claws-mail.org/home/git/claws
authorColin Leroy <colin@colino.net>
Sun, 7 Oct 2018 18:26:50 +0000 (20:26 +0200)
committerColin Leroy <colin@colino.net>
Sun, 7 Oct 2018 18:26:50 +0000 (20:26 +0200)
120 files changed:
configure.ac
src/account.c
src/addrbook.c
src/addrclip.c
src/addressbook.c
src/addrharvest.c
src/addrindex.c
src/addritem.c
src/addrmerge.c
src/advsearch.c
src/autofaces.c
src/common/Makefile.am
src/common/claws_io.c [deleted file]
src/common/claws_io.h [deleted file]
src/common/file-utils.c [new file with mode: 0644]
src/common/file-utils.h [new file with mode: 0644]
src/common/log.c
src/common/plugin.c
src/common/prefs.c
src/common/ssl_certificate.c
src/common/tags.c
src/common/template.c
src/common/utils.c
src/common/utils.h
src/common/xml.c
src/common/xml.h
src/common/xmlprops.c
src/compose.c
src/crash.c
src/editaddress.c
src/enriched.c
src/etpan/imap-thread.c
src/exporthtml.c
src/exportldif.c
src/file_checker.c
src/folder.c
src/gtk/about.c
src/html.c
src/image_viewer.c
src/imap.c
src/inc.c
src/jpilot.c
src/ldapquery.c
src/ldif.c
src/main.c
src/matcher.c
src/mbox.c
src/messageview.c
src/mh.c
src/mimeview.c
src/msgcache.c
src/mutt.c
src/news.c
src/partial_download.c
src/passwordstore.c
src/pine.c
src/plugins/acpi_notifier/acpi_notifier.c
src/plugins/archive/archiver_gtk.c
src/plugins/bogofilter/bogofilter.c
src/plugins/clamd/libclamd/clamd-plugin.c
src/plugins/dillo/dillo_viewer.c
src/plugins/fancy/claws.def
src/plugins/fancy/fancy_prefs.c
src/plugins/fancy/fancy_viewer.c
src/plugins/libravatar/claws.def
src/plugins/libravatar/libravatar_cache.c
src/plugins/libravatar/libravatar_image.c
src/plugins/libravatar/libravatar_missing.c
src/plugins/mailmbox/claws.def
src/plugins/mailmbox/mailimf.c
src/plugins/mailmbox/mailimf_write.c
src/plugins/mailmbox/mailmbox.c
src/plugins/mailmbox/mailmbox_folder.c
src/plugins/newmail/newmail.c
src/plugins/pdf_viewer/poppler_viewer.c
src/plugins/perl/perl_plugin.c
src/plugins/pgpcore/claws.def
src/plugins/pgpcore/pgp_utils.c
src/plugins/pgpcore/sgpgme.c
src/plugins/pgpinline/claws.def
src/plugins/pgpinline/pgpinline.c
src/plugins/pgpmime/claws.def
src/plugins/pgpmime/pgpmime.c
src/plugins/python/python_plugin.c
src/plugins/rssyl/claws.def
src/plugins/rssyl/opml_export.c
src/plugins/rssyl/rssyl.c
src/plugins/rssyl/rssyl_add_item.c
src/plugins/rssyl/rssyl_deleted.c
src/plugins/rssyl/rssyl_update_format.c
src/plugins/smime/claws.def
src/plugins/smime/smime.c
src/plugins/spam_report/spam_report.c
src/plugins/spamassassin/libspamc.c
src/plugins/spamassassin/spamassassin.c
src/plugins/tnef_parse/claws.def
src/plugins/tnef_parse/tnef_parse.c
src/plugins/vcalendar/claws.def
src/plugins/vcalendar/vcal_folder.c
src/plugins/vcalendar/vcal_manager.c
src/plugins/vcalendar/vcal_meeting_gtk.c
src/plugins/vcalendar/vcalendar.c
src/pop.c
src/prefs_account.c
src/prefs_actions.c
src/prefs_common.c
src/prefs_customheader.c
src/prefs_display_header.c
src/prefs_gtk.c
src/prefs_themes.c
src/procheader.c
src/procmime.c
src/procmsg.c
src/quote_fmt_parse.y
src/send_message.c
src/sourcewindow.c
src/summaryview.c
src/textview.c
src/vcard.c
src/wizard.c

index c6ef76f..a5f1bc9 100644 (file)
@@ -180,7 +180,7 @@ if test x"$_gcc_psign" = xyes ; then
        CFLAGS="$CFLAGS -Wno-pointer-sign"
 fi
 
-CFLAGS="$CFLAGS -Wall"
+CFLAGS="$CFLAGS -Wall -D_GNU_SOURCE"
 
 if test $USE_MAINTAINER_MODE = yes; then
        CFLAGS="$CFLAGS -g -Wno-pointer-sign -DUSE_MAINTAINER_MODE"
@@ -427,7 +427,7 @@ AC_CHECK_FUNCS(gethostname mkdir mktime socket strstr strchr \
               uname flock lockf inet_aton inet_addr \
               fchmod mkstemp truncate getuid regcomp)
 
-AC_CHECK_FUNCS(fgets_unlocked fwrite_unlocked fmemopen)
+AC_CHECK_FUNCS(fgets_unlocked fgetc_unlocked fputs_unlocked fputc_unlocked fread_unlocked fwrite_unlocked feof_unlocked ferror_unlocked fmemopen)
 
 dnl *****************
 dnl ** common code **
index e73a012..6f8521e 100644 (file)
@@ -55,6 +55,7 @@
 #include "prefs_actions.h"
 #include "hooks.h"
 #include "passwordstore.h"
+#include "file-utils.h"
 
 enum {
        ACCOUNT_IS_DEFAULT,
@@ -194,14 +195,14 @@ void account_read_config_all(void)
        debug_print("Reading all config for each account...\n");
 
        rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACCOUNT_RC, NULL);
-       if ((fp = g_fopen(rcpath, "rb")) == NULL) {
-               if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
+       if ((fp = claws_fopen(rcpath, "rb")) == NULL) {
+               if (ENOENT != errno) FILE_OP_ERROR(rcpath, "claws_fopen");
                g_free(rcpath);
                return;
        }
        g_free(rcpath);
 
-       while (fgets(buf, sizeof(buf), fp) != NULL) {
+       while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
                if (!strncmp(buf, "[Account: ", 10)) {
                        strretchomp(buf);
                        memmove(buf, buf + 1, sizeof(buf) - 1);
@@ -211,7 +212,7 @@ void account_read_config_all(void)
                                                       g_strdup(buf));
                }
        }
-       fclose(fp);
+       claws_fclose(fp);
 
        /* read config data from file */
        cur_account = NULL;
index 0720c6b..8087996 100644 (file)
 
 /* General functions for accessing address book files */
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#include "claws-features.h"
+#endif
+
 #include <glib.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -33,7 +38,7 @@
 #include "addrcache.h"
 #include "addrbook.h"
 #include "adbookbase.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #ifndef DEV_STANDALONE
 #include "prefs_gtk.h"
@@ -951,11 +956,11 @@ static int addrbook_write_elem_s(FILE *fp, gint lvl, gchar *name)
 {
        gint i;
        for (i = 0; i < lvl; i++) 
-               if (fputs("  ", fp) == EOF)
+               if (claws_fputs("  ", fp) == EOF)
                        return -1;
-       if (fputs("<", fp) == EOF)
+       if (claws_fputs("<", fp) == EOF)
                return -1;
-       if (fputs(name, fp) == EOF)
+       if (claws_fputs(name, fp) == EOF)
                return -1;
                
        return 0;
@@ -971,13 +976,13 @@ static int addrbook_write_elem_e(FILE *fp, gint lvl, gchar *name)
 {
        gint i;
        for(i = 0; i < lvl; i++)
-               if (fputs("  ", fp) == EOF)
+               if (claws_fputs("  ", fp) == EOF)
                        return -1;
-       if (fputs("</", fp) == EOF)
+       if (claws_fputs("</", fp) == EOF)
                return -1;
-       if (fputs(name, fp) == EOF)
+       if (claws_fputs(name, fp) == EOF)
                return -1;
-       if (fputs(">\n", fp) == EOF)
+       if (claws_fputs(">\n", fp) == EOF)
                return -1;
                
        return 0;
@@ -991,15 +996,15 @@ static int addrbook_write_elem_e(FILE *fp, gint lvl, gchar *name)
  */
 static int addrbook_write_attr(FILE *fp, gchar *name, gchar *value)
 {
-       if (fputs(" ", fp) == EOF)
+       if (claws_fputs(" ", fp) == EOF)
                return -1;
-       if (fputs(name, fp) == EOF)
+       if (claws_fputs(name, fp) == EOF)
                return -1;
-       if (fputs("=\"", fp) == EOF)
+       if (claws_fputs("=\"", fp) == EOF)
                return -1;
        if (xml_file_put_escape_str(fp, value) < 0)
                return -1;
-       if (fputs("\"", fp) == EOF)
+       if (claws_fputs("\"", fp) == EOF)
                return -1;
        
        return 0;
@@ -1042,13 +1047,13 @@ static void addrbook_write_item_person_vis(gpointer key, gpointer value,
                                data->error = TRUE;
                        if (addrbook_write_attr(fp, AB_ATTAG_COMMON_NAME, ADDRITEM_NAME(person)) < 0)
                                data->error = TRUE;
-                       if (fputs(" >\n", fp) == EOF)
+                       if (claws_fputs(" >\n", fp) == EOF)
                                data->error = TRUE;
 
                        /* Output email addresses */
                        if (addrbook_write_elem_s(fp, 2, AB_ELTAG_ADDRESS_LIST) < 0)
                                data->error = TRUE;
-                       if (fputs(">\n", fp) == EOF)
+                       if (claws_fputs(">\n", fp) == EOF)
                                data->error = TRUE;
                        node = person->listEMail;
                        while (node) {
@@ -1063,7 +1068,7 @@ static void addrbook_write_item_person_vis(gpointer key, gpointer value,
                                        data->error = TRUE;
                                if (addrbook_write_attr(fp, AB_ATTAG_REMARKS, email->remarks) < 0)
                                        data->error = TRUE;
-                               if (fputs(" />\n", fp) == EOF)
+                               if (claws_fputs(" />\n", fp) == EOF)
                                        data->error = TRUE;
                                node = g_list_next(node);
                        }
@@ -1073,7 +1078,7 @@ static void addrbook_write_item_person_vis(gpointer key, gpointer value,
                        /* Output user attributes */
                        if (addrbook_write_elem_s(fp, 2, AB_ELTAG_ATTRIBUTE_LIST) < 0)
                                data->error = TRUE;
-                       if (fputs(">\n", fp) == EOF)
+                       if (claws_fputs(">\n", fp) == EOF)
                                data->error = TRUE;
                        node = person->listAttrib;
                        while (node) {
@@ -1084,7 +1089,7 @@ static void addrbook_write_item_person_vis(gpointer key, gpointer value,
                                        data->error = TRUE;
                                if (addrbook_write_attr(fp, AB_ATTAG_NAME, attrib->name) < 0)
                                        data->error = TRUE;
-                               if (fputs(" >", fp) == EOF)
+                               if (claws_fputs(" >", fp) == EOF)
                                        data->error = TRUE;
                                if (xml_file_put_escape_str(fp, attrib->value) < 0)
                                        data->error = TRUE;
@@ -1129,13 +1134,13 @@ static void addrbook_write_item_group_vis(gpointer key, gpointer value,
                                data->error = TRUE;
                        if (addrbook_write_attr(fp, AB_ATTAG_REMARKS, group->remarks) < 0)
                                data->error = TRUE;
-                       if (fputs(" >\n", fp) == EOF)
+                       if (claws_fputs(" >\n", fp) == EOF)
                                data->error = TRUE;
 
                        /* Output email address links */
                        if (addrbook_write_elem_s(fp, 2, AB_ELTAG_MEMBER_LIST) < 0)
                                data->error = TRUE;
-                       if (fputs(">\n", fp) == EOF)
+                       if (claws_fputs(">\n", fp) == EOF)
                                data->error = TRUE;
                        node = group->listEMail;
                        while (node) {
@@ -1147,7 +1152,7 @@ static void addrbook_write_item_group_vis(gpointer key, gpointer value,
                                        data->error = TRUE;
                                if (addrbook_write_attr(fp, AB_ATTAG_EID, ADDRITEM_ID(email)) < 0)
                                        data->error = TRUE;
-                               if (fputs(" />\n", fp) == EOF)
+                               if (claws_fputs(" />\n", fp) == EOF)
                                        data->error = TRUE;
                                node = g_list_next(node);
                        }
@@ -1187,11 +1192,11 @@ static void addrbook_write_item_folder_vis(gpointer key, gpointer value,
                                data->error = TRUE;
                        if (addrbook_write_attr(fp, AB_ATTAG_REMARKS, folder->remarks) < 0)
                                data->error = TRUE;
-                       if (fputs(" >\n", fp) == EOF)
+                       if (claws_fputs(" >\n", fp) == EOF)
                                data->error = TRUE;
                        if (addrbook_write_elem_s(fp, 2, AB_ELTAG_ITEM_LIST) < 0)
                                data->error = TRUE;
-                       if (fputs(">\n", fp) == EOF)
+                       if (claws_fputs(">\n", fp) == EOF)
                                data->error = TRUE;
 
                        /* Output persons */
@@ -1204,7 +1209,7 @@ static void addrbook_write_item_folder_vis(gpointer key, gpointer value,
                                        data->error = TRUE;
                                if (addrbook_write_attr(fp, AB_ATTAG_UID, ADDRITEM_ID(item)) < 0)
                                        data->error = TRUE;
-                               if (fputs(" />\n", fp) == EOF)
+                               if (claws_fputs(" />\n", fp) == EOF)
                                        data->error = TRUE;
                                node = g_list_next(node);
                        }
@@ -1219,7 +1224,7 @@ static void addrbook_write_item_folder_vis(gpointer key, gpointer value,
                                        data->error = TRUE;
                                if (addrbook_write_attr(fp, AB_ATTAG_UID, ADDRITEM_ID(item)) < 0)
                                        data->error = TRUE;
-                               if (fputs(" />\n", fp) == EOF)
+                               if (claws_fputs(" />\n", fp) == EOF)
                                        data->error = TRUE;
                                node = g_list_next(node);
                        }
@@ -1234,7 +1239,7 @@ static void addrbook_write_item_folder_vis(gpointer key, gpointer value,
                                        data->error = TRUE;
                                if (addrbook_write_attr(fp, AB_ATTAG_UID, ADDRITEM_ID(item)) < 0)
                                        data->error = TRUE;
-                               if (fputs(" />\n", fp) == EOF)
+                               if (claws_fputs(" />\n", fp) == EOF)
                                        data->error = TRUE;
                                node = g_list_next(node);
                        }
@@ -1268,10 +1273,10 @@ static gint addrbook_write_to(AddressBookFile *book, gchar *newFile)
 
        book->retVal = MGU_OPEN_FILE;
 #ifdef DEV_STANDALONE
-       fp = g_fopen(fileSpec, "wb");
+       fp = claws_fopen(fileSpec, "wb");
        g_free(fileSpec);
        if (fp) {
-               if (fputs("<?xml version=\"1.0\" ?>\n", fp) == EOF) {
+               if (claws_fputs("<?xml version=\"1.0\" ?>\n", fp) == EOF) {
                        book->retVal = MGU_ERROR_WRITE;
                        return book->retVal;
                }
@@ -1288,7 +1293,7 @@ static gint addrbook_write_to(AddressBookFile *book, gchar *newFile)
                if (addrbook_write_attr(fp, AB_ATTAG_NAME,
                                    addrcache_get_name(book->addressCache)) < 0)
                        goto fail;
-               if (fputs(" >\n", fp) == EOF)
+               if (claws_fputs(" >\n", fp) == EOF)
                        goto fail;
 
                /* Output all persons */
@@ -1319,7 +1324,7 @@ static gint addrbook_write_to(AddressBookFile *book, gchar *newFile)
 
                book->retVal = MGU_SUCCESS;
 #ifdef DEV_STANDALONE
-               safe_fclose(fp);
+               claws_safe_fclose(fp);
 #else
                if (prefs_file_close( pfile ) < 0)
                        book->retVal = MGU_ERROR_WRITE;
index 90180c9..58d5947 100644 (file)
@@ -56,6 +56,7 @@
 #include "addrclip.h"
 #include "alertpanel.h"
 #include "defs.h"
+#include "file-utils.h"
 
 /*
 * Create a clipboard.
index a5a0eba..342163e 100644 (file)
@@ -44,6 +44,7 @@
 #include "xml.h"
 #include "prefs_gtk.h"
 #include "procmime.h"
+#include "file-utils.h"
 #include "utils.h"
 #include "gtkutils.h"
 #include "codeconv.h"
index 6d4bf3b..d837293 100644 (file)
  * Functions for an E-Mail address harvester.
  */
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#include "claws-features.h"
+#endif
+
 #include <sys/stat.h>
 #include <dirent.h>
 #include <glib.h>
@@ -35,6 +40,7 @@
 #ifdef USE_ALT_ADDRBOOK
        #include "addressbook-dbus.h"
 #endif
+#include "file-utils.h"
 
 /* Mail header names of interest */
 static gchar *_headerFrom_     = HEADER_FROM;
@@ -644,7 +650,7 @@ static GSList *addrharvest_get_header( FILE *fp, GList *listHdr, gboolean *done
        list = NULL;
 
        /* Read line */
-       if( fgets( buf, MSG_BUFFSIZE, fp ) == NULL ) {
+       if( claws_fgets( buf, MSG_BUFFSIZE, fp ) == NULL ) {
                *done = TRUE;
                return list;
        }
@@ -671,7 +677,7 @@ static GSList *addrharvest_get_header( FILE *fp, GList *listHdr, gboolean *done
                ch = fgetc( fp );
                if( ch == ' ' || ch == '\t' ) {
                        /* Continuation character - read into buffer */
-                       if( fgets( buf, MSG_BUFFSIZE, fp ) == NULL ) {
+                       if( claws_fgets( buf, MSG_BUFFSIZE, fp ) == NULL ) {
                                break;
                        }
                }
@@ -708,7 +714,7 @@ static gint addrharvest_readfile(
        GSList *list;
        gboolean done;
 
-       msgFile = g_fopen( fileName, "rb" );
+       msgFile = claws_fopen( fileName, "rb" );
        if( ! msgFile ) {
                /* Cannot open file */
                retVal = MGU_OPEN_FILE;
@@ -747,7 +753,7 @@ static gint addrharvest_readfile(
                g_free( buf );
        }
 
-       fclose( msgFile );
+       claws_fclose( msgFile );
        return MGU_SUCCESS;
 }
 
index f1adf01..82c2fed 100644 (file)
@@ -43,7 +43,7 @@
 #include "utils.h"
 #include "alertpanel.h"
 #include "passwordstore.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #ifndef DEV_STANDALONE
 #include "prefs_gtk.h"
@@ -937,11 +937,11 @@ static AddressInterface *addrindex_tag_get_datasource(
 static int addrindex_write_elem_s( FILE *fp, const gint lvl, const gchar *name ) {
        gint i;
        for( i = 0; i < lvl; i++ ) 
-               if (fputs( "  ", fp ) == EOF)
+               if (claws_fputs( "  ", fp ) == EOF)
                        return -1;
-       if (fputs( "<", fp ) == EOF)
+       if (claws_fputs( "<", fp ) == EOF)
                return -1;
-       if (fputs( name, fp ) == EOF)
+       if (claws_fputs( name, fp ) == EOF)
                return -1;
        return 0;
 }
@@ -955,13 +955,13 @@ static int addrindex_write_elem_s( FILE *fp, const gint lvl, const gchar *name )
 static int addrindex_write_elem_e( FILE *fp, const gint lvl, const gchar *name ) {
        gint i;
        for( i = 0; i < lvl; i++ ) 
-               if (fputs( "  ", fp ) == EOF)
+               if (claws_fputs( "  ", fp ) == EOF)
                        return -1;
-       if (fputs( "</", fp ) == EOF)
+       if (claws_fputs( "</", fp ) == EOF)
                return -1;
-       if (fputs( name, fp ) == EOF)
+       if (claws_fputs( name, fp ) == EOF)
                return -1;
-       if (fputs( ">\n", fp ) == EOF)
+       if (claws_fputs( ">\n", fp ) == EOF)
                return -1;
        return 0;
 }
@@ -973,15 +973,15 @@ static int addrindex_write_elem_e( FILE *fp, const gint lvl, const gchar *name )
  * \param value Attribute value.
  */
 static int addrindex_write_attr( FILE *fp, const gchar *name, const gchar *value ) {
-       if (fputs( " ", fp ) == EOF)
+       if (claws_fputs( " ", fp ) == EOF)
                return -1;
-       if (fputs( name, fp ) == EOF)
+       if (claws_fputs( name, fp ) == EOF)
                return -1;
-       if (fputs( "=\"", fp ) == EOF)
+       if (claws_fputs( "=\"", fp ) == EOF)
                return -1;
        if (xml_file_put_escape_str( fp, value ) < 0)
                return -1;
-       if (fputs( "\"", fp ) == EOF)
+       if (claws_fputs( "\"", fp ) == EOF)
                return -1;
        return 0;
 }
@@ -1071,7 +1071,7 @@ static int addrindex_write_fragment(
                        node = g_list_next( node );
                }
                if( fragment->children ) {
-                       if (fputs(" >\n", fp) == EOF)
+                       if (claws_fputs(" >\n", fp) == EOF)
                                return -1;
 
                        /* Output children */
@@ -1088,7 +1088,7 @@ static int addrindex_write_fragment(
                                return -1;
                }
                else {
-                       if (fputs(" />\n", fp) == EOF)
+                       if (claws_fputs(" />\n", fp) == EOF)
                                return -1;
                }
        }
@@ -1134,7 +1134,7 @@ static int addrindex_write_book( FILE *fp, AddressDataSource *ds, gint lvl ) {
                        return -1;
                if (addrindex_write_attr( fp, ATTAG_BOOK_FILE, abf->fileName ) < 0)
                        return -1;
-               if (fputs( " />\n", fp ) == EOF)
+               if (claws_fputs( " />\n", fp ) == EOF)
                        return -1;
        }
        return 0;
@@ -1172,7 +1172,7 @@ static int addrindex_write_vcard( FILE *fp, AddressDataSource *ds, gint lvl ) {
                        return -1;
                if (addrindex_write_attr( fp, ATTAG_VCARD_FILE, vcf->path ) < 0)
                        return -1;
-               if (fputs( " />\n", fp ) == EOF)
+               if (claws_fputs( " />\n", fp ) == EOF)
                        return -1;
        }
        return 0;
@@ -1237,7 +1237,7 @@ static int addrindex_write_jpilot( FILE *fp,AddressDataSource *ds, gint lvl ) {
                        ind++;
                        node = g_list_next( node );
                }
-               if (fputs( " />\n", fp ) == EOF)
+               if (claws_fputs( " />\n", fp ) == EOF)
                        return -1;
        }
        return 0;
@@ -1519,7 +1519,7 @@ static int addrindex_write_ldap( FILE *fp, AddressDataSource *ds, gint lvl ) {
                        ATVAL_BOOLEAN_YES : ATVAL_BOOLEAN_NO ) < 0)
                return -1;
 
-       if (fputs(" >\n", fp) == EOF)
+       if (claws_fputs(" >\n", fp) == EOF)
                return -1;
 
        /* Output attributes */
@@ -1529,7 +1529,7 @@ static int addrindex_write_ldap( FILE *fp, AddressDataSource *ds, gint lvl ) {
                        return -1;
                if (addrindex_write_attr( fp, ATTAG_LDAP_ATTR_NAME, node->data ) < 0)
                        return -1;
-               if (fputs(" />\n", fp) == EOF)
+               if (claws_fputs(" />\n", fp) == EOF)
                        return -1;
                node = g_list_next( node );
        }
@@ -1708,7 +1708,7 @@ static int addrindex_write_index( AddressIndex *addrIndex, FILE *fp ) {
                        nodeDS = iface->listSource;
                        if (addrindex_write_elem_s( fp, lvlList, iface->listTag ) < 0)
                                return -1;
-                       if (fputs( ">\n", fp ) == EOF)
+                       if (claws_fputs( ">\n", fp ) == EOF)
                                return -1;
                        while( nodeDS ) {
                                AddressDataSource *ds = nodeDS->data;
@@ -1759,10 +1759,10 @@ static gint addrindex_write_to( AddressIndex *addrIndex, const gchar *newFile )
        fileSpec = g_strconcat( addrIndex->filePath, G_DIR_SEPARATOR_S, newFile, NULL );
        addrIndex->retVal = MGU_OPEN_FILE;
 #ifdef DEV_STANDALONE
-       fp = g_fopen( fileSpec, "wb" );
+       fp = claws_fopen( fileSpec, "wb" );
        g_free( fileSpec );
        if( fp ) {
-               fputs( "<?xml version=\"1.0\" ?>\n", fp );
+               claws_fputs( "<?xml version=\"1.0\" ?>\n", fp );
 #else
        pfile = prefs_write_open( fileSpec );
        g_free( fileSpec );
@@ -1773,7 +1773,7 @@ static gint addrindex_write_to( AddressIndex *addrIndex, const gchar *newFile )
 #endif
                if (addrindex_write_elem_s( fp, 0, TAG_ADDRESS_INDEX ) < 0)
                        goto fail;
-               if (fputs( ">\n", fp ) == EOF)
+               if (claws_fputs( ">\n", fp ) == EOF)
                        goto fail;
 
                if (addrindex_write_index( addrIndex, fp ) < 0)
@@ -1783,7 +1783,7 @@ static gint addrindex_write_to( AddressIndex *addrIndex, const gchar *newFile )
 
                addrIndex->retVal = MGU_SUCCESS;
 #ifdef DEV_STANDALONE
-               safe_fclose( fp );
+               claws_safe_fclose( fp );
 #else
                if( prefs_file_close( pfile ) < 0 ) {
                        addrIndex->retVal = MGU_ERROR_WRITE;
index 3a81826..cb76257 100644 (file)
@@ -25,6 +25,7 @@
 #include <string.h>
 
 #include "defs.h"
+#include "file-utils.h"
 #include "utils.h"
 #include "addritem.h"
 #include "mgutils.h"
index 363bab7..6aa8315 100644 (file)
@@ -38,6 +38,7 @@
 #include "addrmerge.h"
 #include "alertpanel.h"
 #include "gtkutils.h"
+#include "file-utils.h"
 #include "utils.h"
 #include "prefs_common.h"
 
index ccfad6c..85dc3cc 100644 (file)
@@ -31,6 +31,7 @@
 #include "matcher_parser.h"
 #include "utils.h"
 #include "prefs_common.h"
+#include "timing.h"
 
 struct _AdvancedSearch {
        struct {
@@ -502,27 +503,33 @@ static gboolean search_impl(MsgInfoList **messages, AdvancedSearch* search,
                            FolderItem* folderItem, gboolean recursive)
 {
        if (recursive) {
-               if (!search_impl(messages, search, folderItem, FALSE))
+               START_TIMING("recursive");
+               if (!search_impl(messages, search, folderItem, FALSE)) {
+                       END_TIMING();
                        return FALSE;
-
+               }
                if (folderItem->node->children != NULL && !search->search_aborted) {
                        GNode *node;
                        for (node = folderItem->node->children; node != NULL; node = node->next) {
                                FolderItem *cur = FOLDER_ITEM(node->data);
                                debug_print("in: %s\n", cur->path);
-                               if (!search_impl(messages, search, cur, TRUE))
+                               if (!search_impl(messages, search, cur, TRUE)) {
+                                       END_TIMING();
                                        return FALSE;
+                               }
                        }
                }
+               END_TIMING();
        } else if (!folderItem->no_select) {
                MsgNumberList *msgnums = NULL;
                MsgNumberList *cur;
                MsgInfoList *msgs = NULL;
                gboolean can_search_on_server = folderItem->folder->klass->supports_server_search;
-
+               START_TIMING("folder");
                if (!search_filter_folder(&msgnums, search, folderItem,
                                          can_search_on_server)) {
                        g_slist_free(msgnums);
+                       END_TIMING();
                        return FALSE;
                }
 
@@ -542,6 +549,7 @@ static gboolean search_impl(MsgInfoList **messages, AdvancedSearch* search,
                }
 
                g_slist_free(msgnums);
+               END_TIMING();
        }
 
        return TRUE;
index 6146f65..300435c 100644 (file)
@@ -27,6 +27,7 @@
 
 #include "utils.h"
 #include "autofaces.h"
+#include "file-utils.h"
 
 static gint get_content_for_any_face(gchar *buf, gint len, gchar *anyname, gint maxlen)
 {
@@ -37,20 +38,20 @@ static gint get_content_for_any_face(gchar *buf, gint len, gchar *anyname, gint
        xfile = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, AUTOFACES_DIR,
                            G_DIR_SEPARATOR_S, anyname, NULL);
        buf[0] = '\0';
-       if ((xfp = g_fopen(xfile, "rb")) == NULL) {
+       if ((xfp = claws_fopen(xfile, "rb")) == NULL) {
                g_free(xfile);
                debug_print("header content file '%s' not found\n", anyname);
                return -1;
        }
-       if (fgets(buf, (len < maxlen)? len: maxlen, xfp) == NULL) {
-               fclose(xfp);
+       if (claws_fgets(buf, (len < maxlen)? len: maxlen, xfp) == NULL) {
+               claws_fclose(xfp);
                g_free(xfile);
                g_warning("header content file '%s' read failure", anyname);
                return -2;
        }
        lastc = strlen(buf) - 1;        /* remove trailing \n */
        buf[lastc] = (buf[lastc] == '\n')? '\0': buf[lastc];
-       fclose(xfp);
+       claws_fclose(xfp);
        g_free(xfile);
 
        return 0;
index aa307ae..805db4d 100644 (file)
@@ -25,6 +25,7 @@ endif
 
 libclawscommon_la_SOURCES = $(arch_sources) \
        codeconv.c \
+       file-utils.c \
        hooks.c \
        log.c \
        md5.c \
@@ -43,7 +44,6 @@ libclawscommon_la_SOURCES = $(arch_sources) \
        string_match.c \
        stringtable.c \
        claws.c \
-       claws_io.c \
        tags.c \
        template.c \
        utils.c \
@@ -56,6 +56,7 @@ libclawscommon_la_SOURCES = $(arch_sources) \
 clawscommonincludedir = $(pkgincludedir)/common
 clawscommoninclude_HEADERS = $(arch_headers) \
        codeconv.h \
+       file-utils.h \
        defs.h \
        hooks.h \
        log.h \
@@ -75,7 +76,6 @@ clawscommoninclude_HEADERS = $(arch_headers) \
        string_match.h \
        stringtable.h \
        claws.h \
-       claws_io.h \
        tags.h \
        template.h \
        timing.h \
diff --git a/src/common/claws_io.c b/src/common/claws_io.c
deleted file mode 100644 (file)
index 1de8bea..0000000
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2018 Colin Leroy 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 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * 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, see <http://www.gnu.org/licenses/>.
- */
-
-#ifdef HAVE_CONFIG_H
-#  include "config.h"
-#include "claws-features.h"
-#endif
-
-#include <stdio.h>
-#include <unistd.h>
-
-#include "timing.h"
-
-gboolean prefs_common_get_flush_metadata(void);
-
-int safe_fclose(FILE *fp)
-{
-       int r;
-       START_TIMING("");
-
-       if (fflush(fp) != 0) {
-               return EOF;
-       }
-       if (prefs_common_get_flush_metadata() && fsync(fileno(fp)) != 0) {
-               return EOF;
-       }
-
-       r = fclose(fp);
-       END_TIMING();
-
-       return r;
-}
diff --git a/src/common/claws_io.h b/src/common/claws_io.h
deleted file mode 100644 (file)
index e3e0e0c..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2018 Colin Leroy 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 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * 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, see <http://www.gnu.org/licenses/>.
- */
-
-#ifndef __CLAWS_IO_H__
-#define __CLAWS_IO_H__
-
-int safe_fclose(FILE *fp);
-
-#endif
diff --git a/src/common/file-utils.c b/src/common/file-utils.c
new file mode 100644 (file)
index 0000000..8f58fec
--- /dev/null
@@ -0,0 +1,879 @@
+/*
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2018 Colin Leroy 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#include "claws-features.h"
+#endif
+
+#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include "defs.h"
+#include "codeconv.h"
+#include "timing.h"
+#include "file-utils.h"
+
+gboolean prefs_common_get_flush_metadata(void);
+gboolean prefs_common_get_use_shred(void);
+
+/* FIXME make static once every file I/O is done using claws_* wrappers */
+int safe_fclose(FILE *fp)
+{
+       int r;
+       START_TIMING("");
+
+       if (fflush(fp) != 0) {
+               return EOF;
+       }
+       if (prefs_common_get_flush_metadata() && fsync(fileno(fp)) != 0) {
+               return EOF;
+       }
+
+       r = fclose(fp);
+       END_TIMING();
+
+       return r;
+}
+
+#if HAVE_FGETS_UNLOCKED
+
+/* Open a file and locks it once
+ * so subsequent I/O is faster
+ */
+FILE *claws_fopen(const char *file, const char *mode)
+{
+       FILE *fp = fopen(file, mode);
+       if (!fp)
+               return NULL;
+       flockfile(fp);
+       return fp;
+}
+
+FILE *claws_fdopen(int fd, const char *mode)
+{
+       FILE *fp = fdopen(fd, mode);
+       if (!fp)
+               return NULL;
+       flockfile(fp);
+       return fp;
+}
+
+/* Unlocks and close a file pointer
+ */
+
+int claws_fclose(FILE *fp)
+{
+       funlockfile(fp);
+       return fclose(fp);
+}
+
+/* Unlock, then safe-close a file pointer
+ * Safe close is done using fflush + fsync
+ * if the according preference says so.
+ */
+int claws_safe_fclose(FILE *fp)
+{
+       funlockfile(fp);
+       return safe_fclose(fp);
+}
+
+#ifdef G_OS_WIN32
+#define WEXITSTATUS(x) (x)
+#endif
+
+int claws_unlink(const char *filename) 
+{
+       GStatBuf s;
+       static int found_shred = -1;
+       static const gchar *args[4];
+
+       if (filename == NULL)
+               return 0;
+
+       if (prefs_common_get_use_shred()) {
+               if (found_shred == -1) {
+                       /* init */
+                       args[0] = g_find_program_in_path("shred");
+                       debug_print("found shred: %s\n", args[0]);
+                       found_shred = (args[0] != NULL) ? 1:0;
+                       args[1] = "-f";
+                       args[3] = NULL;
+               }
+               if (found_shred == 1) {
+                       if (g_stat(filename, &s) == 0 && S_ISREG(s.st_mode)) {
+                               if (s.st_nlink == 1) {
+                                       gint status=0;
+                                       args[2] = filename;
+                                       g_spawn_sync(NULL, (gchar **)args, NULL, 0,
+                                        NULL, NULL, NULL, NULL, &status, NULL);
+                                       debug_print("%s %s exited with status %d\n",
+                                               args[0], filename, WEXITSTATUS(status));
+                                       if (truncate(filename, 0) < 0)
+                                               g_warning("couln't truncate: %s", filename);
+                               }
+                       }
+               }
+       }
+       return g_unlink(filename);
+}
+
+gint file_strip_crs(const gchar *file)
+{
+       FILE *fp = NULL, *outfp = NULL;
+       gchar buf[4096];
+       gchar *out = get_tmp_file();
+       if (file == NULL)
+               goto freeout;
+
+       fp = claws_fopen(file, "rb");
+       if (!fp)
+               goto freeout;
+
+       outfp = claws_fopen(out, "wb");
+       if (!outfp) {
+               claws_fclose(fp);
+               goto freeout;
+       }
+
+       while (claws_fgets(buf, sizeof (buf), fp) != NULL) {
+               strcrchomp(buf);
+               if (claws_fputs(buf, outfp) == EOF) {
+                       claws_fclose(fp);
+                       claws_fclose(outfp);
+                       goto unlinkout;
+               }
+       }
+
+       claws_fclose(fp);
+       if (claws_safe_fclose(outfp) == EOF) {
+               goto unlinkout;
+       }
+       
+       if (move_file(out, file, TRUE) < 0)
+               goto unlinkout;
+       
+       g_free(out);
+       return 0;
+unlinkout:
+       claws_unlink(out);
+freeout:
+       g_free(out);
+       return -1;
+}
+
+/*
+ * Append src file body to the tail of dest file.
+ * Now keep_backup has no effects.
+ */
+gint append_file(const gchar *src, const gchar *dest, gboolean keep_backup)
+{
+       FILE *src_fp, *dest_fp;
+       gint n_read;
+       gchar buf[BUFSIZ];
+
+       gboolean err = FALSE;
+
+       if ((src_fp = claws_fopen(src, "rb")) == NULL) {
+               FILE_OP_ERROR(src, "claws_fopen");
+               return -1;
+       }
+
+       if ((dest_fp = claws_fopen(dest, "ab")) == NULL) {
+               FILE_OP_ERROR(dest, "claws_fopen");
+               claws_fclose(src_fp);
+               return -1;
+       }
+
+       if (change_file_mode_rw(dest_fp, dest) < 0) {
+               FILE_OP_ERROR(dest, "chmod");
+               g_warning("can't change file mode: %s", dest);
+       }
+
+       while ((n_read = claws_fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
+               if (n_read < sizeof(buf) && claws_ferror(src_fp))
+                       break;
+               if (claws_fwrite(buf, 1, n_read, dest_fp) < n_read) {
+                       g_warning("writing to %s failed.", dest);
+                       claws_fclose(dest_fp);
+                       claws_fclose(src_fp);
+                       claws_unlink(dest);
+                       return -1;
+               }
+       }
+
+       if (claws_ferror(src_fp)) {
+               FILE_OP_ERROR(src, "claws_fread");
+               err = TRUE;
+       }
+       claws_fclose(src_fp);
+       if (claws_fclose(dest_fp) == EOF) {
+               FILE_OP_ERROR(dest, "claws_fclose");
+               err = TRUE;
+       }
+
+       if (err) {
+               claws_unlink(dest);
+               return -1;
+       }
+
+       return 0;
+}
+
+gint copy_file(const gchar *src, const gchar *dest, gboolean keep_backup)
+{
+       FILE *src_fp, *dest_fp;
+       gint n_read;
+       gchar buf[BUFSIZ];
+       gchar *dest_bak = NULL;
+       gboolean err = FALSE;
+
+       if ((src_fp = claws_fopen(src, "rb")) == NULL) {
+               FILE_OP_ERROR(src, "claws_fopen");
+               return -1;
+       }
+       if (is_file_exist(dest)) {
+               dest_bak = g_strconcat(dest, ".bak", NULL);
+               if (rename_force(dest, dest_bak) < 0) {
+                       FILE_OP_ERROR(dest, "rename");
+                       claws_fclose(src_fp);
+                       g_free(dest_bak);
+                       return -1;
+               }
+       }
+
+       if ((dest_fp = claws_fopen(dest, "wb")) == NULL) {
+               FILE_OP_ERROR(dest, "claws_fopen");
+               claws_fclose(src_fp);
+               if (dest_bak) {
+                       if (rename_force(dest_bak, dest) < 0)
+                               FILE_OP_ERROR(dest_bak, "rename");
+                       g_free(dest_bak);
+               }
+               return -1;
+       }
+
+       if (change_file_mode_rw(dest_fp, dest) < 0) {
+               FILE_OP_ERROR(dest, "chmod");
+               g_warning("can't change file mode: %s", dest);
+       }
+
+       while ((n_read = claws_fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
+               if (n_read < sizeof(buf) && claws_ferror(src_fp))
+                       break;
+               if (claws_fwrite(buf, 1, n_read, dest_fp) < n_read) {
+                       g_warning("writing to %s failed.", dest);
+                       claws_fclose(dest_fp);
+                       claws_fclose(src_fp);
+                       claws_unlink(dest);
+                       if (dest_bak) {
+                               if (rename_force(dest_bak, dest) < 0)
+                                       FILE_OP_ERROR(dest_bak, "rename");
+                               g_free(dest_bak);
+                       }
+                       return -1;
+               }
+       }
+
+       if (claws_ferror(src_fp)) {
+               FILE_OP_ERROR(src, "claws_fread");
+               err = TRUE;
+       }
+       claws_fclose(src_fp);
+       if (claws_safe_fclose(dest_fp) == EOF) {
+               FILE_OP_ERROR(dest, "claws_fclose");
+               err = TRUE;
+       }
+
+       if (err) {
+               claws_unlink(dest);
+               if (dest_bak) {
+                       if (rename_force(dest_bak, dest) < 0)
+                               FILE_OP_ERROR(dest_bak, "rename");
+                       g_free(dest_bak);
+               }
+               return -1;
+       }
+
+       if (keep_backup == FALSE && dest_bak)
+               claws_unlink(dest_bak);
+
+       g_free(dest_bak);
+
+       return 0;
+}
+
+gint move_file(const gchar *src, const gchar *dest, gboolean overwrite)
+{
+       if (overwrite == FALSE && is_file_exist(dest)) {
+               g_warning("move_file(): file %s already exists.", dest);
+               return -1;
+       }
+
+       if (rename_force(src, dest) == 0) return 0;
+
+       if (EXDEV != errno) {
+               FILE_OP_ERROR(src, "rename");
+               return -1;
+       }
+
+       if (copy_file(src, dest, FALSE) < 0) return -1;
+
+       claws_unlink(src);
+
+       return 0;
+}
+
+gint copy_file_part_to_fp(FILE *fp, off_t offset, size_t length, FILE *dest_fp)
+{
+       gint n_read;
+       gint bytes_left, to_read;
+       gchar buf[BUFSIZ];
+
+       if (fseek(fp, offset, SEEK_SET) < 0) {
+               perror("fseek");
+               return -1;
+       }
+
+       bytes_left = length;
+       to_read = MIN(bytes_left, sizeof(buf));
+
+       while ((n_read = claws_fread(buf, sizeof(gchar), to_read, fp)) > 0) {
+               if (n_read < to_read && claws_ferror(fp))
+                       break;
+               if (claws_fwrite(buf, 1, n_read, dest_fp) < n_read) {
+                       return -1;
+               }
+               bytes_left -= n_read;
+               if (bytes_left == 0)
+                       break;
+               to_read = MIN(bytes_left, sizeof(buf));
+       }
+
+       if (claws_ferror(fp)) {
+               perror("claws_fread");
+               return -1;
+       }
+
+       return 0;
+}
+
+gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
+{
+       FILE *dest_fp;
+       gboolean err = FALSE;
+
+       if ((dest_fp = claws_fopen(dest, "wb")) == NULL) {
+               FILE_OP_ERROR(dest, "claws_fopen");
+               return -1;
+       }
+
+       if (change_file_mode_rw(dest_fp, dest) < 0) {
+               FILE_OP_ERROR(dest, "chmod");
+               g_warning("can't change file mode: %s", dest);
+       }
+
+       if (copy_file_part_to_fp(fp, offset, length, dest_fp) < 0)
+               err = TRUE;
+
+       if (claws_safe_fclose(dest_fp) == EOF) {
+               FILE_OP_ERROR(dest, "claws_fclose");
+               err = TRUE;
+       }
+
+       if (err) {
+               g_warning("writing to %s failed.", dest);
+               claws_unlink(dest);
+               return -1;
+       }
+
+       return 0;
+}
+
+gint canonicalize_file(const gchar *src, const gchar *dest)
+{
+       FILE *src_fp, *dest_fp;
+       gchar buf[BUFFSIZE];
+       gint len;
+       gboolean err = FALSE;
+       gboolean last_linebreak = FALSE;
+
+       if (src == NULL || dest == NULL)
+               return -1;
+
+       if ((src_fp = claws_fopen(src, "rb")) == NULL) {
+               FILE_OP_ERROR(src, "claws_fopen");
+               return -1;
+       }
+
+       if ((dest_fp = claws_fopen(dest, "wb")) == NULL) {
+               FILE_OP_ERROR(dest, "claws_fopen");
+               claws_fclose(src_fp);
+               return -1;
+       }
+
+       if (change_file_mode_rw(dest_fp, dest) < 0) {
+               FILE_OP_ERROR(dest, "chmod");
+               g_warning("can't change file mode: %s", dest);
+       }
+
+       while (claws_fgets(buf, sizeof(buf), src_fp) != NULL) {
+               gint r = 0;
+
+               len = strlen(buf);
+               if (len == 0) break;
+               last_linebreak = FALSE;
+
+               if (buf[len - 1] != '\n') {
+                       last_linebreak = TRUE;
+                       r = claws_fputs(buf, dest_fp);
+               } else if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
+                       r = claws_fputs(buf, dest_fp);
+               } else {
+                       if (len > 1) {
+                               r = claws_fwrite(buf, 1, len - 1, dest_fp);
+                               if (r != (len -1))
+                                       r = EOF;
+                       }
+                       if (r != EOF)
+                               r = claws_fputs("\r\n", dest_fp);
+               }
+
+               if (r == EOF) {
+                       g_warning("writing to %s failed.", dest);
+                       claws_fclose(dest_fp);
+                       claws_fclose(src_fp);
+                       claws_unlink(dest);
+                       return -1;
+               }
+       }
+
+       if (last_linebreak == TRUE) {
+               if (claws_fputs("\r\n", dest_fp) == EOF)
+                       err = TRUE;
+       }
+
+       if (claws_ferror(src_fp)) {
+               FILE_OP_ERROR(src, "claws_fgets");
+               err = TRUE;
+       }
+       claws_fclose(src_fp);
+       if (claws_safe_fclose(dest_fp) == EOF) {
+               FILE_OP_ERROR(dest, "claws_fclose");
+               err = TRUE;
+       }
+
+       if (err) {
+               claws_unlink(dest);
+               return -1;
+       }
+
+       return 0;
+}
+
+gint canonicalize_file_replace(const gchar *file)
+{
+       gchar *tmp_file;
+
+       tmp_file = get_tmp_file();
+
+       if (canonicalize_file(file, tmp_file) < 0) {
+               g_free(tmp_file);
+               return -1;
+       }
+
+       if (move_file(tmp_file, file, TRUE) < 0) {
+               g_warning("can't replace file: %s", file);
+               claws_unlink(tmp_file);
+               g_free(tmp_file);
+               return -1;
+       }
+
+       g_free(tmp_file);
+       return 0;
+}
+
+
+gint str_write_to_file(const gchar *str, const gchar *file, gboolean safe)
+{
+       FILE *fp;
+       size_t len;
+       int r;
+
+       cm_return_val_if_fail(str != NULL, -1);
+       cm_return_val_if_fail(file != NULL, -1);
+
+       if ((fp = claws_fopen(file, "wb")) == NULL) {
+               FILE_OP_ERROR(file, "claws_fopen");
+               return -1;
+       }
+
+       len = strlen(str);
+       if (len == 0) {
+               claws_fclose(fp);
+               return 0;
+       }
+
+       if (claws_fwrite(str, 1, len, fp) != len) {
+               FILE_OP_ERROR(file, "claws_fwrite");
+               claws_fclose(fp);
+               claws_unlink(file);
+               return -1;
+       }
+
+       if (safe) {
+               r = claws_safe_fclose(fp);
+       } else {
+               r = claws_fclose(fp);
+       }
+
+       if (r == EOF) {
+               FILE_OP_ERROR(file, "claws_fclose");
+               claws_unlink(file);
+               return -1;
+       }
+
+       return 0;
+}
+
+static gchar *file_read_stream_to_str_full(FILE *fp, gboolean recode)
+{
+       GByteArray *array;
+       guchar buf[BUFSIZ];
+       gint n_read;
+       gchar *str;
+
+       cm_return_val_if_fail(fp != NULL, NULL);
+
+       array = g_byte_array_new();
+
+       while ((n_read = claws_fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
+               if (n_read < sizeof(buf) && claws_ferror(fp))
+                       break;
+               g_byte_array_append(array, buf, n_read);
+       }
+
+       if (claws_ferror(fp)) {
+               FILE_OP_ERROR("file stream", "claws_fread");
+               g_byte_array_free(array, TRUE);
+               return NULL;
+       }
+
+       buf[0] = '\0';
+       g_byte_array_append(array, buf, 1);
+       str = (gchar *)array->data;
+       g_byte_array_free(array, FALSE);
+
+       if (recode && !g_utf8_validate(str, -1, NULL)) {
+               const gchar *src_codeset, *dest_codeset;
+               gchar *tmp = NULL;
+               src_codeset = conv_get_locale_charset_str();
+               dest_codeset = CS_UTF_8;
+               tmp = conv_codeset_strdup(str, src_codeset, dest_codeset);
+               g_free(str);
+               str = tmp;
+       }
+
+       return str;
+}
+
+static gchar *file_read_to_str_full(const gchar *file, gboolean recode)
+{
+       FILE *fp;
+       gchar *str;
+       GStatBuf s;
+#ifndef G_OS_WIN32
+       gint fd, err;
+       struct timeval timeout = {1, 0};
+       fd_set fds;
+       int fflags = 0;
+#endif
+
+       cm_return_val_if_fail(file != NULL, NULL);
+
+       if (g_stat(file, &s) != 0) {
+               FILE_OP_ERROR(file, "stat");
+               return NULL;
+       }
+       if (S_ISDIR(s.st_mode)) {
+               g_warning("%s: is a directory", file);
+               return NULL;
+       }
+
+#ifdef G_OS_WIN32
+       fp = claws_fopen (file, "rb");
+       if (fp == NULL) {
+               FILE_OP_ERROR(file, "open");
+               return NULL;
+       }
+#else    
+       /* test whether the file is readable without blocking */
+       fd = g_open(file, O_RDONLY | O_NONBLOCK, 0);
+       if (fd == -1) {
+               FILE_OP_ERROR(file, "open");
+               return NULL;
+       }
+
+       FD_ZERO(&fds);
+       FD_SET(fd, &fds);
+
+       /* allow for one second */
+       err = select(fd+1, &fds, NULL, NULL, &timeout);
+       if (err <= 0 || !FD_ISSET(fd, &fds)) {
+               if (err < 0) {
+                       FILE_OP_ERROR(file, "select");
+               } else {
+                       g_warning("%s: doesn't seem readable", file);
+               }
+               close(fd);
+               return NULL;
+       }
+       
+       /* Now clear O_NONBLOCK */
+       if ((fflags = fcntl(fd, F_GETFL)) < 0) {
+               FILE_OP_ERROR(file, "fcntl (F_GETFL)");
+               close(fd);
+               return NULL;
+       }
+       if (fcntl(fd, F_SETFL, (fflags & ~O_NONBLOCK)) < 0) {
+               FILE_OP_ERROR(file, "fcntl (F_SETFL)");
+               close(fd);
+               return NULL;
+       }
+       
+       /* get the FILE pointer */
+       fp = claws_fdopen(fd, "rb");
+
+       if (fp == NULL) {
+               FILE_OP_ERROR(file, "claws_fdopen");
+               close(fd); /* if fp isn't NULL, we'll use claws_fclose instead! */
+               return NULL;
+       }
+#endif
+
+       str = file_read_stream_to_str_full(fp, recode);
+
+       claws_fclose(fp);
+
+       return str;
+}
+
+gchar *file_read_to_str(const gchar *file)
+{
+       return file_read_to_str_full(file, TRUE);
+}
+gchar *file_read_stream_to_str(FILE *fp)
+{
+       return file_read_stream_to_str_full(fp, TRUE);
+}
+
+gchar *file_read_to_str_no_recode(const gchar *file)
+{
+       return file_read_to_str_full(file, FALSE);
+}
+gchar *file_read_stream_to_str_no_recode(FILE *fp)
+{
+       return file_read_stream_to_str_full(fp, FALSE);
+}
+
+gint rename_force(const gchar *oldpath, const gchar *newpath)
+{
+#ifndef G_OS_UNIX
+       if (!is_file_entry_exist(oldpath)) {
+               errno = ENOENT;
+               return -1;
+       }
+       if (is_file_exist(newpath)) {
+               if (claws_unlink(newpath) < 0)
+                       FILE_OP_ERROR(newpath, "unlink");
+       }
+#endif
+       return g_rename(oldpath, newpath);
+}
+
+gint copy_dir(const gchar *src, const gchar *dst)
+{
+       GDir *dir;
+       const gchar *name;
+
+       if ((dir = g_dir_open(src, 0, NULL)) == NULL) {
+               g_warning("failed to open directory: %s", src);
+               return -1;
+       }
+
+       if (make_dir(dst) < 0)
+               return -1;
+
+       while ((name = g_dir_read_name(dir)) != NULL) {
+               gchar *old_file, *new_file;
+               old_file = g_strconcat(src, G_DIR_SEPARATOR_S, name, NULL);
+               new_file = g_strconcat(dst, G_DIR_SEPARATOR_S, name, NULL);
+               debug_print("copying: %s -> %s\n", old_file, new_file);
+               if (g_file_test(old_file, G_FILE_TEST_IS_REGULAR)) {
+                       gint r = copy_file(old_file, new_file, TRUE);
+                       if (r < 0) {
+                               g_dir_close(dir);
+                               return r;
+                       }
+                }
+#ifndef G_OS_WIN32
+                /* Windows has no symlinks.  Or well, Vista seems to
+                   have something like this but the semantics might be
+                   different.  Thus we don't use it under Windows. */
+                else if (g_file_test(old_file, G_FILE_TEST_IS_SYMLINK)) {
+                       GError *error = NULL;
+                       gint r = 0;
+                       gchar *target = g_file_read_link(old_file, &error);
+                       if (target)
+                               r = symlink(target, new_file);
+                       g_free(target);
+                       if (r < 0) {
+                               g_dir_close(dir);
+                               return r;
+                       }
+                 }
+#endif /*G_OS_WIN32*/
+               else if (g_file_test(old_file, G_FILE_TEST_IS_DIR)) {
+                       gint r = copy_dir(old_file, new_file);
+                       if (r < 0) {
+                               g_dir_close(dir);
+                               return r;
+                       }
+               }
+       }
+       g_dir_close(dir);
+       return 0;
+}
+
+gint change_file_mode_rw(FILE *fp, const gchar *file)
+{
+#if HAVE_FCHMOD
+       return fchmod(fileno(fp), S_IRUSR|S_IWUSR);
+#else
+       return g_chmod(file, S_IRUSR|S_IWUSR);
+#endif
+}
+
+FILE *my_tmpfile(void)
+{
+       const gchar suffix[] = ".XXXXXX";
+       const gchar *tmpdir;
+       guint tmplen;
+       const gchar *progname;
+       guint proglen;
+       gchar *fname;
+       gint fd;
+       FILE *fp;
+#ifndef G_OS_WIN32
+       gchar buf[2]="\0";
+#endif
+
+       tmpdir = get_tmp_dir();
+       tmplen = strlen(tmpdir);
+       progname = g_get_prgname();
+       if (progname == NULL)
+               progname = "claws-mail";
+       proglen = strlen(progname);
+       Xalloca(fname, tmplen + 1 + proglen + sizeof(suffix),
+               return tmpfile());
+
+       memcpy(fname, tmpdir, tmplen);
+       fname[tmplen] = G_DIR_SEPARATOR;
+       memcpy(fname + tmplen + 1, progname, proglen);
+       memcpy(fname + tmplen + 1 + proglen, suffix, sizeof(suffix));
+
+       fd = g_mkstemp(fname);
+       if (fd < 0)
+               return tmpfile();
+
+#ifndef G_OS_WIN32
+       claws_unlink(fname);
+       
+       /* verify that we can write in the file after unlinking */
+       if (write(fd, buf, 1) < 0) {
+               close(fd);
+               return tmpfile();
+       }
+       
+#endif
+
+       fp = claws_fdopen(fd, "w+b");
+       if (!fp)
+               close(fd);
+       else {
+               rewind(fp);
+               return fp;
+       }
+
+       return tmpfile();
+}
+
+FILE *my_tmpfile_with_len(size_t len)
+{
+#if HAVE_FMEMOPEN
+       FILE *tmpfp = fmemopen(NULL, len, "w+b");
+
+       if (tmpfp == NULL) {
+               return my_tmpfile();
+       }
+
+       return tmpfp;
+#else
+       return my_tmpfile();
+#endif
+}
+
+FILE *get_tmpfile_in_dir(const gchar *dir, gchar **filename)
+{
+       int fd;
+       *filename = g_strdup_printf("%s%cclaws.XXXXXX", dir, G_DIR_SEPARATOR);
+       fd = g_mkstemp(*filename);
+       if (fd < 0)
+               return NULL;
+       return claws_fdopen(fd, "w+");
+}
+
+FILE *str_open_as_stream(const gchar *str)
+{
+       FILE *fp;
+       size_t len;
+
+       cm_return_val_if_fail(str != NULL, NULL);
+
+       fp = my_tmpfile();
+       if (!fp) {
+               FILE_OP_ERROR("str_open_as_stream", "my_tmpfile");
+               return NULL;
+       }
+
+       len = strlen(str);
+       if (len == 0) return fp;
+
+       if (claws_fwrite(str, 1, len, fp) != len) {
+               FILE_OP_ERROR("str_open_as_stream", "claws_fwrite");
+               claws_fclose(fp);
+               return NULL;
+       }
+
+       rewind(fp);
+       return fp;
+}
+
+#endif
diff --git a/src/common/file-utils.h b/src/common/file-utils.h
new file mode 100644 (file)
index 0000000..10926f0
--- /dev/null
@@ -0,0 +1,107 @@
+/*
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2018 Colin Leroy 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __CLAWS_IO_H__
+#define __CLAWS_IO_H__
+
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#include "claws-features.h"
+#endif
+
+#include <stdio.h>
+#include <glib.h>
+
+#if HAVE_FGETS_UNLOCKED
+#define claws_fgets fgets_unlocked
+#define claws_fgetc fgetc_unlocked
+#define claws_fputs fputs_unlocked
+#define claws_fputc fputc_unlocked
+#define claws_fread fread_unlocked
+#define claws_fwrite fwrite_unlocked
+#define claws_feof feof_unlocked
+#define claws_ferror ferror_unlocked
+
+FILE *claws_fopen              (const char *file,
+                                const char *mode);
+FILE *claws_fdopen             (int fd,
+                                const char *mode);
+int claws_fclose               (FILE *fp);
+int claws_safe_fclose          (FILE *fp);
+
+#else
+
+#define claws_fgets fgets
+#define claws_fgetc fgetc
+#define claws_fputs fputs
+#define claws_fputc fputc
+#define claws_fread fread
+#define claws_fwrite fwrite
+#define claws_feof feof
+#define claws_ferror ferror
+#define claws_fopen g_fopen
+#define claws_fdopen fdopen
+#define claws_fclose fclose
+#define claws_safe_fclose safe_fclose
+#endif
+
+int safe_fclose                        (FILE *fp);
+int claws_unlink               (const char     *filename);
+
+gint file_strip_crs            (const gchar    *file);
+gint append_file               (const gchar    *src,
+                                const gchar    *dest,
+                                gboolean        keep_backup);
+gint copy_file                 (const gchar    *src,
+                                const gchar    *dest,
+                                gboolean        keep_backup);
+gint move_file                 (const gchar    *src,
+                                const gchar    *dest,
+                                gboolean        overwrite);
+gint copy_file_part_to_fp      (FILE           *fp,
+                                off_t           offset,
+                                size_t          length,
+                                FILE           *dest_fp);
+gint copy_file_part            (FILE           *fp,
+                                off_t           offset,
+                                size_t          length,
+                                const gchar    *dest);
+gint canonicalize_file         (const gchar    *src,
+                                const gchar    *dest);
+gint canonicalize_file_replace (const gchar    *file);
+gchar *file_read_to_str                (const gchar    *file);
+gchar *file_read_to_str_no_recode(const gchar  *file);
+gchar *file_read_stream_to_str (FILE           *fp);
+gchar *file_read_stream_to_str_no_recode(FILE  *fp);
+
+gint rename_force              (const gchar    *oldpath,
+                                const gchar    *newpath);
+gint copy_dir                  (const gchar    *src,
+                                const gchar    *dest);
+gint change_file_mode_rw       (FILE           *fp,
+                                const gchar    *file);
+FILE *my_tmpfile               (void);
+FILE *my_tmpfile_with_len      (size_t          len);
+FILE *get_tmpfile_in_dir       (const gchar    *dir,
+                                gchar         **filename);
+FILE *str_open_as_stream       (const gchar    *str);
+gint str_write_to_file         (const gchar    *str,
+                                const gchar    *file,
+                                gboolean        safe);
+
+#endif
index 65f284e..83b023e 100644 (file)
 #  include <w32lib.h>
 #endif
 
-#include <stdio.h>
 #include <glib.h>
 #include <glib/gi18n.h>
 
 #include "utils.h"
 #include "log.h"
 #include "hooks.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
-#define FWRITE(_b,_s,_n,_f)    if (fwrite(_b,_s,_n,_f) != _n) { \
-                                       g_message("log fwrite failed!\n"); \
+#define FWRITE(_b,_s,_n,_f)    if (claws_fwrite(_b,_s,_n,_f) != _n) { \
+                                       g_message("log claws_fwrite failed!\n"); \
                                        return; \
                                }
-#define FPUTS(_b,_f)           if (fputs(_b,_f) == EOF) { \
-                                       g_message("log fputs failed!\n"); \
+#define FPUTS(_b,_f)           if (claws_fputs(_b,_f) == EOF) { \
+                                       g_message("log claws_fputs failed!\n"); \
                                        return; \
                                }
 #define FFLUSH(_f)             if (fflush(_f) != 0) { \
@@ -122,9 +121,9 @@ void set_log_file(LogInstance instance, const gchar *filename)
                g_free(backupname);
        }
 
-       log_fp[instance] = g_fopen(fullname, "wb");
+       log_fp[instance] = claws_fopen(fullname, "wb");
        if (!log_fp[instance]) {
-               FILE_OP_ERROR(fullname, "fopen");
+               FILE_OP_ERROR(fullname, "claws_fopen");
                log_filename[instance] = NULL;
                g_free(fullname);
                return;
@@ -137,7 +136,7 @@ void set_log_file(LogInstance instance, const gchar *filename)
 void close_log_file(LogInstance instance)
 {
        if (log_fp[instance]) {
-               safe_fclose(log_fp[instance]);
+               claws_safe_fclose(log_fp[instance]);
                log_fp[instance] = NULL;
                log_size[instance] = 0;
                g_free(log_filename[instance]);
index 64a6f01..39679e8 100644 (file)
@@ -41,6 +41,7 @@
 #include "prefs.h"
 #include "claws.h"
 #include "timing.h"
+#include "file-utils.h"
 
 #ifdef G_OS_WIN32
 #define PLUGINS_BLOCK_PREFIX "PluginsWin32_"
@@ -245,13 +246,13 @@ static gint plugin_load_deps(const gchar *filename, gchar **error)
        deps_file = g_strconcat(tmp, ".deps", NULL);
        g_free(tmp);
        
-       fp = g_fopen(deps_file, "rb");
+       fp = claws_fopen(deps_file, "rb");
        g_free(deps_file);
        
        if (!fp)
                return 0;
        
-       while (fgets(buf, sizeof(buf), fp) != NULL) {
+       while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
                Plugin *dep_plugin = NULL;
                gchar *path = NULL;
                buf[strlen(buf)-1]='\0'; /* chop off \n */
@@ -262,7 +263,7 @@ static gint plugin_load_deps(const gchar *filename, gchar **error)
                        dep_plugin = plugin_load(path, error);
                        if (dep_plugin == NULL) {
                                g_free(path);
-                               fclose(fp);
+                               claws_fclose(fp);
                                return -1;
                        }
                        dep_plugin->in_prefix_dir = TRUE;
@@ -278,7 +279,7 @@ static gint plugin_load_deps(const gchar *filename, gchar **error)
                                        g_strdup(filename));
                }
        }
-       fclose(fp);
+       claws_fclose(fp);
        return 0;
 }
 
@@ -605,7 +606,7 @@ void plugin_load_all(const gchar *type)
        }
        g_free(block);
 
-       while (fgets(buf, sizeof(buf), pfile->fp) != NULL) {
+       while (claws_fgets(buf, sizeof(buf), pfile->fp) != NULL) {
                if (buf[0] == '[')
                        break;
 
index 77b4213..7489507 100644 (file)
 #include "claws-features.h"
 #endif
 
+#include <stdio.h>
+
 #include "defs.h"
 
 #include <glib.h>
 
 #include "prefs.h"
 #include "utils.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 static gboolean prefs_is_readonly      (const gchar    *path);
 
@@ -46,8 +48,8 @@ PrefFile *prefs_read_open(const gchar *path)
 
        cm_return_val_if_fail(path != NULL, NULL);
 
-       if ((fp = g_fopen(path, "rb")) == NULL) {
-               FILE_OP_ERROR(path, "fopen");
+       if ((fp = claws_fopen(path, "rb")) == NULL) {
+               FILE_OP_ERROR(path, "claws_fopen");
                return NULL;
        }
 
@@ -83,8 +85,8 @@ PrefFile *prefs_write_open(const gchar *path)
        }
 
        tmppath = g_strconcat(path, ".tmp", NULL);
-       if ((fp = g_fopen(tmppath, "wb")) == NULL) {
-               FILE_OP_ERROR(tmppath, "fopen");
+       if ((fp = claws_fopen(tmppath, "wb")) == NULL) {
+               FILE_OP_ERROR(tmppath, "claws_fopen");
                g_free(tmppath);
                return NULL;
        }
@@ -128,17 +130,17 @@ gint prefs_file_close(PrefFile *pfile)
        path = pfile->path;
 
        if (!pfile->writing) {
-               fclose(fp);
+               claws_fclose(fp);
                g_free(pfile);
                g_free(path);
                return 0;
        }
 
        if (orig_fp) {
-               while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
+               while (claws_fgets(buf, sizeof(buf), orig_fp) != NULL) {
                        /* next block */
                        if (buf[0] == '[') {
-                               if (fputs(buf, fp)  == EOF) {
+                               if (claws_fputs(buf, fp)  == EOF) {
                                        g_warning("failed to write configuration to file");
                                        prefs_file_close_revert(pfile);
                                
@@ -148,20 +150,20 @@ gint prefs_file_close(PrefFile *pfile)
                        }
                }
                
-               while (fgets(buf, sizeof(buf), orig_fp) != NULL)
-                       if (fputs(buf, fp) == EOF) {
+               while (claws_fgets(buf, sizeof(buf), orig_fp) != NULL)
+                       if (claws_fputs(buf, fp) == EOF) {
                                g_warning("failed to write configuration to file");
                                prefs_file_close_revert(pfile);                 
                                
                                return -1;
                        }
-               fclose(orig_fp);
+               claws_fclose(orig_fp);
        }
 
        tmppath = g_strconcat(path, ".tmp", NULL);
 
-       if (safe_fclose(fp) == EOF) {
-               FILE_OP_ERROR(tmppath, "fclose");
+       if (claws_safe_fclose(fp) == EOF) {
+               FILE_OP_ERROR(tmppath, "claws_fclose");
                claws_unlink(tmppath);
                g_free(path);
                g_free(tmppath);
@@ -214,10 +216,10 @@ gint prefs_file_close_revert(PrefFile *pfile)
        cm_return_val_if_fail(pfile != NULL, -1);
 
        if (pfile->orig_fp)
-               fclose(pfile->orig_fp);
+               claws_fclose(pfile->orig_fp);
        if (pfile->writing)
                tmppath = g_strconcat(pfile->path, ".tmp", NULL);
-       fclose(pfile->fp);
+       claws_fclose(pfile->fp);
        if (pfile->writing) {
                if (claws_unlink(tmppath) < 0) FILE_OP_ERROR(tmppath, "unlink");
                g_free(tmppath);
@@ -272,7 +274,7 @@ gint prefs_set_block_label(PrefFile *pfile, const gchar *label)
 
        block_label = g_strdup_printf("[%s]", label);
        if (!pfile->writing) {
-               while (fgets(buf, sizeof(buf), pfile->fp) != NULL) {
+               while (claws_fgets(buf, sizeof(buf), pfile->fp) != NULL) {
                        gint val;
                        
                        val = strncmp(buf, block_label, strlen(block_label));
@@ -282,10 +284,10 @@ gint prefs_set_block_label(PrefFile *pfile, const gchar *label)
                        }
                }
        } else {
-               if ((pfile->orig_fp = g_fopen(pfile->path, "rb")) != NULL) {
+               if ((pfile->orig_fp = claws_fopen(pfile->path, "rb")) != NULL) {
                        gboolean block_matched = FALSE;
 
-                       while (fgets(buf, sizeof(buf), pfile->orig_fp) != NULL) {
+                       while (claws_fgets(buf, sizeof(buf), pfile->orig_fp) != NULL) {
                                gint val;
 
                                val = strncmp(buf, block_label, strlen(block_label));
@@ -294,7 +296,7 @@ gint prefs_set_block_label(PrefFile *pfile, const gchar *label)
                                        block_matched = TRUE;
                                        break;
                                } else {
-                                       if (fputs(buf, pfile->fp) == EOF) {
+                                       if (claws_fputs(buf, pfile->fp) == EOF) {
                                                g_warning("failed to write configuration to file");
                                                prefs_file_close_revert(pfile);
                                                g_free(block_label);
@@ -305,13 +307,13 @@ gint prefs_set_block_label(PrefFile *pfile, const gchar *label)
                        }
 
                        if (!block_matched) {
-                               fclose(pfile->orig_fp);
+                               claws_fclose(pfile->orig_fp);
                                pfile->orig_fp = NULL;
                        }
                }
 
-               if (fputs(block_label, pfile->fp) == EOF ||
-                       fputc('\n', pfile->fp) == EOF) {
+               if (claws_fputs(block_label, pfile->fp) == EOF ||
+                       claws_fputc('\n', pfile->fp) == EOF) {
                        g_warning("failed to write configuration to file");
                        prefs_file_close_revert(pfile);
                        g_free(block_label);
index d098ac2..6db05f5 100644 (file)
@@ -46,7 +46,7 @@
 #include "socket.h"
 #include "hooks.h"
 #include "defs.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 static GHashTable *warned_expired = NULL;
 
@@ -173,7 +173,7 @@ static void gnutls_export_X509_fp(FILE *fp, gnutls_x509_crt_t x509_cert, gnutls_
        }
 
        debug_print("writing %zd bytes\n",cert_size);
-       if (fwrite(&output, 1, cert_size, fp) < cert_size) {
+       if (claws_fwrite(&output, 1, cert_size, fp) < cert_size) {
                g_warning("failed to write cert: %d %s", errno, g_strerror(errno));
        }
 }
@@ -241,8 +241,8 @@ static int gnutls_import_X509_list_fp(FILE *fp, gnutls_x509_crt_fmt_t format,
        tmp.data = malloc(s.st_size);
        memset(tmp.data, 0, s.st_size);
        tmp.size = s.st_size;
-       if (fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
-               perror("fread");
+       if (claws_fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
+               perror("claws_fread");
                free(tmp.data);
                free(crt_list);
                return -EIO;
@@ -300,8 +300,8 @@ static gnutls_x509_privkey_t gnutls_import_key_fp(FILE *fp, gnutls_x509_crt_fmt_
        tmp.data = malloc(s.st_size);
        memset(tmp.data, 0, s.st_size);
        tmp.size = s.st_size;
-       if (fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
-               perror("fread");
+       if (claws_fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
+               perror("claws_fread");
                free(tmp.data);
                return NULL;
        }
@@ -331,7 +331,7 @@ static gnutls_pkcs12_t gnutls_import_PKCS12_fp(FILE *fp, gnutls_x509_crt_fmt_t f
        tmp.data = malloc(s.st_size);
        memset(tmp.data, 0, s.st_size);
        tmp.size = s.st_size;
-       if (fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
+       if (claws_fread (tmp.data, 1, s.st_size, fp) < s.st_size) {
                log_error(LOG_PROTOCOL, _("Cannot read P12 certificate file (%s)\n"),
                                  g_strerror(errno));
                free(tmp.data);
@@ -367,7 +367,7 @@ static void ssl_certificate_save (SSLCertificate *cert)
        file = get_certificate_path(cert->host, port, cert->fingerprint);
 
        g_free(port);
-       fp = g_fopen(file, "wb");
+       fp = claws_fopen(file, "wb");
        if (fp == NULL) {
                g_free(file);
                debug_print("Can't save certificate !\n");
@@ -377,7 +377,7 @@ static void ssl_certificate_save (SSLCertificate *cert)
        gnutls_export_X509_fp(fp, cert->x509_cert, GNUTLS_X509_FMT_DER);
 
        g_free(file);
-       safe_fclose(fp);
+       claws_safe_fclose(fp);
 
 }
 
@@ -421,14 +421,14 @@ SSLCertificate *ssl_certificate_find (const gchar *host, gushort port, const gch
        
        if (fingerprint != NULL) {
                file = get_certificate_path(host, buf, fingerprint);
-               fp = g_fopen(file, "rb");
+               fp = claws_fopen(file, "rb");
        }
        if (fp == NULL) {
                /* see if we have the old one */
                debug_print("didn't get %s\n", file);
                g_free(file);
                file = get_certificate_path(host, buf, NULL);
-               fp = g_fopen(file, "rb");
+               fp = claws_fopen(file, "rb");
 
                if (fp) {
                        debug_print("got %s\n", file);
@@ -449,7 +449,7 @@ SSLCertificate *ssl_certificate_find (const gchar *host, gushort port, const gch
                gnutls_x509_crt_deinit(tmp_x509);
        }
 
-       fclose(fp);
+       claws_fclose(fp);
        g_free(file);
        
        if (must_rename) {
@@ -531,7 +531,7 @@ static guint check_cert(SSLCertificate *cert)
        FILE *fp;
 
        if (claws_ssl_get_cert_file())
-               fp = g_fopen(claws_ssl_get_cert_file(), "r");
+               fp = claws_fopen(claws_ssl_get_cert_file(), "r");
        else
                return (guint)-1;
 
@@ -540,10 +540,10 @@ static guint check_cert(SSLCertificate *cert)
 
        if ((r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &ca_list, &max_ca)) < 0) {
                debug_print("CA import failed: %s\n", gnutls_strerror(r));
-               fclose(fp);
+               claws_fclose(fp);
                return (guint)-1;
        }
-       fclose(fp);
+       claws_fclose(fp);
        fp = NULL;
        
        buf = g_strdup_printf("%d", cert->port);
@@ -554,20 +554,20 @@ static guint check_cert(SSLCertificate *cert)
                size_t n = 128;
                char *fingerprint;
 
-               fp = g_fopen(chain_file, "r");
+               fp = claws_fopen(chain_file, "r");
                if (fp == NULL) {
-                       debug_print("fopen %s failed: %s\n", chain_file, g_strerror(errno));
+                       debug_print("claws_fopen %s failed: %s\n", chain_file, g_strerror(errno));
                        g_free(chain_file);
                        return (guint)-1;
                }
                if ((r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &chain, &max_certs)) < 0) {
                        debug_print("chain import failed: %s\n", gnutls_strerror(r));
-                       fclose(fp);
+                       claws_fclose(fp);
                        g_free(chain_file);
                        return (guint)-1;
                }
                g_free(chain_file);
-               fclose(fp);
+               claws_fclose(fp);
                fp = NULL;
 
                gnutls_x509_crt_get_fingerprint(chain[0], GNUTLS_DIG_MD5, md, &n);
@@ -670,7 +670,7 @@ static void ssl_certificate_save_chain(gnutls_x509_crt_t *certs, gint len, const
 
                        g_free(buf);
 
-                       fp = g_fopen(file, "wb");
+                       fp = claws_fopen(file, "wb");
                        if (fp == NULL) {
                                g_free(file);
                                debug_print("Can't save certificate !\n");
@@ -683,7 +683,7 @@ static void ssl_certificate_save_chain(gnutls_x509_crt_t *certs, gint len, const
 
        }
        if (fp)
-               safe_fclose(fp);
+               claws_safe_fclose(fp);
 }
 
 gboolean ssl_certificate_check (gnutls_x509_crt_t x509_cert, guint status, 
@@ -816,12 +816,12 @@ gboolean ssl_certificate_check_chain(gnutls_x509_crt_t *certs, gint chain_len,
        gint status;
 
        if (claws_ssl_get_cert_file()) {
-               FILE *fp = g_fopen(claws_ssl_get_cert_file(), "rb");
+               FILE *fp = claws_fopen(claws_ssl_get_cert_file(), "rb");
                int r = -errno;
 
                if (fp) {
                        r = gnutls_import_X509_list_fp(fp, GNUTLS_X509_FMT_PEM, &cas, &ncas);
-                       fclose(fp);
+                       claws_fclose(fp);
                }
 
                if (r < 0)
@@ -861,10 +861,10 @@ gnutls_x509_crt_t ssl_certificate_get_x509_from_pem_file(const gchar *file)
                return NULL;
        
        if (is_file_exist(file)) {
-               FILE *fp = g_fopen(file, "r");
+               FILE *fp = claws_fopen(file, "r");
                if (fp) {
                        x509 = gnutls_import_X509_fp(fp, GNUTLS_X509_FMT_PEM);
-                       fclose(fp);
+                       claws_fclose(fp);
                        return x509;
                } else {
                        log_error(LOG_PROTOCOL, _("Cannot open certificate file %s: %s\n"),
@@ -884,10 +884,10 @@ gnutls_x509_privkey_t ssl_certificate_get_pkey_from_pem_file(const gchar *file)
                return NULL;
        
        if (is_file_exist(file)) {
-               FILE *fp = g_fopen(file, "r");
+               FILE *fp = claws_fopen(file, "r");
                if (fp) {
                        key = gnutls_import_key_fp(fp, GNUTLS_X509_FMT_PEM);
-                       fclose(fp);
+                       claws_fclose(fp);
                        return key;
                } else {
                        log_error(LOG_PROTOCOL, _("Cannot open key file %s (%s)\n"),
@@ -1037,10 +1037,10 @@ void ssl_certificate_get_x509_and_pkey_from_p12_file(const gchar *file, const gc
                return;
 
        if (is_file_exist(file)) {
-               FILE *fp = g_fopen(file, "r");
+               FILE *fp = claws_fopen(file, "r");
                if (fp) {
                        p12 = gnutls_import_PKCS12_fp(fp, GNUTLS_X509_FMT_DER);
-                       fclose(fp);
+                       claws_fclose(fp);
                        if (!p12) {
                                log_error(LOG_PROTOCOL, _("Failed to read P12 certificate file %s\n"), file);
                        }
index 264f139..7c8bf4d 100644 (file)
@@ -40,7 +40,7 @@
 #include "defs.h"
 #include "utils.h"
 #include "tags.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 static GHashTable *tags_table = NULL;
 static GHashTable *tags_reverse_table = NULL;
@@ -53,7 +53,7 @@ void tags_read_tags(void)
                        TAGS_RC, NULL);
        gchar tmp[255];
        gint id;
-       FILE *fp = g_fopen(file, "rb");
+       FILE *fp = claws_fopen(file, "rb");
        
        g_free(file);
 
@@ -69,10 +69,10 @@ void tags_read_tags(void)
        if (!fp) 
                return;
        if (fscanf(fp, "max_id %d\n", &tag_max_id) != 1) {
-               fclose(fp);
+               claws_fclose(fp);
                return;
        }
-       while (fgets(tmp, sizeof(tmp), fp) != NULL) {
+       while (claws_fgets(tmp, sizeof(tmp), fp) != NULL) {
                gchar *sep = strchr(tmp, '\t');
                gchar *tag_name = sep?(sep+1):NULL;
                
@@ -89,7 +89,7 @@ void tags_read_tags(void)
                }
        }
        
-       fclose(fp);
+       claws_fclose(fp);
 }
 
 typedef struct _TagWriteData
@@ -121,10 +121,10 @@ void tags_write_tags(void)
                        TAGS_RC, NULL);
        TagWriteData data;
 
-       FILE *fp = g_fopen(file, "wb");
+       FILE *fp = claws_fopen(file, "wb");
                        
        if (!fp) {
-               FILE_OP_ERROR(file, "g_fopen");
+               FILE_OP_ERROR(file, "claws_fopen");
                g_free(file);
                g_free(file_new);
                return;
@@ -141,14 +141,14 @@ void tags_write_tags(void)
        }
 
        if (data.error) {
-               fclose(fp);
+               claws_fclose(fp);
                g_free(file);
                g_free(file_new);
                return;
        }
        
-       if (safe_fclose(fp) == EOF) {
-               FILE_OP_ERROR(file, "fclose");
+       if (claws_safe_fclose(fp) == EOF) {
+               FILE_OP_ERROR(file, "claws_fclose");
                g_free(file);
                g_free(file_new);
                return;
index e8db301..a7f9ea1 100644 (file)
@@ -29,7 +29,7 @@
 #include "utils.h"
 #include "template.h"
 #include "codeconv.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 static GSList *template_list;
 
@@ -40,8 +40,8 @@ static Template *template_load(gchar *filename)
        gchar buf[BUFFSIZE];
        gint bytes_read;
 
-       if ((fp = g_fopen(filename, "rb")) == NULL) {
-               FILE_OP_ERROR(filename, "fopen");
+       if ((fp = claws_fopen(filename, "rb")) == NULL) {
+               FILE_OP_ERROR(filename, "claws_fopen");
                return NULL;
        }
 
@@ -56,7 +56,7 @@ static Template *template_load(gchar *filename)
        tmpl->replyto = NULL;
        tmpl->value = NULL;
 
-       while (fgets(buf, sizeof(buf), fp) != NULL) {
+       while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
                if (buf[0] == '\n')
                        break;
                else if (!g_ascii_strncasecmp(buf, "Name:", 5))
@@ -78,19 +78,19 @@ static Template *template_load(gchar *filename)
        if (!tmpl->name) {
                g_warning("wrong template format");
                template_free(tmpl);
-               fclose(fp);
+               claws_fclose(fp);
                return NULL;
        }
 
-       if ((bytes_read = fread(buf, 1, sizeof(buf), fp)) == 0) {
-               if (ferror(fp)) {
-                       FILE_OP_ERROR(filename, "fread");
+       if ((bytes_read = claws_fread(buf, 1, sizeof(buf), fp)) == 0) {
+               if (claws_ferror(fp)) {
+                       FILE_OP_ERROR(filename, "claws_fread");
                        template_free(tmpl);
-                       fclose(fp);
+                       claws_fclose(fp);
                        return NULL;
                }
        }
-       fclose(fp);
+       claws_fclose(fp);
        tmpl->value = g_strndup(buf, bytes_read);
 
        return tmpl;
@@ -203,7 +203,7 @@ GSList *template_read_config(void)
 if (!(func)) \
 { \
        g_warning("Failed to write template to file"); \
-       if (fp) fclose(fp); \
+       if (fp) claws_fclose(fp); \
        if (new) claws_unlink(new); \
        g_free(new); \
        g_free(filename); \
@@ -256,8 +256,8 @@ static void template_write_config(GSList *tmpl_list)
                        new = g_strconcat(filename, ".new", NULL);
                }
 
-               if ((fp = g_fopen(new?new:filename, "wb")) == NULL) {
-                       FILE_OP_ERROR(new?new:filename, "fopen");
+               if ((fp = claws_fopen(new?new:filename, "wb")) == NULL) {
+                       FILE_OP_ERROR(new?new:filename, "claws_fopen");
                        g_free(new);
                        g_free(filename);
                        return;
@@ -277,14 +277,14 @@ static void template_write_config(GSList *tmpl_list)
                if (tmpl->replyto && *tmpl->replyto != '\0')
                        TRY(fprintf(fp, "Reply-To: %s\n", tmpl->replyto) > 0);
 
-               TRY(fputs("\n", fp) != EOF);
+               TRY(claws_fputs("\n", fp) != EOF);
 
                if (tmpl->value && *tmpl->value != '\0') {
-                       TRY(fwrite(tmpl->value, sizeof(gchar), strlen(tmpl->value), fp) == strlen(tmpl->value));
+                       TRY(claws_fwrite(tmpl->value, sizeof(gchar), strlen(tmpl->value), fp) == strlen(tmpl->value));
                } else {
-                       TRY(fwrite("", sizeof(gchar), 1, fp) == 1);
+                       TRY(claws_fwrite("", sizeof(gchar), 1, fp) == 1);
                }
-               TRY_NO_CLOSE(safe_fclose(fp) != EOF);
+               TRY_NO_CLOSE(claws_safe_fclose(fp) != EOF);
 
                if (new) {
                        if (rename_force(new, filename) < 0) {
index 977eda4..f288888 100644 (file)
@@ -86,7 +86,7 @@
 #include "codeconv.h"
 #include "tlds.h"
 #include "timing.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #define BUFFSIZE       8192
 
@@ -314,50 +314,6 @@ gchar *strcrchomp(gchar *str)
        return str;
 }
 
-gint file_strip_crs(const gchar *file)
-{
-       FILE *fp = NULL, *outfp = NULL;
-       gchar buf[4096];
-       gchar *out = get_tmp_file();
-       if (file == NULL)
-               goto freeout;
-
-       fp = g_fopen(file, "rb");
-       if (!fp)
-               goto freeout;
-
-       outfp = g_fopen(out, "wb");
-       if (!outfp) {
-               fclose(fp);
-               goto freeout;
-       }
-
-       while (fgets(buf, sizeof (buf), fp) != NULL) {
-               strcrchomp(buf);
-               if (fputs(buf, outfp) == EOF) {
-                       fclose(fp);
-                       fclose(outfp);
-                       goto unlinkout;
-               }
-       }
-
-       fclose(fp);
-       if (safe_fclose(outfp) == EOF) {
-               goto unlinkout;
-       }
-       
-       if (move_file(out, file, TRUE) < 0)
-               goto unlinkout;
-       
-       g_free(out);
-       return 0;
-unlinkout:
-       claws_unlink(out);
-freeout:
-       g_free(out);
-       return -1;
-}
-
 /* Similar to `strstr' but this function ignores the case of both strings.  */
 gchar *strcasestr(const gchar *haystack, const gchar *needle)
 {
@@ -2392,249 +2348,6 @@ gint remove_dir_recursive(const gchar *dir)
        return 0;
 }
 
-gint rename_force(const gchar *oldpath, const gchar *newpath)
-{
-#ifndef G_OS_UNIX
-       if (!is_file_entry_exist(oldpath)) {
-               errno = ENOENT;
-               return -1;
-       }
-       if (is_file_exist(newpath)) {
-               if (claws_unlink(newpath) < 0)
-                       FILE_OP_ERROR(newpath, "unlink");
-       }
-#endif
-       return g_rename(oldpath, newpath);
-}
-
-/*
- * Append src file body to the tail of dest file.
- * Now keep_backup has no effects.
- */
-gint append_file(const gchar *src, const gchar *dest, gboolean keep_backup)
-{
-       FILE *src_fp, *dest_fp;
-       gint n_read;
-       gchar buf[BUFSIZ];
-
-       gboolean err = FALSE;
-
-       if ((src_fp = g_fopen(src, "rb")) == NULL) {
-               FILE_OP_ERROR(src, "g_fopen");
-               return -1;
-       }
-
-       if ((dest_fp = g_fopen(dest, "ab")) == NULL) {
-               FILE_OP_ERROR(dest, "g_fopen");
-               fclose(src_fp);
-               return -1;
-       }
-
-       if (change_file_mode_rw(dest_fp, dest) < 0) {
-               FILE_OP_ERROR(dest, "chmod");
-               g_warning("can't change file mode: %s", dest);
-       }
-
-       while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
-               if (n_read < sizeof(buf) && ferror(src_fp))
-                       break;
-               if (fwrite(buf, 1, n_read, dest_fp) < n_read) {
-                       g_warning("writing to %s failed.", dest);
-                       fclose(dest_fp);
-                       fclose(src_fp);
-                       claws_unlink(dest);
-                       return -1;
-               }
-       }
-
-       if (ferror(src_fp)) {
-               FILE_OP_ERROR(src, "fread");
-               err = TRUE;
-       }
-       fclose(src_fp);
-       if (fclose(dest_fp) == EOF) {
-               FILE_OP_ERROR(dest, "fclose");
-               err = TRUE;
-       }
-
-       if (err) {
-               claws_unlink(dest);
-               return -1;
-       }
-
-       return 0;
-}
-
-gint copy_file(const gchar *src, const gchar *dest, gboolean keep_backup)
-{
-       FILE *src_fp, *dest_fp;
-       gint n_read;
-       gchar buf[BUFSIZ];
-       gchar *dest_bak = NULL;
-       gboolean err = FALSE;
-
-       if ((src_fp = g_fopen(src, "rb")) == NULL) {
-               FILE_OP_ERROR(src, "g_fopen");
-               return -1;
-       }
-       if (is_file_exist(dest)) {
-               dest_bak = g_strconcat(dest, ".bak", NULL);
-               if (rename_force(dest, dest_bak) < 0) {
-                       FILE_OP_ERROR(dest, "rename");
-                       fclose(src_fp);
-                       g_free(dest_bak);
-                       return -1;
-               }
-       }
-
-       if ((dest_fp = g_fopen(dest, "wb")) == NULL) {
-               FILE_OP_ERROR(dest, "g_fopen");
-               fclose(src_fp);
-               if (dest_bak) {
-                       if (rename_force(dest_bak, dest) < 0)
-                               FILE_OP_ERROR(dest_bak, "rename");
-                       g_free(dest_bak);
-               }
-               return -1;
-       }
-
-       if (change_file_mode_rw(dest_fp, dest) < 0) {
-               FILE_OP_ERROR(dest, "chmod");
-               g_warning("can't change file mode: %s", dest);
-       }
-
-       while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), src_fp)) > 0) {
-               if (n_read < sizeof(buf) && ferror(src_fp))
-                       break;
-               if (fwrite(buf, 1, n_read, dest_fp) < n_read) {
-                       g_warning("writing to %s failed.", dest);
-                       fclose(dest_fp);
-                       fclose(src_fp);
-                       claws_unlink(dest);
-                       if (dest_bak) {
-                               if (rename_force(dest_bak, dest) < 0)
-                                       FILE_OP_ERROR(dest_bak, "rename");
-                               g_free(dest_bak);
-                       }
-                       return -1;
-               }
-       }
-
-       if (ferror(src_fp)) {
-               FILE_OP_ERROR(src, "fread");
-               err = TRUE;
-       }
-       fclose(src_fp);
-       if (safe_fclose(dest_fp) == EOF) {
-               FILE_OP_ERROR(dest, "fclose");
-               err = TRUE;
-       }
-
-       if (err) {
-               claws_unlink(dest);
-               if (dest_bak) {
-                       if (rename_force(dest_bak, dest) < 0)
-                               FILE_OP_ERROR(dest_bak, "rename");
-                       g_free(dest_bak);
-               }
-               return -1;
-       }
-
-       if (keep_backup == FALSE && dest_bak)
-               claws_unlink(dest_bak);
-
-       g_free(dest_bak);
-
-       return 0;
-}
-
-gint move_file(const gchar *src, const gchar *dest, gboolean overwrite)
-{
-       if (overwrite == FALSE && is_file_exist(dest)) {
-               g_warning("move_file(): file %s already exists.", dest);
-               return -1;
-       }
-
-       if (rename_force(src, dest) == 0) return 0;
-
-       if (EXDEV != errno) {
-               FILE_OP_ERROR(src, "rename");
-               return -1;
-       }
-
-       if (copy_file(src, dest, FALSE) < 0) return -1;
-
-       claws_unlink(src);
-
-       return 0;
-}
-
-gint copy_file_part_to_fp(FILE *fp, off_t offset, size_t length, FILE *dest_fp)
-{
-       gint n_read;
-       gint bytes_left, to_read;
-       gchar buf[BUFSIZ];
-
-       if (fseek(fp, offset, SEEK_SET) < 0) {
-               perror("fseek");
-               return -1;
-       }
-
-       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, 1, n_read, dest_fp) < n_read) {
-                       return -1;
-               }
-               bytes_left -= n_read;
-               if (bytes_left == 0)
-                       break;
-               to_read = MIN(bytes_left, sizeof(buf));
-       }
-
-       if (ferror(fp)) {
-               perror("fread");
-               return -1;
-       }
-
-       return 0;
-}
-
-gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
-{
-       FILE *dest_fp;
-       gboolean err = FALSE;
-
-       if ((dest_fp = g_fopen(dest, "wb")) == NULL) {
-               FILE_OP_ERROR(dest, "g_fopen");
-               return -1;
-       }
-
-       if (change_file_mode_rw(dest_fp, dest) < 0) {
-               FILE_OP_ERROR(dest, "chmod");
-               g_warning("can't change file mode: %s", dest);
-       }
-
-       if (copy_file_part_to_fp(fp, offset, length, dest_fp) < 0)
-               err = TRUE;
-
-       if (safe_fclose(dest_fp) == EOF) {
-               FILE_OP_ERROR(dest, "fclose");
-               err = TRUE;
-       }
-
-       if (err) {
-               g_warning("writing to %s failed.", dest);
-               claws_unlink(dest);
-               return -1;
-       }
-
-       return 0;
-}
-
 /* convert line endings into CRLF. If the last line doesn't end with
  * linebreak, add it.
  */
@@ -2671,109 +2384,6 @@ gchar *canonicalize_str(const gchar *str)
        return out;
 }
 
-gint canonicalize_file(const gchar *src, const gchar *dest)
-{
-       FILE *src_fp, *dest_fp;
-       gchar buf[BUFFSIZE];
-       gint len;
-       gboolean err = FALSE;
-       gboolean last_linebreak = FALSE;
-
-       if (src == NULL || dest == NULL)
-               return -1;
-
-       if ((src_fp = g_fopen(src, "rb")) == NULL) {
-               FILE_OP_ERROR(src, "g_fopen");
-               return -1;
-       }
-
-       if ((dest_fp = g_fopen(dest, "wb")) == NULL) {
-               FILE_OP_ERROR(dest, "g_fopen");
-               fclose(src_fp);
-               return -1;
-       }
-
-       if (change_file_mode_rw(dest_fp, dest) < 0) {
-               FILE_OP_ERROR(dest, "chmod");
-               g_warning("can't change file mode: %s", dest);
-       }
-
-       while (fgets(buf, sizeof(buf), src_fp) != NULL) {
-               gint r = 0;
-
-               len = strlen(buf);
-               if (len == 0) break;
-               last_linebreak = FALSE;
-
-               if (buf[len - 1] != '\n') {
-                       last_linebreak = TRUE;
-                       r = fputs(buf, dest_fp);
-               } else if (len > 1 && buf[len - 1] == '\n' && buf[len - 2] == '\r') {
-                       r = fputs(buf, dest_fp);
-               } else {
-                       if (len > 1) {
-                               r = fwrite(buf, 1, len - 1, dest_fp);
-                               if (r != (len -1))
-                                       r = EOF;
-                       }
-                       if (r != EOF)
-                               r = fputs("\r\n", dest_fp);
-               }
-
-               if (r == EOF) {
-                       g_warning("writing to %s failed.", dest);
-                       fclose(dest_fp);
-                       fclose(src_fp);
-                       claws_unlink(dest);
-                       return -1;
-               }
-       }
-
-       if (last_linebreak == TRUE) {
-               if (fputs("\r\n", dest_fp) == EOF)
-                       err = TRUE;
-       }
-
-       if (ferror(src_fp)) {
-               FILE_OP_ERROR(src, "fgets");
-               err = TRUE;
-       }
-       fclose(src_fp);
-       if (safe_fclose(dest_fp) == EOF) {
-               FILE_OP_ERROR(dest, "fclose");
-               err = TRUE;
-       }
-
-       if (err) {
-               claws_unlink(dest);
-               return -1;
-       }
-
-       return 0;
-}
-
-gint canonicalize_file_replace(const gchar *file)
-{
-       gchar *tmp_file;
-
-       tmp_file = get_tmp_file();
-
-       if (canonicalize_file(file, tmp_file) < 0) {
-               g_free(tmp_file);
-               return -1;
-       }
-
-       if (move_file(tmp_file, file, TRUE) < 0) {
-               g_warning("can't replace file: %s", file);
-               claws_unlink(tmp_file);
-               g_free(tmp_file);
-               return -1;
-       }
-
-       g_free(tmp_file);
-       return 0;
-}
-
 gchar *normalize_newlines(const gchar *str)
 {
        const gchar *p;
@@ -2802,7 +2412,7 @@ gchar *get_outgoing_rfc2822_str(FILE *fp)
        str = g_string_new(NULL);
 
        /* output header part */
-       while (fgets(buf, sizeof(buf), fp) != NULL) {
+       while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
                strretchomp(buf);
                if (!g_ascii_strncasecmp(buf, "Bcc:", 4)) {
                        gint next;
@@ -2815,7 +2425,7 @@ gchar *get_outgoing_rfc2822_str(FILE *fp)
                                        ungetc(next, fp);
                                        break;
                                }
-                               if (fgets(buf, sizeof(buf), fp) == NULL)
+                               if (claws_fgets(buf, sizeof(buf), fp) == NULL)
                                        break;
                        }
                } else {
@@ -2827,7 +2437,7 @@ gchar *get_outgoing_rfc2822_str(FILE *fp)
        }
 
        /* output body part */
-       while (fgets(buf, sizeof(buf), fp) != NULL) {
+       while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
                strretchomp(buf);
                if (buf[0] == '.')
                        g_string_append_c(str, '.');
@@ -2874,280 +2484,6 @@ gchar *generate_mime_boundary(const gchar *prefix)
                               buf_uniq);
 }
 
-gint change_file_mode_rw(FILE *fp, const gchar *file)
-{
-#if HAVE_FCHMOD
-       return fchmod(fileno(fp), S_IRUSR|S_IWUSR);
-#else
-       return g_chmod(file, S_IRUSR|S_IWUSR);
-#endif
-}
-
-FILE *my_tmpfile(void)
-{
-       const gchar suffix[] = ".XXXXXX";
-       const gchar *tmpdir;
-       guint tmplen;
-       const gchar *progname;
-       guint proglen;
-       gchar *fname;
-       gint fd;
-       FILE *fp;
-#ifndef G_OS_WIN32
-       gchar buf[2]="\0";
-#endif
-
-       tmpdir = get_tmp_dir();
-       tmplen = strlen(tmpdir);
-       progname = g_get_prgname();
-       if (progname == NULL)
-               progname = "claws-mail";
-       proglen = strlen(progname);
-       Xalloca(fname, tmplen + 1 + proglen + sizeof(suffix),
-               return tmpfile());
-
-       memcpy(fname, tmpdir, tmplen);
-       fname[tmplen] = G_DIR_SEPARATOR;
-       memcpy(fname + tmplen + 1, progname, proglen);
-       memcpy(fname + tmplen + 1 + proglen, suffix, sizeof(suffix));
-
-       fd = g_mkstemp(fname);
-       if (fd < 0)
-               return tmpfile();
-
-#ifndef G_OS_WIN32
-       claws_unlink(fname);
-       
-       /* verify that we can write in the file after unlinking */
-       if (write(fd, buf, 1) < 0) {
-               close(fd);
-               return tmpfile();
-       }
-       
-#endif
-
-       fp = fdopen(fd, "w+b");
-       if (!fp)
-               close(fd);
-       else {
-               rewind(fp);
-               return fp;
-       }
-
-       return tmpfile();
-}
-
-FILE *get_tmpfile_in_dir(const gchar *dir, gchar **filename)
-{
-       int fd;
-       *filename = g_strdup_printf("%s%cclaws.XXXXXX", dir, G_DIR_SEPARATOR);
-       fd = g_mkstemp(*filename);
-       if (fd < 0)
-               return NULL;
-       return fdopen(fd, "w+");
-}
-
-FILE *str_open_as_stream(const gchar *str)
-{
-       FILE *fp;
-       size_t len;
-
-       cm_return_val_if_fail(str != NULL, NULL);
-
-       fp = my_tmpfile();
-       if (!fp) {
-               FILE_OP_ERROR("str_open_as_stream", "my_tmpfile");
-               return NULL;
-       }
-
-       len = strlen(str);
-       if (len == 0) return fp;
-
-       if (fwrite(str, 1, len, fp) != len) {
-               FILE_OP_ERROR("str_open_as_stream", "fwrite");
-               fclose(fp);
-               return NULL;
-       }
-
-       rewind(fp);
-       return fp;
-}
-
-gint str_write_to_file(const gchar *str, const gchar *file)
-{
-       FILE *fp;
-       size_t len;
-
-       cm_return_val_if_fail(str != NULL, -1);
-       cm_return_val_if_fail(file != NULL, -1);
-
-       if ((fp = g_fopen(file, "wb")) == NULL) {
-               FILE_OP_ERROR(file, "g_fopen");
-               return -1;
-       }
-
-       len = strlen(str);
-       if (len == 0) {
-               fclose(fp);
-               return 0;
-       }
-
-       if (fwrite(str, 1, len, fp) != len) {
-               FILE_OP_ERROR(file, "fwrite");
-               fclose(fp);
-               claws_unlink(file);
-               return -1;
-       }
-
-       if (safe_fclose(fp) == EOF) {
-               FILE_OP_ERROR(file, "fclose");
-               claws_unlink(file);
-               return -1;
-       }
-
-       return 0;
-}
-
-static gchar *file_read_stream_to_str_full(FILE *fp, gboolean recode)
-{
-       GByteArray *array;
-       guchar buf[BUFSIZ];
-       gint n_read;
-       gchar *str;
-
-       cm_return_val_if_fail(fp != NULL, NULL);
-
-       array = g_byte_array_new();
-
-       while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
-               if (n_read < sizeof(buf) && ferror(fp))
-                       break;
-               g_byte_array_append(array, buf, n_read);
-       }
-
-       if (ferror(fp)) {
-               FILE_OP_ERROR("file stream", "fread");
-               g_byte_array_free(array, TRUE);
-               return NULL;
-       }
-
-       buf[0] = '\0';
-       g_byte_array_append(array, buf, 1);
-       str = (gchar *)array->data;
-       g_byte_array_free(array, FALSE);
-
-       if (recode && !g_utf8_validate(str, -1, NULL)) {
-               const gchar *src_codeset, *dest_codeset;
-               gchar *tmp = NULL;
-               src_codeset = conv_get_locale_charset_str();
-               dest_codeset = CS_UTF_8;
-               tmp = conv_codeset_strdup(str, src_codeset, dest_codeset);
-               g_free(str);
-               str = tmp;
-       }
-
-       return str;
-}
-
-static gchar *file_read_to_str_full(const gchar *file, gboolean recode)
-{
-       FILE *fp;
-       gchar *str;
-       GStatBuf s;
-#ifndef G_OS_WIN32
-       gint fd, err;
-       struct timeval timeout = {1, 0};
-       fd_set fds;
-       int fflags = 0;
-#endif
-
-       cm_return_val_if_fail(file != NULL, NULL);
-
-       if (g_stat(file, &s) != 0) {
-               FILE_OP_ERROR(file, "stat");
-               return NULL;
-       }
-       if (S_ISDIR(s.st_mode)) {
-               g_warning("%s: is a directory", file);
-               return NULL;
-       }
-
-#ifdef G_OS_WIN32
-       fp = g_fopen (file, "rb");
-       if (fp == NULL) {
-               FILE_OP_ERROR(file, "open");
-               return NULL;
-       }
-#else    
-       /* test whether the file is readable without blocking */
-       fd = g_open(file, O_RDONLY | O_NONBLOCK, 0);
-       if (fd == -1) {
-               FILE_OP_ERROR(file, "open");
-               return NULL;
-       }
-
-       FD_ZERO(&fds);
-       FD_SET(fd, &fds);
-
-       /* allow for one second */
-       err = select(fd+1, &fds, NULL, NULL, &timeout);
-       if (err <= 0 || !FD_ISSET(fd, &fds)) {
-               if (err < 0) {
-                       FILE_OP_ERROR(file, "select");
-               } else {
-                       g_warning("%s: doesn't seem readable", file);
-               }
-               close(fd);
-               return NULL;
-       }
-       
-       /* Now clear O_NONBLOCK */
-       if ((fflags = fcntl(fd, F_GETFL)) < 0) {
-               FILE_OP_ERROR(file, "fcntl (F_GETFL)");
-               close(fd);
-               return NULL;
-       }
-       if (fcntl(fd, F_SETFL, (fflags & ~O_NONBLOCK)) < 0) {
-               FILE_OP_ERROR(file, "fcntl (F_SETFL)");
-               close(fd);
-               return NULL;
-       }
-       
-       /* get the FILE pointer */
-       fp = fdopen(fd, "rb");
-
-       if (fp == NULL) {
-               FILE_OP_ERROR(file, "fdopen");
-               close(fd); /* if fp isn't NULL, we'll use fclose instead! */
-               return NULL;
-       }
-#endif
-
-       str = file_read_stream_to_str_full(fp, recode);
-
-       fclose(fp);
-
-       return str;
-}
-
-gchar *file_read_to_str(const gchar *file)
-{
-       return file_read_to_str_full(file, TRUE);
-}
-gchar *file_read_stream_to_str(FILE *fp)
-{
-       return file_read_stream_to_str_full(fp, TRUE);
-}
-
-gchar *file_read_to_str_no_recode(const gchar *file)
-{
-       return file_read_to_str_full(file, FALSE);
-}
-gchar *file_read_stream_to_str_no_recode(FILE *fp)
-{
-       return file_read_stream_to_str_full(fp, FALSE);
-}
-
 char *fgets_crlf(char *buf, int size, FILE *stream)
 {
        gboolean is_cr = FALSE;
@@ -4429,12 +3765,12 @@ gchar *make_http_string(const gchar *bp, const gchar *ep)
 
 static gchar *mailcap_get_command_in_file(const gchar *path, const gchar *type, const gchar *file_to_open)
 {
-       FILE *fp = g_fopen(path, "rb");
+       FILE *fp = claws_fopen(path, "rb");
        gchar buf[BUFFSIZE];
        gchar *result = NULL;
        if (!fp)
                return NULL;
-       while (fgets(buf, sizeof (buf), fp) != NULL) {
+       while (claws_fgets(buf, sizeof (buf), fp) != NULL) {
                gchar **parts = g_strsplit(buf, ";", 3);
                gchar *trimmed = parts[0];
                while (trimmed[0] == ' ' || trimmed[0] == '\t')
@@ -4493,7 +3829,7 @@ static gchar *mailcap_get_command_in_file(const gchar *path, const gchar *type,
                                trimmed[strlen(trimmed)-1] = '\0';
                        result = g_strdup(trimmed);
                        g_strfreev(parts);
-                       fclose(fp);
+                       claws_fclose(fp);
                        if (needsterminal) {
                                gchar *tmp = g_strdup_printf("xterm -e %s", result);
                                g_free(result);
@@ -4503,7 +3839,7 @@ static gchar *mailcap_get_command_in_file(const gchar *path, const gchar *type,
                }
                g_strfreev(parts);
        }
-       fclose(fp);
+       claws_fclose(fp);
        return NULL;
 }
 gchar *mailcap_get_command_for_type(const gchar *type, const gchar *file_to_open)
@@ -4526,13 +3862,13 @@ void mailcap_update_default(const gchar *type, const gchar *command)
        gchar *path = NULL, *outpath = NULL;
        path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".mailcap", NULL);
        outpath = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".mailcap.new", NULL);
-       FILE *fp = g_fopen(path, "rb");
+       FILE *fp = claws_fopen(path, "rb");
        FILE *outfp = NULL;
        gchar buf[BUFFSIZE];
        gboolean err = FALSE;
 
        if (!fp) {
-               fp = g_fopen(path, "a");
+               fp = claws_fopen(path, "a");
                if (!fp) {
                        g_warning("failed to create file %s", path);
                        g_free(path);
@@ -4548,15 +3884,15 @@ void mailcap_update_default(const gchar *type, const gchar *command)
                }
        }
 
-       outfp = g_fopen(outpath, "wb");
+       outfp = claws_fopen(outpath, "wb");
        if (!outfp) {
                g_warning("failed to create file %s", outpath);
                g_free(path);
                g_free(outpath);
-               fclose(fp);
+               claws_fclose(fp);
                return;
        }
-       while (fp && fgets(buf, sizeof (buf), fp) != NULL) {
+       while (fp && claws_fgets(buf, sizeof (buf), fp) != NULL) {
                gchar **parts = g_strsplit(buf, ";", 3);
                gchar *trimmed = parts[0];
                while (trimmed[0] == ' ')
@@ -4569,7 +3905,7 @@ void mailcap_update_default(const gchar *type, const gchar *command)
                        continue;
                }
                else {
-                       if(fputs(buf, outfp) == EOF) {
+                       if(claws_fputs(buf, outfp) == EOF) {
                                err = TRUE;
                                break;
                        }
@@ -4580,9 +3916,9 @@ void mailcap_update_default(const gchar *type, const gchar *command)
                err = TRUE;
 
        if (fp)
-               fclose(fp);
+               claws_fclose(fp);
 
-       if (safe_fclose(outfp) == EOF)
+       if (claws_safe_fclose(outfp) == EOF)
                err = TRUE;
                
        if (!err)
@@ -4592,60 +3928,6 @@ void mailcap_update_default(const gchar *type, const gchar *command)
        g_free(outpath);
 }
 
-gint copy_dir(const gchar *src, const gchar *dst)
-{
-       GDir *dir;
-       const gchar *name;
-
-       if ((dir = g_dir_open(src, 0, NULL)) == NULL) {
-               g_warning("failed to open directory: %s", src);
-               return -1;
-       }
-
-       if (make_dir(dst) < 0)
-               return -1;
-
-       while ((name = g_dir_read_name(dir)) != NULL) {
-               gchar *old_file, *new_file;
-               old_file = g_strconcat(src, G_DIR_SEPARATOR_S, name, NULL);
-               new_file = g_strconcat(dst, G_DIR_SEPARATOR_S, name, NULL);
-               debug_print("copying: %s -> %s\n", old_file, new_file);
-               if (g_file_test(old_file, G_FILE_TEST_IS_REGULAR)) {
-                       gint r = copy_file(old_file, new_file, TRUE);
-                       if (r < 0) {
-                               g_dir_close(dir);
-                               return r;
-                       }
-                }
-#ifndef G_OS_WIN32
-                /* Windows has no symlinks.  Or well, Vista seems to
-                   have something like this but the semantics might be
-                   different.  Thus we don't use it under Windows. */
-                else if (g_file_test(old_file, G_FILE_TEST_IS_SYMLINK)) {
-                       GError *error = NULL;
-                       gint r = 0;
-                       gchar *target = g_file_read_link(old_file, &error);
-                       if (target)
-                               r = symlink(target, new_file);
-                       g_free(target);
-                       if (r < 0) {
-                               g_dir_close(dir);
-                               return r;
-                       }
-                 }
-#endif /*G_OS_WIN32*/
-               else if (g_file_test(old_file, G_FILE_TEST_IS_DIR)) {
-                       gint r = copy_dir(old_file, new_file);
-                       if (r < 0) {
-                               g_dir_close(dir);
-                               return r;
-                       }
-               }
-       }
-       g_dir_close(dir);
-       return 0;
-}
-
 /* crude test to see if a file is an email. */
 gboolean file_is_email (const gchar *filename)
 {
@@ -4655,10 +3937,10 @@ gboolean file_is_email (const gchar *filename)
        gint score = 0;
        if (filename == NULL)
                return FALSE;
-       if ((fp = g_fopen(filename, "rb")) == NULL)
+       if ((fp = claws_fopen(filename, "rb")) == NULL)
                return FALSE;
        while (i < 60 && score < 3
-              && fgets(buffer, sizeof (buffer), fp) != NULL) {
+              && claws_fgets(buffer, sizeof (buffer), fp) != NULL) {
                if (!strncmp(buffer, "From:", strlen("From:")))
                        score++;
                else if (!strncmp(buffer, "Date:", strlen("Date:")))
@@ -4669,7 +3951,7 @@ gboolean file_is_email (const gchar *filename)
                        score++;
                i++;
        }
-       fclose(fp);
+       claws_fclose(fp);
        return (score >= 3);
 }
 
@@ -5046,49 +4328,10 @@ size_t fast_strftime(gchar *buf, gint buflen, const gchar *format, struct tm *lt
        return total_done;
 }
 
-gboolean prefs_common_get_use_shred(void);
-
-
 #ifdef G_OS_WIN32
 #define WEXITSTATUS(x) (x)
 #endif
 
-int claws_unlink(const gchar *filename) 
-{
-       GStatBuf s;
-       static int found_shred = -1;
-       static const gchar *args[4];
-
-       if (filename == NULL)
-               return 0;
-
-       if (prefs_common_get_use_shred()) {
-               if (found_shred == -1) {
-                       /* init */
-                       args[0] = g_find_program_in_path("shred");
-                       debug_print("found shred: %s\n", args[0]);
-                       found_shred = (args[0] != NULL) ? 1:0;
-                       args[1] = "-f";
-                       args[3] = NULL;
-               }
-               if (found_shred == 1) {
-                       if (g_stat(filename, &s) == 0 && S_ISREG(s.st_mode)) {
-                               if (s.st_nlink == 1) {
-                                       gint status=0;
-                                       args[2] = filename;
-                                       g_spawn_sync(NULL, (gchar **)args, NULL, 0,
-                                        NULL, NULL, NULL, NULL, &status, NULL);
-                                       debug_print("%s %s exited with status %d\n",
-                                               args[0], filename, WEXITSTATUS(status));
-                                       if (truncate(filename, 0) < 0)
-                                               g_warning("couln't truncate: %s", filename);
-                               }
-                       }
-               }
-       }
-       return g_unlink(filename);
-}
-
 GMutex *cm_mutex_new(void) {
 #if GLIB_CHECK_VERSION(2,32,0)
        GMutex *m = g_new0(GMutex, 1);
index 96c4dc2..dfb327f 100644 (file)
@@ -281,7 +281,6 @@ gchar *strretchomp  (gchar          *str);
 gchar *strtailchomp    (gchar          *str,
                         gchar           tail_char);
 gchar *strcrchomp      (gchar          *str);
-gint file_strip_crs    (const gchar    *file);
 gchar *strcasestr      (const gchar    *haystack,
                         const gchar    *needle);
 gchar *strncasestr     (const gchar    *haystack,
@@ -427,50 +426,11 @@ gint remove_numbered_files_not_in_list(const gchar *dir,
                                       GSList *numberlist);
 gint remove_all_numbered_files (const gchar    *dir);
 gint remove_dir_recursive      (const gchar    *dir);
-gint append_file               (const gchar    *src,
-                                const gchar    *dest,
-                                gboolean        keep_backup);
-gint rename_force              (const gchar    *oldpath,
-                                const gchar    *newpath);
-gint copy_file                 (const gchar    *src,
-                                const gchar    *dest,
-                                gboolean        keep_backup);
-gint move_file                 (const gchar    *src,
-                                const gchar    *dest,
-                                gboolean        overwrite);
-gint copy_dir                  (const gchar    *src,
-                                const gchar    *dest);
-gint copy_file_part_to_fp      (FILE           *fp,
-                                off_t           offset,
-                                size_t          length,
-                                FILE           *dest_fp);
-gint copy_file_part            (FILE           *fp,
-                                off_t           offset,
-                                size_t          length,
-                                const gchar    *dest);
-
 gchar *canonicalize_str                (const gchar    *str);
-gint canonicalize_file         (const gchar    *src,
-                                const gchar    *dest);
-gint canonicalize_file_replace (const gchar    *file);
-
 gchar *normalize_newlines      (const gchar    *str);
 
 gchar *get_outgoing_rfc2822_str        (FILE           *fp);
 
-gint change_file_mode_rw       (FILE           *fp,
-                                const gchar    *file);
-FILE *my_tmpfile               (void);
-FILE *get_tmpfile_in_dir       (const gchar    *dir,
-                                gchar         **filename);
-FILE *str_open_as_stream       (const gchar    *str);
-gint str_write_to_file         (const gchar    *str,
-                                const gchar    *file);
-gchar *file_read_to_str                (const gchar    *file);
-gchar *file_read_stream_to_str (FILE           *fp);
-gchar *file_read_to_str_no_recode(const gchar *file);
-gchar *file_read_stream_to_str_no_recode(FILE *fp);
-
 char *fgets_crlf(char *buf, int size, FILE *stream);
 
 /* process execution */
@@ -563,8 +523,6 @@ gboolean file_is_email(const gchar *filename);
 gboolean sc_g_list_bigger(GList *list, gint max);
 gboolean sc_g_slist_bigger(GSList *list, gint max);
 
-int claws_unlink(const gchar *filename);
-
 GMutex *cm_mutex_new(void);
 void cm_mutex_free(GMutex *mutex);
 
index 45fcd30..874db1f 100644 (file)
  * 
  */
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#include "claws-features.h"
+#endif
+
 #include <glib.h>
 #include <stdio.h>
 #include <string.h>
@@ -25,6 +30,7 @@
 #include "xml.h"
 #include "utils.h"
 #include "codeconv.h"
+#include "file-utils.h"
 
 #define SPARSE_MEMORY
 /* if this is defined all attr.names and tag.names are stored
@@ -80,7 +86,7 @@ XMLFile *xml_open_file(const gchar *path)
 
        newfile = g_new(XMLFile, 1);
 
-       newfile->fp = g_fopen(path, "rb");
+       newfile->fp = claws_fopen(path, "rb");
        if (!newfile->fp) {
                g_free(newfile);
                return NULL;
@@ -97,6 +103,8 @@ XMLFile *xml_open_file(const gchar *path)
        newfile->level = 0;
        newfile->is_empty_element = FALSE;
 
+       newfile->path = g_strdup(path);
+
        return newfile;
 }
 
@@ -104,12 +112,13 @@ void xml_close_file(XMLFile *file)
 {
        cm_return_if_fail(file != NULL);
 
-       if (file->fp) fclose(file->fp);
+       if (file->fp) claws_fclose(file->fp);
 
        g_string_free(file->buf, TRUE);
 
        g_free(file->dtd);
        g_free(file->encoding);
+       g_free(file->path);
 
        while (file->tag_stack != NULL)
                xml_pop_tag(file);
@@ -126,7 +135,7 @@ static GNode *xml_build_tree(XMLFile *file, GNode *parent, guint level)
        while (xml_parse_next_tag(file) == 0) {
                if (file->level < level) break;
                if (file->level == level) {
-                       g_warning("xml_build_tree(): Parse error");
+                       g_warning("xml_build_tree(): Parse error in %s", file->path);
                        break;
                }
 
@@ -191,7 +200,7 @@ gint xml_get_dtd(XMLFile *file)
                        file->need_codeconv = FALSE;
                }
        } else {
-               g_warning("Can't get XML DTD");
+               g_warning("Can't get XML DTD in %s", file->path);
                return -1;
        }
 
@@ -214,7 +223,7 @@ next:
        }
 
        if (xml_get_parenthesis(file, buf, sizeof(buf)) < 0) {
-               g_warning("xml_parse_next_tag(): Can't parse next tag");
+               g_warning("xml_parse_next_tag(): Can't parse next tag  in %s", file->path);
                return -1;
        }
 
@@ -223,7 +232,7 @@ next:
        /* end-tag */
        if (buf[0] == '/') {
                if (strcmp(xml_get_current_tag(file)->tag, buf + 1) != 0) {
-                       g_warning("xml_parse_next_tag(): Tag name mismatch: %s (%s)", buf, xml_get_current_tag(file)->tag);
+                       g_warning("xml_parse_next_tag(): Tag name mismatch in %s : %s (%s)", file->path, buf, xml_get_current_tag(file)->tag);
                        return -1;
                }
                xml_pop_tag(file);
@@ -245,7 +254,7 @@ next:
        }
        
        if (strlen(buf) == 0) {
-               g_warning("xml_parse_next_tag(): Tag name is empty");
+               g_warning("xml_parse_next_tag(): Tag name is empty in %s", file->path);
                return -1;
        }
 
@@ -287,7 +296,7 @@ next:
                while (g_ascii_isspace(*bufp)) bufp++;
                attr_name = bufp;
                if ((p = strchr(attr_name, '=')) == NULL) {
-                       g_warning("xml_parse_next_tag(): Syntax error in tag (a) %s", attr_name);
+                       g_warning("xml_parse_next_tag(): Syntax error in %s, tag (a) %s", file->path, attr_name);
                        return -1;
                }
                bufp = p;
@@ -295,14 +304,14 @@ next:
                while (g_ascii_isspace(*bufp)) bufp++;
 
                if (*bufp != '"' && *bufp != '\'') {
-                       g_warning("xml_parse_next_tag(): Syntax error in tag (b) %s", bufp);
+                       g_warning("xml_parse_next_tag(): Syntax error in %s, tag (b) %s", file->path, bufp);
                        return -1;
                }
                quote = *bufp;
                bufp++;
                attr_value = bufp;
                if ((p = strchr(attr_value, quote)) == NULL) {
-                       g_warning("xml_parse_next_tag(): Syntax error in tag (c) %s", attr_value);
+                       g_warning("xml_parse_next_tag(): Syntax error in %s, tag (c) %s", file->path, attr_value);
                        return -1;
                }
                bufp = p;
@@ -414,7 +423,7 @@ static gint xml_read_line(XMLFile *file)
        gchar buf[XMLBUFSIZE];
        gint index;
 
-       if (fgets(buf, sizeof(buf), file->fp) == NULL)
+       if (claws_fgets(buf, sizeof(buf), file->fp) == NULL)
                return -1;
 
        index = file->bufp - file->buf->str;
@@ -581,22 +590,22 @@ gint xml_file_put_escape_str(FILE *fp, const gchar *str)
        for (p = str; *p != '\0'; p++) {
                switch (*p) {
                case '<':
-                       result = fputs("&lt;", fp);
+                       result = claws_fputs("&lt;", fp);
                        break;
                case '>':
-                       result = fputs("&gt;", fp);
+                       result = claws_fputs("&gt;", fp);
                        break;
                case '&':
-                       result = fputs("&amp;", fp);
+                       result = claws_fputs("&amp;", fp);
                        break;
                case '\'':
-                       result = fputs("&apos;", fp);
+                       result = claws_fputs("&apos;", fp);
                        break;
                case '\"':
-                       result = fputs("&quot;", fp);
+                       result = claws_fputs("&quot;", fp);
                        break;
                default:
-                       result = fputc(*p, fp);
+                       result = claws_fputc(*p, fp);
                }
        }
 
@@ -695,7 +704,7 @@ static int xml_write_tree_recursive(GNode *node, FILE *fp)
 
        depth = g_node_depth(node) - 1;
        for (i = 0; i < depth; i++)
-               TRY(fputs("    ", fp) != EOF);
+               TRY(claws_fputs("    ", fp) != EOF);
 
        tag = ((XMLNode *) node->data)->tag;
 
@@ -706,13 +715,13 @@ static int xml_write_tree_recursive(GNode *node, FILE *fp)
 
                TRY(fprintf(fp, " %s=\"", attr->name) > 0);
                TRY(xml_file_put_escape_str(fp, attr->value) == 0);
-               TRY(fputs("\"", fp) != EOF);
+               TRY(claws_fputs("\"", fp) != EOF);
                
        }
 
        if (node->children) {
                GNode *child;
-               TRY(fputs(">\n", fp) != EOF);
+               TRY(claws_fputs(">\n", fp) != EOF);
 
                child = node->children;
                while (child) {
@@ -724,10 +733,10 @@ static int xml_write_tree_recursive(GNode *node, FILE *fp)
                }
 
                for (i = 0; i < depth; i++)
-                       TRY(fputs("    ", fp) != EOF);
+                       TRY(claws_fputs("    ", fp) != EOF);
                TRY(fprintf(fp, "</%s>\n", tag->tag) > 0);
        } else
-               TRY(fputs(" />\n", fp) != EOF);
+               TRY(claws_fputs(" />\n", fp) != EOF);
        
        return 0;
 }
index 225a12e..6ca9059 100644 (file)
@@ -51,6 +51,7 @@ struct _XMLNode
 struct _XMLFile
 {
        FILE *fp;
+       gchar *path;
 
        GString *buf;
        gchar *bufp;
index 9aadc3b..0557bd2 100644 (file)
  * ***********************************************************************
  */
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#include "claws-features.h"
+#endif
 
 #include <glib.h>
 #include <stdio.h>
@@ -41,6 +45,7 @@
 #include "mgutils.h"
 #include "xmlprops.h"
 #include "utils.h"
+#include "file-utils.h"
 
 /* Element tag names */
 #define XMLS_ELTAG_PROP_LIST     "property-list"
@@ -126,12 +131,12 @@ void xmlprops_free( XmlProperty *props ) {
 static int xmlprops_write_elem_s( FILE *fp, gint lvl, gchar *name ) {
        gint i;
        for( i = 0; i < lvl; i++ ) {
-               if(fputs( "  ", fp ) == EOF)
+               if(claws_fputs( "  ", fp ) == EOF)
                        return -1;
        }
-       if(fputs( "<", fp ) == EOF)
+       if(claws_fputs( "<", fp ) == EOF)
                return -1;
-       if(fputs( name, fp ) == EOF)
+       if(claws_fputs( name, fp ) == EOF)
                return -1;
        
        return 0;
@@ -140,29 +145,29 @@ static int xmlprops_write_elem_s( FILE *fp, gint lvl, gchar *name ) {
 static int xmlprops_write_elem_e( FILE *fp, gint lvl, gchar *name ) {
        gint i;
        for( i = 0; i < lvl; i++ ) {
-               if(fputs( "  ", fp ) == EOF)
+               if(claws_fputs( "  ", fp ) == EOF)
                        return -1;
        }
-       if(fputs( "</", fp ) == EOF)
+       if(claws_fputs( "</", fp ) == EOF)
                return -1;
-       if(fputs( name, fp ) == EOF)
+       if(claws_fputs( name, fp ) == EOF)
                return -1;
-       if(fputs( ">\n", fp ) == EOF)
+       if(claws_fputs( ">\n", fp ) == EOF)
                return -1;
        
        return 0;
 }
 
 static int xmlprops_write_attr( FILE *fp, gchar *name, gchar *value ) {
-       if(fputs( " ", fp ) == EOF)
+       if(claws_fputs( " ", fp ) == EOF)
                return -1;
-       if(fputs( name, fp ) == EOF)
+       if(claws_fputs( name, fp ) == EOF)
                return -1;
-       if(fputs( "=\"", fp ) == EOF)
+       if(claws_fputs( "=\"", fp ) == EOF)
                return -1;
        if(xml_file_put_escape_str( fp, value ) < 0)
                return -1;
-       if(fputs( "\"", fp ) == EOF)
+       if(claws_fputs( "\"", fp ) == EOF)
                return -1;
        
        return 0;
@@ -177,7 +182,7 @@ static void xmlprops_write_vis( gpointer key, gpointer value, gpointer d ) {
                data->error = 1;
        if(xmlprops_write_attr( data->fp, XMLS_ATTAG_VALUE, value ) < 0)
                data->error = 1;
-       if(fputs( " />\n", data->fp ) == EOF)
+       if(claws_fputs( " />\n", data->fp ) == EOF)
                data->error = 1;
 }
 
@@ -200,7 +205,7 @@ static gint xmlprops_write_to( XmlProperty *props, const gchar *fileSpec ) {
                        goto revert;
                if(xmlprops_write_elem_s( fp, 0, XMLS_ELTAG_PROP_LIST ) < 0)
                        goto revert;
-               if(fputs( ">\n", fp ) == EOF)
+               if(claws_fputs( ">\n", fp ) == EOF)
                        goto revert;
 
                /* Output all properties */
index ade8db9..0d8f774 100644 (file)
 #include "autofaces.h"
 #include "spell_entry.h"
 #include "headers.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #ifdef USE_LDAP
 #include "password.h"
@@ -2433,12 +2433,12 @@ Compose *compose_reedit(MsgInfo *msginfo, gboolean batch)
                gboolean prev_autowrap;
                GtkTextBuffer *buffer;
                BLOCK_WRAP();
-               while (fgets(buf, sizeof(buf), fp) != NULL) {
+               while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
                        strcrchomp(buf);
                        gtk_text_buffer_insert(textbuf, &iter, buf, -1);
                }
                UNBLOCK_WRAP();
-               fclose(fp);
+               claws_fclose(fp);
        }
        
        compose_attach_parts(compose, msginfo);
@@ -2873,7 +2873,7 @@ static gint compose_parse_header(Compose *compose, MsgInfo *msginfo)
 
        if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
        procheader_get_header_fields(fp, hentry);
-       fclose(fp);
+       claws_fclose(fp);
 
        if (hentry[H_REPLY_TO].body != NULL) {
                if (hentry[H_REPLY_TO].body[0] != '\0') {
@@ -2998,7 +2998,7 @@ static gint compose_parse_manual_headers(Compose *compose, MsgInfo *msginfo, Hea
 
        if ((fp = procmsg_open_message(msginfo)) == NULL) return -1;
        procheader_get_header_fields(fp, entries);
-       fclose(fp);
+       claws_fclose(fp);
 
        he = entries;
        while (he != NULL && he->name != NULL) {
@@ -3672,8 +3672,8 @@ static ComposeInsertResult compose_insert_file(Compose *compose, const gchar *fi
        }
 
 
-       if ((fp = g_fopen(file, "rb")) == NULL) {
-               FILE_OP_ERROR(file, "fopen");
+       if ((fp = claws_fopen(file, "rb")) == NULL) {
+               FILE_OP_ERROR(file, "claws_fopen");
                return COMPOSE_INSERT_READ_ERROR;
        }
 
@@ -3692,7 +3692,7 @@ static ComposeInsertResult compose_insert_file(Compose *compose, const gchar *fi
        cur_encoding = conv_get_locale_charset_str_no_utf8();
 
        file_contents = g_string_new("");
-       while (fgets(buf, sizeof(buf), fp) != NULL) {
+       while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
                gchar *str;
 
                if (g_utf8_validate(buf, -1, NULL) == TRUE)
@@ -3736,7 +3736,7 @@ static ComposeInsertResult compose_insert_file(Compose *compose, const gchar *fi
        }
 
        g_string_free(file_contents, TRUE);
-       fclose(fp);
+       claws_fclose(fp);
 
        return result;
 }
@@ -3788,11 +3788,11 @@ static gboolean compose_attach_append(Compose *compose, const gchar *file,
                        return FALSE;
                }
        }
-       if ((fp = g_fopen(file, "rb")) == NULL) {
+       if ((fp = claws_fopen(file, "rb")) == NULL) {
                alertpanel_error(_("Can't read %s."), filename);
                return FALSE;
        }
-       fclose(fp);
+       claws_fclose(fp);
 
        ainfo = g_new0(AttachInfo, 1);
        auto_ainfo = g_auto_pointer_new_with_free
@@ -5532,7 +5532,7 @@ static gint compose_redirect_write_headers(Compose *compose, FILE *fp)
                return -1;
 
        /* separator between header and body */
-       err |= (fputs("\n", fp) == EOF);
+       err |= (claws_fputs("\n", fp) == EOF);
 
        return (err ? -1:0);
 }
@@ -5565,8 +5565,8 @@ static gint compose_redirect_write_to_file(Compose *compose, FILE *fdest)
                };
        gint ret = 0;
 
-       if ((fp = g_fopen(compose->redirect_filename, "rb")) == NULL) {
-               FILE_OP_ERROR(compose->redirect_filename, "fopen");
+       if ((fp = claws_fopen(compose->redirect_filename, "rb")) == NULL) {
+               FILE_OP_ERROR(compose->redirect_filename, "claws_fopen");
                return -1;
        }
 
@@ -5584,7 +5584,7 @@ static gint compose_redirect_write_to_file(Compose *compose, FILE *fdest)
                        buf = NULL;
                        continue;
                }
-               if (fputs(buf, fdest) == -1) {
+               if (claws_fputs(buf, fdest) == -1) {
                        g_free(buf);
                        buf = NULL;
                        goto error;
@@ -5593,7 +5593,7 @@ static gint compose_redirect_write_to_file(Compose *compose, FILE *fdest)
                if (!prefs_common.redirect_keep_from) {
                        if (g_ascii_strncasecmp(buf, "From:",
                                          strlen("From:")) == 0) {
-                               err |= (fputs(" (by way of ", fdest) == EOF);
+                               err |= (claws_fputs(" (by way of ", fdest) == EOF);
                                if (compose->account->name
                                    && *compose->account->name) {
                                        gchar buffer[BUFFSIZE];
@@ -5609,13 +5609,13 @@ static gint compose_redirect_write_to_file(Compose *compose, FILE *fdest)
                                } else
                                        err |= (fprintf(fdest, "%s",
                                                compose->account->address) < 0);
-                               err |= (fputs(")", fdest) == EOF);
+                               err |= (claws_fputs(")", fdest) == EOF);
                        }
                }
 
                g_free(buf);
                buf = NULL;
-               if (fputs("\n", fdest) == -1)
+               if (claws_fputs("\n", fdest) == -1)
                        goto error;
        }
 
@@ -5625,17 +5625,17 @@ static gint compose_redirect_write_to_file(Compose *compose, FILE *fdest)
        if (compose_redirect_write_headers(compose, fdest))
                goto error;
 
-       while ((len = fread(rewrite_buf, sizeof(gchar), sizeof(rewrite_buf), fp)) > 0) {
-               if (fwrite(rewrite_buf, sizeof(gchar), len, fdest) != len)
+       while ((len = claws_fread(rewrite_buf, sizeof(gchar), sizeof(rewrite_buf), fp)) > 0) {
+               if (claws_fwrite(rewrite_buf, sizeof(gchar), len, fdest) != len)
                        goto error;
        }
 
-       fclose(fp);
+       claws_fclose(fp);
 
        return 0;
 
 error:
-       fclose(fp);
+       claws_fclose(fp);
 
        return -1;
 }
@@ -5897,20 +5897,20 @@ static gint compose_write_to_file(Compose *compose, FILE *fp, gint action, gbool
                                debug_print("saving sent message unencrypted...\n");
                                FILE *tmpfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmp_enc_file);
                                if (tmpfp) {
-                                       fclose(tmpfp);
+                                       claws_fclose(tmpfp);
 
                                        /* fp now points to a file with headers written,
                                         * let's make a copy. */
                                        rewind(fp);
                                        content = file_read_stream_to_str(fp);
 
-                                       str_write_to_file(content, tmp_enc_file);
+                                       str_write_to_file(content, tmp_enc_file, TRUE);
                                        g_free(content);
 
                                        /* Now write the unencrypted body. */
-                                       if ((tmpfp = g_fopen(tmp_enc_file, "a")) != NULL) {
+                                       if ((tmpfp = claws_fopen(tmp_enc_file, "a")) != NULL) {
                                                procmime_write_mimeinfo(mimemsg, tmpfp);
-                                               fclose(tmpfp);
+                                               claws_fclose(tmpfp);
 
                                                outbox = folder_find_item_from_identifier(compose_get_save_to(compose));
                                                if (!outbox)
@@ -5948,8 +5948,8 @@ static gint compose_write_body_to_file(Compose *compose, const gchar *file)
        size_t len;
        gchar *chars, *tmp;
 
-       if ((fp = g_fopen(file, "wb")) == NULL) {
-               FILE_OP_ERROR(file, "fopen");
+       if ((fp = claws_fopen(file, "wb")) == NULL) {
+               FILE_OP_ERROR(file, "claws_fopen");
                return -1;
        }
 
@@ -5969,24 +5969,24 @@ static gint compose_write_body_to_file(Compose *compose, const gchar *file)
 
        g_free(tmp);
        if (!chars) {
-               fclose(fp);
+               claws_fclose(fp);
                claws_unlink(file);
                return -1;
        }
        /* write body */
        len = strlen(chars);
-       if (fwrite(chars, sizeof(gchar), len, fp) != len) {
-               FILE_OP_ERROR(file, "fwrite");
+       if (claws_fwrite(chars, sizeof(gchar), len, fp) != len) {
+               FILE_OP_ERROR(file, "claws_fwrite");
                g_free(chars);
-               fclose(fp);
+               claws_fclose(fp);
                claws_unlink(file);
                return -1;
        }
 
        g_free(chars);
 
-       if (safe_fclose(fp) == EOF) {
-               FILE_OP_ERROR(file, "fclose");
+       if (claws_safe_fclose(fp) == EOF) {
+               FILE_OP_ERROR(file, "claws_fclose");
                claws_unlink(file);
                return -1;
        }
@@ -6116,8 +6116,8 @@ static ComposeQueueResult compose_queue_sub(Compose *compose, gint *msgnum, Fold
        tmp = g_strdup_printf("%s%cqueue.%p%08x", get_tmp_dir(),
                              G_DIR_SEPARATOR, compose, (guint) rand());
        debug_print("queuing to %s\n", tmp);
-       if ((fp = g_fopen(tmp, "w+b")) == NULL) {
-               FILE_OP_ERROR(tmp, "fopen");
+       if ((fp = claws_fopen(tmp, "w+b")) == NULL) {
+               FILE_OP_ERROR(tmp, "claws_fopen");
                g_free(tmp);
                return COMPOSE_QUEUE_ERROR_WITH_ERRNO;
        }
@@ -6179,7 +6179,7 @@ static ComposeQueueResult compose_queue_sub(Compose *compose, gint *msgnum, Fold
                err |= (fprintf(fp, "X-Claws-Sign:%d\n", compose->use_signing) < 0);
                if (compose->use_encryption) {
                        if (!compose_warn_encryption(compose)) {
-                               fclose(fp);
+                               claws_fclose(fp);
                                claws_unlink(tmp);
                                g_free(tmp);
                                return COMPOSE_QUEUE_ERROR_NO_MSG;
@@ -6204,7 +6204,7 @@ static ComposeQueueResult compose_queue_sub(Compose *compose, gint *msgnum, Fold
                                 * key selection */
                                if (err == TRUE)
                                        g_warning("failed to write queue message");
-                               fclose(fp);
+                               claws_fclose(fp);
                                claws_unlink(tmp);
                                g_free(tmp);
                                return COMPOSE_QUEUE_ERROR_NO_ENCRYPTION_KEY;
@@ -6257,7 +6257,7 @@ static ComposeQueueResult compose_queue_sub(Compose *compose, gint *msgnum, Fold
 
        if (compose->redirect_filename != NULL) {
                if (compose_redirect_write_to_file(compose, fp) < 0) {
-                       fclose(fp);
+                       claws_fclose(fp);
                        claws_unlink(tmp);
                        g_free(tmp);
                        return COMPOSE_QUEUE_ERROR_WITH_ERRNO;
@@ -6265,7 +6265,7 @@ static ComposeQueueResult compose_queue_sub(Compose *compose, gint *msgnum, Fold
        } else {
                gint result = 0;
                if ((result = compose_write_to_file(compose, fp, COMPOSE_WRITE_FOR_SEND, TRUE)) < 0) {
-                       fclose(fp);
+                       claws_fclose(fp);
                        claws_unlink(tmp);
                        g_free(tmp);
                        return result;
@@ -6273,13 +6273,13 @@ static ComposeQueueResult compose_queue_sub(Compose *compose, gint *msgnum, Fold
        }
        if (err == TRUE) {
                g_warning("failed to write queue message");
-               fclose(fp);
+               claws_fclose(fp);
                claws_unlink(tmp);
                g_free(tmp);
                return COMPOSE_QUEUE_ERROR_WITH_ERRNO;
        }
-       if (safe_fclose(fp) == EOF) {
-               FILE_OP_ERROR(tmp, "fclose");
+       if (claws_safe_fclose(fp) == EOF) {
+               FILE_OP_ERROR(tmp, "claws_fclose");
                claws_unlink(tmp);
                g_free(tmp);
                return COMPOSE_QUEUE_ERROR_WITH_ERRNO;
@@ -7050,11 +7050,11 @@ void compose_add_extra_header_entries(GtkListStore *model)
 
        if (extra_headers == NULL) {
                exhrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "extraheaderrc", NULL);
-               if ((exh = g_fopen(exhrc, "rb")) == NULL) {
+               if ((exh = claws_fopen(exhrc, "rb")) == NULL) {
                        debug_print("extra headers file not found\n");
                        goto extra_headers_done;
                }
-               while (fgets(buf, BUFFSIZE, exh) != NULL) {
+               while (claws_fgets(buf, BUFFSIZE, exh) != NULL) {
                        lastc = strlen(buf) - 1;        /* remove trailing control chars */
                        while (lastc >= 0 && buf[lastc] != ':')
                                buf[lastc--] = '\0';
@@ -7072,7 +7072,7 @@ void compose_add_extra_header_entries(GtkListStore *model)
                                        g_message("invalid extra header line: %s\n", buf);
                        }
                }
-               fclose(exh);
+               claws_fclose(exh);
 extra_headers_done:
                g_free(exhrc);
                extra_headers = g_slist_prepend(extra_headers, g_strdup("")); /* end of list */
@@ -10260,12 +10260,12 @@ static void compose_register_draft(MsgInfo *info)
 {
        gchar *filepath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
                                      DRAFTED_AT_EXIT, NULL);
-       FILE *fp = g_fopen(filepath, "ab");
+       FILE *fp = claws_fopen(filepath, "ab");
        
        if (fp) {
                fprintf(fp, "%s\t%d\n", folder_item_get_identifier(info->folder), 
                                info->msgnum);
-               fclose(fp);
+               claws_fclose(fp);
        }
                
        g_free(filepath);       
@@ -10306,8 +10306,8 @@ gboolean compose_draft (gpointer data, guint action)
 
        tmp = g_strdup_printf("%s%cdraft.%p", get_tmp_dir(),
                              G_DIR_SEPARATOR, compose);
-       if ((fp = g_fopen(tmp, "wb")) == NULL) {
-               FILE_OP_ERROR(tmp, "fopen");
+       if ((fp = claws_fopen(tmp, "wb")) == NULL) {
+               FILE_OP_ERROR(tmp, "claws_fopen");
                goto warn_err;
        }
 
@@ -10373,15 +10373,15 @@ gboolean compose_draft (gpointer data, guint action)
        err |= (fprintf(fp, "X-Claws-End-Special-Headers: 1\n") < 0);
 
        if (err) {
-               fclose(fp);
+               claws_fclose(fp);
                goto warn_err;
        }
 
        if (compose_write_to_file(compose, fp, COMPOSE_WRITE_FOR_STORE, action != COMPOSE_AUTO_SAVE) < 0) {
-               fclose(fp);
+               claws_fclose(fp);
                goto warn_err;
        }
-       if (safe_fclose(fp) == EOF) {
+       if (claws_safe_fclose(fp) == EOF) {
                goto warn_err;
        }
        
@@ -10551,11 +10551,11 @@ void compose_reopen_exit_drafts(void)
 {
        gchar *filepath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
                                      DRAFTED_AT_EXIT, NULL);
-       FILE *fp = g_fopen(filepath, "rb");
+       FILE *fp = claws_fopen(filepath, "rb");
        gchar buf[1024];
        
        if (fp) {
-               while (fgets(buf, sizeof(buf), fp)) {
+               while (claws_fgets(buf, sizeof(buf), fp)) {
                        gchar **parts = g_strsplit(buf, "\t", 2);
                        const gchar *folder = parts[0];
                        int msgnum = parts[1] ? atoi(parts[1]):-1;
@@ -10568,7 +10568,7 @@ void compose_reopen_exit_drafts(void)
                        }
                        g_strfreev(parts);
                }       
-               fclose(fp);
+               claws_fclose(fp);
        }       
        g_free(filepath);
        compose_clear_exit_drafts();
@@ -11674,7 +11674,7 @@ static void compose_insert_drag_received_cb (GtkWidget          *widget,
                        /* Assume a list of no files, and data has ://, is a remote link */
                        gchar *tmpdata = g_strstrip(g_strdup(ddata));
                        gchar *tmpfile = get_tmp_file();
-                       str_write_to_file(tmpdata, tmpfile);
+                       str_write_to_file(tmpdata, tmpfile, TRUE);
                        g_free(tmpdata);  
                        compose_insert_file(compose, tmpfile);
                        claws_unlink(tmpfile);
index ae1d4b1..edac5c7 100644 (file)
@@ -290,7 +290,7 @@ static void crash_create_debugger_file(void)
 {
        gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
        
-       str_write_to_file(DEBUG_SCRIPT, filespec);
+       str_write_to_file(DEBUG_SCRIPT, filespec, TRUE);
        g_free(filespec);
 }
 
@@ -310,7 +310,7 @@ static void crash_save_crash_log(GtkButton *button, const gchar *text)
        strftime(buf, sizeof buf, "claws-crash-log-%Y-%m-%d-%H-%M-%S.txt", lt);
        if (NULL != (filename = filesel_select_file_save(_("Save crash information"), buf))
        &&  *filename)
-               str_write_to_file(text, filename);
+               str_write_to_file(text, filename, TRUE);
        g_free(filename);       
 }
 
index ef50ca8..8fcdce6 100644 (file)
@@ -44,6 +44,7 @@
 #include "prefs_common.h"
 #include "menu.h"
 #include "combobox.h"
+#include "file-utils.h"
 
 /* transient data */
 static struct _PersonEdit_dlg personeditdlg;
index e149047..c29affa 100644 (file)
  * 
  */
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#include "claws-features.h"
+#endif
+
 #include <glib.h>
 #include <stdio.h>
 #include <string.h>
@@ -24,6 +29,7 @@
 
 #include "enriched.h"
 #include "utils.h"
+#include "file-utils.h"
 
 #define ERTFBUFSIZE    8192
 
@@ -114,7 +120,7 @@ static ERTFState ertf_read_line(ERTFParser *parser)
        gchar buf2[ERTFBUFSIZE];
        gint index;
 
-       if (fgets(buf, sizeof(buf), parser->fp) == NULL) {
+       if (claws_fgets(buf, sizeof(buf), parser->fp) == NULL) {
                parser->state = ERTF_EOF;
                return ERTF_EOF;
        }
index 48823e1..c6dd801 100644 (file)
@@ -44,7 +44,7 @@
 #include "utils.h"
 #include "mainwindow.h"
 #include "proxy.h"
-#include "claws_io.h"
+#include "file-utils.h"
 #include "ssl.h"
 #include "ssl_certificate.h"
 #include "socket.h"
@@ -2626,27 +2626,27 @@ static void fetch_content_run(struct etpan_thread_op * op)
                        goto free;
                }
                
-               f = fdopen(fd, "wb");
+               f = claws_fdopen(fd, "wb");
                if (f == NULL) {
                        result->error = MAILIMAP_ERROR_FETCH;
                        goto close;
                }
                
-               r = fwrite(content, 1, content_size, f);
+               r = claws_fwrite(content, 1, content_size, f);
                if (r < content_size) {
                        result->error = MAILIMAP_ERROR_FETCH;
-                       goto fclose;
+                       goto do_fclose;
                }
                
-               r = safe_fclose(f);
+               r = claws_safe_fclose(f);
                if (r == EOF) {
                        result->error = MAILIMAP_ERROR_FETCH;
                        goto unlink;
                }
                goto free;
                
-       fclose:
-               fclose(f);
+       do_fclose:
+               claws_fclose(f);
                goto unlink;
        close:
                close(fd);
index d0bfefd..86bfdfe 100644 (file)
@@ -46,7 +46,7 @@
 #include "utils.h"
 #include "exporthtml.h"
 #include "xmlprops.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #ifdef MKDIR_TAKES_ONE_ARG
 #undef mkdir
@@ -256,12 +256,12 @@ void exporthtml_set_attributes( ExportHtmlCtl *ctl, const gboolean value ) {
 static gint exporthtml_create_css_dfl( const gchar *fileSpec ) {
        FILE *cssFile;
 
-       cssFile = g_fopen( fileSpec, "rb" );
+       cssFile = claws_fopen( fileSpec, "rb" );
        if( cssFile ) {
-               fclose( cssFile );
+               claws_fclose( cssFile );
                return MGU_SUCCESS;
        }
-       cssFile = g_fopen( fileSpec, "wb" );
+       cssFile = claws_fopen( fileSpec, "wb" );
        if( ! cssFile ) {
                return MGU_OPEN_FILE;
        }
@@ -302,7 +302,7 @@ static gint exporthtml_create_css_dfl( const gchar *fileSpec ) {
        fprintf( cssFile, ".tab-attr {\n" );
        fprintf( cssFile, "}\n" );
 
-       safe_fclose( cssFile );
+       claws_safe_fclose( cssFile );
        return MGU_SUCCESS;
 }
 
@@ -314,12 +314,12 @@ static gint exporthtml_create_css_dfl( const gchar *fileSpec ) {
 static gint exporthtml_create_css_full( const gchar *fileSpec ) {
        FILE *cssFile;
 
-       cssFile = g_fopen( fileSpec, "rb" );
+       cssFile = claws_fopen( fileSpec, "rb" );
        if( cssFile ) {
-               fclose( cssFile );
+               claws_fclose( cssFile );
                return MGU_SUCCESS;
        }
-       cssFile = g_fopen( fileSpec, "wb" );
+       cssFile = claws_fopen( fileSpec, "wb" );
        if( ! cssFile ) {
                return MGU_OPEN_FILE;
        }
@@ -366,7 +366,7 @@ static gint exporthtml_create_css_full( const gchar *fileSpec ) {
        fprintf( cssFile, ".tab-attr {\n" );
        fprintf( cssFile, "}\n" );
 
-       safe_fclose( cssFile );
+       claws_safe_fclose( cssFile );
        return MGU_SUCCESS;
 }
 
@@ -964,7 +964,7 @@ void exporthtml_process(
        static gchar *title;
        gchar buf[512];
 
-       htmlFile = g_fopen( ctl->path, "wb" );
+       htmlFile = claws_fopen( ctl->path, "wb" );
        if( ! htmlFile ) {
                /* Cannot open file */
                g_print( "Cannot open file for write\n" );
@@ -995,7 +995,7 @@ void exporthtml_process(
        fprintf( htmlFile, "</body>\n" );
        fprintf( htmlFile, "</html>\n" );
 
-       safe_fclose( htmlFile );
+       claws_safe_fclose( htmlFile );
        ctl->retVal = MGU_SUCCESS;
 
        /* Create stylesheet files */
index ded6a3e..c35d6de 100644 (file)
@@ -38,7 +38,7 @@
 #include "exportldif.h"
 #include "xmlprops.h"
 #include "ldif.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 
 #ifdef MKDIR_TAKES_ONE_ARG
@@ -533,7 +533,7 @@ void exportldif_process( ExportLdifCtl *ctl, AddressCache *cache )
        ItemFolder *rootFolder;
        FILE *ldifFile;
 
-       ldifFile = g_fopen( ctl->path, "wb" );
+       ldifFile = claws_fopen( ctl->path, "wb" );
        if( ! ldifFile ) {
                /* Cannot open file */
                ctl->retVal = MGU_OPEN_FILE;
@@ -542,7 +542,7 @@ void exportldif_process( ExportLdifCtl *ctl, AddressCache *cache )
 
        rootFolder = cache->rootFolder;
        exportldif_fmt_folder( ctl, ldifFile, rootFolder );
-       safe_fclose( ldifFile );
+       claws_safe_fclose( ldifFile );
        ctl->retVal = MGU_SUCCESS;
 }
 
index d458a83..ccf8d54 100644 (file)
@@ -35,6 +35,7 @@
 #include <string.h>
 #include <ctype.h>
 
+#include "file-utils.h"
 #include "utils.h"
 #include "alertpanel.h"
 #include "folder.h"
index 503a049..abe8072 100644 (file)
@@ -63,6 +63,7 @@
 #include "privacy.h"
 #include "prefs_common.h"
 #include "prefs_migration.h"
+#include "file-utils.h"
 
 /* Dependecies to be removed ?! */
 #include "prefs_account.h"
@@ -2990,12 +2991,12 @@ static gint folder_item_get_msg_num_by_file(FolderItem *dest, const gchar *file)
        gint msgnum = 0;
        gchar buf[BUFFSIZE];
 
-       if ((fp = g_fopen(file, "rb")) == NULL)
+       if ((fp = claws_fopen(file, "rb")) == NULL)
                return 0;
 
        if ((folder_has_parent_of_type(dest, F_QUEUE)) || 
            (folder_has_parent_of_type(dest, F_DRAFT)))
-               while (fgets(buf, sizeof(buf), fp) != NULL) {
+               while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
                        /* new way */
                        if ((!strncmp(buf, "X-Claws-End-Special-Headers: 1",
                                strlen("X-Claws-End-Special-Headers:"))) ||
@@ -3029,7 +3030,7 @@ static gint folder_item_get_msg_num_by_file(FolderItem *dest, const gchar *file)
        
        g_free(hentry[0].body);
        hentry[0].body = NULL;
-       fclose(fp);
+       claws_fclose(fp);
 
        return msgnum;
 }
index f5893e1..d442387 100644 (file)
@@ -43,6 +43,7 @@
 #include "menu.h"
 #include "textview.h"
 #include "main.h"
+#include "file-utils.h"
 
 extern SessionStats session_stats;
 static GtkTextBuffer *stats_text_buffer;
@@ -638,14 +639,14 @@ static GtkWidget *about_create_child_page_release_notes(void)
        gtk_text_buffer_get_iter_at_offset(buffer, &iter, 0);
 
        path = g_strconcat(DOCDIR, G_DIR_SEPARATOR_S, RELEASE_NOTES_FILE, NULL);
-       if ((fp = g_fopen(path, "rb")) == NULL) {
-               if (ENOENT != errno) FILE_OP_ERROR(path, "fopen");
+       if ((fp = claws_fopen(path, "rb")) == NULL) {
+               if (ENOENT != errno) FILE_OP_ERROR(path, "claws_fopen");
                g_free(path);
                return scrolledwin;
        }
        g_free(path);
 
-       while (fgets(buf, sizeof(buf), fp) != NULL) {
+       while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
                const gchar *src_codeset = conv_get_locale_charset_str();
                const gchar *dest_codeset = CS_UTF_8;
                gchar *tmp;
@@ -659,7 +660,7 @@ static GtkWidget *about_create_child_page_release_notes(void)
                gtk_text_buffer_insert(buffer, &iter, tmp, -1);
                g_free(tmp);
        }
-       fclose(fp);
+       claws_fclose(fp);
 
        return scrolledwin;
 }
index da8625e..b702404 100644 (file)
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#include "claws-features.h"
+#endif
+
 #include <glib.h>
 #include <stdio.h>
 #include <string.h>
@@ -25,6 +30,7 @@
 #include "codeconv.h"
 #include "utils.h"
 #include "entity.h"
+#include "file-utils.h"
 
 #define SC_HTMLBUFSIZE 8192
 #define HR_STR         "────────────────────────────────────────────────"
@@ -136,7 +142,7 @@ static SC_HTMLState sc_html_read_line(SC_HTMLParser *parser)
        if (parser->fp == NULL)
                return SC_HTML_EOF;
 
-       n = fread(buf, 1, sizeof(buf) - 1, parser->fp);
+       n = claws_fread(buf, 1, sizeof(buf) - 1, parser->fp);
        if (n == 0) {
                parser->state = SC_HTML_EOF;
                return SC_HTML_EOF;
index d56e2c1..09fd79c 100644 (file)
@@ -27,6 +27,7 @@
 #include <gtk/gtk.h>
 
 #include "procmime.h"
+#include "file-utils.h"
 #include "utils.h"
 #include "mimeview.h"
 
index 4982b61..1e98aac 100644 (file)
@@ -71,6 +71,7 @@
 #include "tags.h"
 #include "main.h"
 #include "passwordstore.h"
+#include "file-utils.h"
 
 typedef struct _IMAPFolder     IMAPFolder;
 typedef struct _IMAPSession    IMAPSession;
@@ -1377,17 +1378,17 @@ static guint get_file_size_with_crs(const gchar *filename)
        if (filename == NULL)
                return -1;
        
-       fp = g_fopen(filename, "rb");
+       fp = claws_fopen(filename, "rb");
        if (!fp)
                return -1;
        
-       while (fgets(buf, sizeof (buf), fp) != NULL) {
+       while (claws_fgets(buf, sizeof (buf), fp) != NULL) {
                cnt += strlen(buf);
                if (!strstr(buf, "\r\n") && strstr(buf, "\n"))
                        cnt++;
        }
        
-       fclose(fp);
+       claws_fclose(fp);
        return cnt;
 }
 
index ed681fb..78fc240 100644 (file)
--- a/src/inc.c
+++ b/src/inc.c
@@ -45,6 +45,7 @@
 #include "pop.h"
 #include "recv.h"
 #include "mbox.h"
+#include "file-utils.h"
 #include "utils.h"
 #include "gtkutils.h"
 #include "statusbar.h"
index fc3927c..7b37c37 100644 (file)
@@ -56,6 +56,7 @@
 #include "codeconv.h"
 #include "adbookbase.h"
 #include "utils.h"
+#include "file-utils.h"
 
 #define JPILOT_DBHOME_DIR   ".jpilot"
 #define JPILOT_DBHOME_FILE  "AddressDB.pdb"
@@ -538,10 +539,10 @@ static int jpilot_get_info_size( FILE *in, int *size ) {
        int r;
 
        fseek(in, 0, SEEK_SET);
-       r = fread(&rdbh, sizeof(RawDBHeader), 1, in);
+       r = claws_fread(&rdbh, sizeof(RawDBHeader), 1, in);
        if (r < 1)
                return MGU_ERROR_READ;
-       if (feof(in)) {
+       if (claws_feof(in)) {
                return MGU_EOF;
        }
 
@@ -560,7 +561,7 @@ static int jpilot_get_info_size( FILE *in, int *size ) {
                return MGU_SUCCESS;
        }
 
-       r = fread(&rh, sizeof(record_header), 1, in);
+       r = claws_fread(&rh, sizeof(record_header), 1, in);
        if (r < 1)
                return MGU_ERROR_READ;
 
@@ -589,7 +590,7 @@ static gint jpilot_get_file_info( JPilotFile *pilotFile, unsigned char **buf, in
        *buf_size=0;
 
        if( pilotFile->path ) {
-               in = g_fopen( pilotFile->path, "rb" );
+               in = claws_fopen( pilotFile->path, "rb" );
                if( !in ) {
                        return MGU_OPEN_FILE;
                }
@@ -598,15 +599,15 @@ static gint jpilot_get_file_info( JPilotFile *pilotFile, unsigned char **buf, in
                return MGU_NO_FILE;
        }
 
-       num = fread( &rdbh, sizeof( RawDBHeader ), 1, in );
+       num = claws_fread( &rdbh, sizeof( RawDBHeader ), 1, in );
        if( num != 1 ) {
-               if( ferror(in) ) {
-                       fclose(in);
+               if( claws_ferror(in) ) {
+                       claws_fclose(in);
                        return MGU_ERROR_READ;
                }
        }
-       if (feof(in)) {
-               fclose(in);
+       if (claws_feof(in)) {
+               claws_fclose(in);
                return MGU_EOF;
        }
 
@@ -615,28 +616,28 @@ static gint jpilot_get_file_info( JPilotFile *pilotFile, unsigned char **buf, in
 
        num = jpilot_get_info_size(in, &rec_size);
        if (num) {
-               fclose(in);
+               claws_fclose(in);
                return MGU_ERROR_READ;
        }
 
        if (fseek(in, dbh.app_info_offset, SEEK_SET) < 0) {
-               fclose(in);
+               claws_fclose(in);
                return MGU_ERROR_READ;
        }
        *buf = ( char * ) malloc(rec_size);
        if (!(*buf)) {
-               fclose(in);
+               claws_fclose(in);
                return MGU_OO_MEMORY;
        }
-       num = fread(*buf, rec_size, 1, in);
+       num = claws_fread(*buf, rec_size, 1, in);
        if (num != 1) {
-               if (ferror(in)) {
-                       fclose(in);
+               if (claws_ferror(in)) {
+                       claws_fclose(in);
                        free(*buf);
                        return MGU_ERROR_READ;
                }
        }
-       fclose(in);
+       claws_fclose(in);
 
        *buf_size = rec_size;
 
@@ -685,8 +686,8 @@ static int read_header(FILE *pc_in, PC3RecordHeader *header) {
 
        memset(header, 0, sizeof(PC3RecordHeader));
 
-       num = fread(&l, sizeof(l), 1, pc_in);
-       if (feof(pc_in)) {
+       num = claws_fread(&l, sizeof(l), 1, pc_in);
+       if (claws_feof(pc_in)) {
                return -1;
        }
        if (num!=1) {
@@ -697,8 +698,8 @@ static int read_header(FILE *pc_in, PC3RecordHeader *header) {
        if (len > 255 || len < sizeof(l)) {
                return -1;
        }
-       num = fread(packed_header+sizeof(l), len-sizeof(l), 1, pc_in);
-       if (feof(pc_in)) {
+       num = claws_fread(packed_header+sizeof(l), len-sizeof(l), 1, pc_in);
+       if (claws_feof(pc_in)) {
                return -1;
        }
        if (num!=1) {
@@ -722,14 +723,14 @@ static gint jpilot_read_next_pc( FILE *in, buf_rec *br ) {
        int rec_len, num;
        char *record;
 
-       if( feof( in ) ) {
+       if( claws_feof( in ) ) {
                return MGU_EOF;
        }
        num = read_header( in, &header );
        if( num < 1 ) {
-               if( ferror( in ) )
+               if( claws_ferror( in ) )
                        return MGU_ERROR_READ;
-               else if( feof( in ) )
+               else if( claws_feof( in ) )
                        return MGU_EOF;
                else
                        return -1;
@@ -739,9 +740,9 @@ static gint jpilot_read_next_pc( FILE *in, buf_rec *br ) {
        if( ! record ) {
                return MGU_OO_MEMORY;
        }
-       num = fread( record, rec_len, 1, in );
+       num = claws_fread( record, rec_len, 1, in );
        if( num != 1 ) {
-               if( ferror( in ) ) {
+               if( claws_ferror( in ) ) {
                        free( record );
                        return MGU_ERROR_READ;
                }
@@ -789,20 +790,20 @@ static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
                return MGU_BAD_ARGS;
        }
 
-       in = g_fopen( pilotFile->path, "rb" );
+       in = claws_fopen( pilotFile->path, "rb" );
        if (!in) {
                return MGU_OPEN_FILE;
        }
 
        /* Read the database header */
-       num = fread( &rdbh, sizeof( RawDBHeader ), 1, in );
+       num = claws_fread( &rdbh, sizeof( RawDBHeader ), 1, in );
        if( num != 1 ) {
-               if( ferror( in ) ) {
-                       fclose( in );
+               if( claws_ferror( in ) ) {
+                       claws_fclose( in );
                        return MGU_ERROR_READ;
                }
-               if( feof( in ) ) {
-                       fclose( in );
+               if( claws_feof( in ) ) {
+                       claws_fclose( in );
                        return MGU_EOF;
                }
        }
@@ -814,13 +815,13 @@ static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
        prev_offset = 0;
 
        for( i = 1; i < num_records + 1; i++ ) {
-               num = fread( &rh, sizeof( record_header ), 1, in );
+               num = claws_fread( &rh, sizeof( record_header ), 1, in );
                if( num != 1 ) {
-                       if( ferror( in ) ) {
+                       if( claws_ferror( in ) ) {
                                break;
                        }
-                       if( feof( in ) ) {
-                               fclose( in );
+                       if( claws_feof( in ) ) {
+                               claws_fclose( in );
                                if (mem_rh)
                                        free_mem_rec_header( &mem_rh );
                                return MGU_EOF;
@@ -872,10 +873,10 @@ static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
                }
                if (fseek( in, next_offset, SEEK_SET ) < 0) {
                        free_mem_rec_header( &mem_rh );
-                       fclose(in);
+                       claws_fclose(in);
                        return MGU_ERROR_READ;
                }
-               while( ! feof( in ) ) {
+               while( ! claws_feof( in ) ) {
                        fpos = ftell( in );
                        if( out_of_order ) {
                                find_next_offset(
@@ -895,9 +896,9 @@ static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
                        rec_size = next_offset - fpos;
                        buf = malloc( rec_size );
                        if( ! buf ) break;
-                       num = fread( buf, rec_size, 1, in );
+                       num = claws_fread( buf, rec_size, 1, in );
                        if( ( num != 1 ) ) {
-                               if( ferror( in ) ) {
+                               if( claws_ferror( in ) ) {
                                        free( buf );
                                        break;
                                }
@@ -919,20 +920,20 @@ static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
                        recs_returned++;
                }
        }
-       fclose( in );
+       claws_fclose( in );
        free_mem_rec_header( &mem_rh );
 
        /* Read the PC3 file, if present */
        pcFile = jpilot_get_pc3_file( pilotFile );
        if( pcFile == NULL ) return MGU_SUCCESS;
-       pc_in = g_fopen( pcFile, "rb");
+       pc_in = claws_fopen( pcFile, "rb");
        g_free( pcFile );
 
        if( pc_in == NULL ) {
                return MGU_SUCCESS;
        }
 
-       while( ! feof( pc_in ) ) {
+       while( ! claws_feof( pc_in ) ) {
                gboolean linked;
 
                temp_br = malloc( sizeof( buf_rec ) );
@@ -982,7 +983,7 @@ static gint jpilot_read_db_files( JPilotFile *pilotFile, GList **records ) {
                        free( temp_br );
                }
        }
-       fclose( pc_in );
+       claws_fclose( pc_in );
 
        return MGU_SUCCESS;
 }
@@ -1616,8 +1617,8 @@ gchar *jpilot_find_pilotdb( void ) {
        strncat( str, JPILOT_DBHOME_FILE, WORK_BUFLEN - strlen(str) );
 
        /* Attempt to open */
-       if( ( fp = g_fopen( str, "rb" ) ) != NULL ) {
-               fclose( fp );
+       if( ( fp = claws_fopen( str, "rb" ) ) != NULL ) {
+               claws_fclose( fp );
        }
        else {
                /* Truncate filename */
index 5b0750a..2e96831 100644 (file)
@@ -38,7 +38,7 @@
 #include "ldapctrl.h"
 #include "ldapserver.h"
 #include "mgutils.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #include "addritem.h"
 #include "addrcache.h"
@@ -424,10 +424,10 @@ static GSList *ldapqry_add_single_value( LDAP *ld, LDAPMessage *entry, char *att
                                list = g_slist_append( list, g_strndup( vals[0]->bv_val, vals[0]->bv_len ));
                        } else {
                                char *file = get_tmp_file();
-                               FILE *fp = g_fopen(file, "wb");
+                               FILE *fp = claws_fopen(file, "wb");
                                if (fp) {
-                                       fwrite(vals[0]->bv_val, 1, vals[0]->bv_len, fp);
-                                       safe_fclose(fp);
+                                       claws_fwrite(vals[0]->bv_val, 1, vals[0]->bv_len, fp);
+                                       claws_safe_fclose(fp);
                                }
                                list = g_slist_append( list, file);
                        }
index 93ce800..d2b19ef 100644 (file)
@@ -32,6 +32,7 @@
 #include "addrcache.h"
 
 #include "utils.h"
+#include "file-utils.h"
 
 #define        LDIF_SEP_TAG    ':'
 #define        LDIF_LANG_TAG   ';'
@@ -176,7 +177,7 @@ void ldif_free( LdifFile *ldifFile ) {
        cm_return_if_fail( ldifFile != NULL );
 
        /* Close file */
-       if( ldifFile->file ) fclose( ldifFile->file );
+       if( ldifFile->file ) claws_fclose( ldifFile->file );
 
        /* Free internal stuff */
        g_free( ldifFile->path );
@@ -207,7 +208,7 @@ void ldif_free( LdifFile *ldifFile ) {
 static gint ldif_open_file( LdifFile* ldifFile ) {
        /* g_print( "Opening file\n" ); */
        if( ldifFile->path ) {
-               ldifFile->file = g_fopen( ldifFile->path, "rb" );
+               ldifFile->file = claws_fopen( ldifFile->path, "rb" );
                if( ! ldifFile->file ) {
                        /* g_print( "can't open %s\n", ldifFile->path ); */
                        ldifFile->retVal = MGU_OPEN_FILE;
@@ -231,7 +232,7 @@ static gint ldif_open_file( LdifFile* ldifFile ) {
  */
 static void ldif_close_file( LdifFile *ldifFile ) {
        cm_return_if_fail( ldifFile != NULL );
-       if( ldifFile->file ) fclose( ldifFile->file );
+       if( ldifFile->file ) claws_fclose( ldifFile->file );
        ldifFile->file = NULL;
 }
 
@@ -246,14 +247,14 @@ static gchar *ldif_get_line( LdifFile *ldifFile ) {
        int i = 0;
        int cur_alloc = LDIFBUFSIZE;
 
-       if( feof( ldifFile->file ) ) {
+       if( claws_feof( ldifFile->file ) ) {
                g_free(buf);
                return NULL;
        }
 
        while( i < cur_alloc-1 ) {
                ch = fgetc( ldifFile->file );
-               if (ferror( ldifFile->file ))
+               if (claws_ferror( ldifFile->file ))
                        ldifFile->retVal = MGU_ERROR_READ;
                if( ch == '\0' || ch == EOF ) {
                        if( i == 0 ) return NULL;
index 0bfee71..0f17016 100644 (file)
 #include "advsearch.h"
 #include "avatars.h"
 #include "passwordstore.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #ifdef HAVE_LIBETPAN
 #include "imap-thread.h"
@@ -443,12 +443,12 @@ static int migrate_common_rc(const gchar *old_rc, const gchar *new_rc)
        gchar buf[BUFFSIZE];
        gboolean err = FALSE;
 
-       oldfp = g_fopen(old_rc, "r");
+       oldfp = claws_fopen(old_rc, "r");
        if (!oldfp)
                return -1;
-       newfp = g_fopen(new_rc, "w");
+       newfp = claws_fopen(new_rc, "w");
        if (!newfp) {
-               fclose(oldfp);
+               claws_fclose(oldfp);
                return -1;
        }
        
@@ -464,21 +464,21 @@ static int migrate_common_rc(const gchar *old_rc, const gchar *new_rc)
                old_plugin_path = g_strdup(new_plugin_path);
        }
        debug_print("replacing %s with %s\n", old_plugin_path, new_plugin_path);
-       while (fgets(buf, sizeof(buf), oldfp)) {
+       while (claws_fgets(buf, sizeof(buf), oldfp)) {
                if (strncmp(buf, old_plugin_path, strlen(old_plugin_path))) {
-                       err |= (fputs(buf, newfp) == EOF);
+                       err |= (claws_fputs(buf, newfp) == EOF);
                } else {
                        debug_print("->replacing %s\n", buf);
                        debug_print("  with %s%s\n", new_plugin_path, buf+strlen(old_plugin_path));
-                       err |= (fputs(new_plugin_path, newfp) == EOF);
-                       err |= (fputs(buf+strlen(old_plugin_path), newfp) == EOF);
+                       err |= (claws_fputs(new_plugin_path, newfp) == EOF);
+                       err |= (claws_fputs(buf+strlen(old_plugin_path), newfp) == EOF);
                }
        }
        g_free(plugin_path);
        g_free(new_plugin_path);
        g_free(old_plugin_path);
-       fclose(oldfp);
-       if (safe_fclose(newfp) == EOF)
+       claws_fclose(oldfp);
+       if (claws_safe_fclose(newfp) == EOF)
                err = TRUE;
        
        return (err ? -1:0);
@@ -738,7 +738,7 @@ static void win32_open_log(void)
                if (rename_force(logfile, oldlogfile) < 0)
                        FILE_OP_ERROR(logfile, "rename");
        }
-       win32_debug_fp = g_fopen(logfile, "w");
+       win32_debug_fp = claws_fopen(logfile, "w");
        g_free(logfile);
        g_free(oldlogfile);
        if (win32_debug_fp)
@@ -761,7 +761,7 @@ static void win32_close_log(void)
                g_log_remove_handler("", win32_log_handler_app_id);
                g_log_remove_handler("GLib", win32_log_handler_glib_id);
                g_log_remove_handler("Gtk", win32_log_handler_gtk_id);
-               fclose(win32_debug_fp);
+               claws_fclose(win32_debug_fp);
                win32_debug_fp=NULL;
        }
 }              
@@ -1440,7 +1440,7 @@ int main(int argc, char *argv[])
                folder_item_update_freeze();
        }
        /* make the crash-indicator file */
-       str_write_to_file("foo", get_crashfile_name());
+       str_write_to_file("foo", get_crashfile_name(), FALSE);
 
        inc_autocheck_timer_init(mainwin);
 
@@ -1788,12 +1788,12 @@ static GString * parse_cmd_compose_from_file(const gchar *fn)
        if (isstdin)
                fp = stdin;
        else {
-               fp = g_fopen(fn, "r");
+               fp = claws_fopen(fn, "r");
                if (!fp)
                        G_PRINT_EXIT(_("Cannot open filename for reading\n"));
        }
 
-       while (fgets(fb, sizeof(fb), fp)) {
+       while (claws_fgets(fb, sizeof(fb), fp)) {
                gchar *tmp;     
                strretchomp(fb);
                if (*fb == '\0')
@@ -1823,11 +1823,11 @@ static GString * parse_cmd_compose_from_file(const gchar *fn)
        g_string_append(body, to);
        g_free(to);
        g_string_append(body, "?body=");
-       while (fgets(fb, sizeof(fb), fp)) {
+       while (claws_fgets(fb, sizeof(fb), fp)) {
                g_string_append_uri_escaped(body, fb, NULL, TRUE);
        }
        if (!isstdin)
-               fclose(fp);
+               claws_fclose(fp);
        /* append the remaining headers */
        g_string_append(body, headers->str);
        g_string_free(headers, TRUE);
@@ -2415,7 +2415,7 @@ static gint prohibit_duplicate_launch(void)
                        fd_gets(uxsock, buf, sizeof(buf) - 1);
                        buf[sizeof(buf) - 1] = '\0';
                        if (!strncmp(buf, ".\n", 2)) break;
-                       fputs(buf, stdout);
+                       claws_fputs(buf, stdout);
                }
        } else if (cmd.exit) {
                fd_write_all(uxsock, "exit\n", 5);
@@ -2426,7 +2426,7 @@ static gint prohibit_duplicate_launch(void)
                        fd_gets(uxsock, buf, sizeof(buf) - 1);
                        buf[sizeof(buf) - 1] = '\0';
                        if (!strncmp(buf, ".\n", 2)) break;
-                       fputs(buf, stdout);
+                       claws_fputs(buf, stdout);
                }
        } else if (cmd.reset_statistics) {
                fd_write(uxsock, "reset_statistics\n", 17);
@@ -2446,7 +2446,7 @@ static gint prohibit_duplicate_launch(void)
                        fd_gets(uxsock, buf, sizeof(buf) - 1);
                        buf[sizeof(buf) - 1] = '\0';
                        if (!strncmp(buf, ".\n", 2)) break;
-                       fputs(buf, stdout);
+                       claws_fputs(buf, stdout);
                }
        } else {
 #ifndef G_OS_WIN32
index 5b67051..1186dba 100644 (file)
@@ -49,6 +49,7 @@
 #include "tags.h"
 #include "folder_item_prefs.h"
 #include "procmsg.h"
+#include "file-utils.h"
 
 /*!
  *\brief       Keyword lookup element
@@ -1623,7 +1624,7 @@ static gboolean matcherlist_match_binary_content(MatcherList *matchers, MimeInfo
        if (!outfp)
                return FALSE;
 
-       while (fgets(buf, sizeof(buf), outfp) != NULL) {
+       while (claws_fgets(buf, sizeof(buf), outfp) != NULL) {
                strretchomp(buf);
 
                for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
@@ -1664,14 +1665,14 @@ static gboolean matcherlist_match_binary_content(MatcherList *matchers, MimeInfo
                         * no need to check the others. */
                        if (matcher->result && matcher->done) {
                                if (!matchers->bool_and) {
-                                       fclose(outfp);
+                                       claws_fclose(outfp);
                                        return TRUE;
                                }
                        }
                }
        }
 
-       fclose(outfp);
+       claws_fclose(outfp);
        return FALSE;
 }
 
@@ -1823,8 +1824,8 @@ static gboolean matcherlist_match_file(MatcherList *matchers, MsgInfo *info,
        if (file == NULL)
                return FALSE;
 
-       if ((fp = g_fopen(file, "rb")) == NULL) {
-               FILE_OP_ERROR(file, "fopen");
+       if ((fp = claws_fopen(file, "rb")) == NULL) {
+               FILE_OP_ERROR(file, "claws_fopen");
                g_free(file);
                return result;
        }
@@ -1866,7 +1867,7 @@ static gboolean matcherlist_match_file(MatcherList *matchers, MsgInfo *info,
 
        g_free(file);
 
-       fclose(fp);
+       claws_fclose(fp);
        
        return result;
 }
@@ -2429,42 +2430,42 @@ static int prefs_filtering_write(FILE *fp, GSList *prefs_filtering)
                        continue;
 
                if (prop->enabled) {
-                       if (fputs("enabled ", fp) == EOF) {
-                               FILE_OP_ERROR("filtering config", "fputs");
+                       if (claws_fputs("enabled ", fp) == EOF) {
+                               FILE_OP_ERROR("filtering config", "claws_fputs");
                                return -1;
                        }
                } else {
-                       if (fputs("disabled ", fp) == EOF) {
-                               FILE_OP_ERROR("filtering config", "fputs");
+                       if (claws_fputs("disabled ", fp) == EOF) {
+                               FILE_OP_ERROR("filtering config", "claws_fputs");
                                return -1;
                        }
                }
 
-               if (fputs("rulename \"", fp) == EOF) {
-                       FILE_OP_ERROR("filtering config", "fputs");
+               if (claws_fputs("rulename \"", fp) == EOF) {
+                       FILE_OP_ERROR("filtering config", "claws_fputs");
                        g_free(filtering_str);
                        return -1;
                }
                tmp_name = prop->name;
                while (tmp_name && *tmp_name != '\0') {
                        if (*tmp_name != '"') {
-                               if (fputc(*tmp_name, fp) == EOF) {
-                                       FILE_OP_ERROR("filtering config", "fputs || fputc");
+                               if (claws_fputc(*tmp_name, fp) == EOF) {
+                                       FILE_OP_ERROR("filtering config", "claws_fputs || claws_fputc");
                                        g_free(filtering_str);
                                        return -1;
                                }
                        } else if (*tmp_name == '"') {
-                               if (fputc('\\', fp) == EOF ||
-                                   fputc('"', fp) == EOF) {
-                                       FILE_OP_ERROR("filtering config", "fputs || fputc");
+                               if (claws_fputc('\\', fp) == EOF ||
+                                   claws_fputc('"', fp) == EOF) {
+                                       FILE_OP_ERROR("filtering config", "claws_fputs || claws_fputc");
                                        g_free(filtering_str);
                                        return -1;
                                }
                        }
                        tmp_name ++;
                }
-               if (fputs("\" ", fp) == EOF) {
-                       FILE_OP_ERROR("filtering config", "fputs");
+               if (claws_fputs("\" ", fp) == EOF) {
+                       FILE_OP_ERROR("filtering config", "claws_fputs");
                        g_free(filtering_str);
                        return -1;
                }
@@ -2473,17 +2474,17 @@ static int prefs_filtering_write(FILE *fp, GSList *prefs_filtering)
                        gchar *tmp = NULL;
 
                        tmp = g_strdup_printf("account %d ", prop->account_id);
-                       if (fputs(tmp, fp) == EOF) {
-                               FILE_OP_ERROR("filtering config", "fputs");
+                       if (claws_fputs(tmp, fp) == EOF) {
+                               FILE_OP_ERROR("filtering config", "claws_fputs");
                                g_free(tmp);
                                return -1;
                        }
                        g_free(tmp);
                }
 
-               if(fputs(filtering_str, fp) == EOF ||
-                   fputc('\n', fp) == EOF) {
-                       FILE_OP_ERROR("filtering config", "fputs || fputc");
+               if(claws_fputs(filtering_str, fp) == EOF ||
+                   claws_fputc('\n', fp) == EOF) {
+                       FILE_OP_ERROR("filtering config", "claws_fputs || claws_fputc");
                        g_free(filtering_str);
                        return -1;
                }
@@ -2531,7 +2532,7 @@ static gboolean prefs_matcher_write_func(GNode *node, gpointer d)
                        data->error = TRUE;
                        goto fail;
                }
-               if (fputc('\n', data->fp) == EOF) {
+               if (claws_fputc('\n', data->fp) == EOF) {
                        data->error = TRUE;
                        goto fail;
                }
@@ -2569,19 +2570,19 @@ static int prefs_matcher_save(FILE *fp)
         /* pre global rules */
         if (fprintf(fp, "[preglobal]\n") < 0 ||
             prefs_filtering_write(fp, pre_global_processing) < 0 ||
-            fputc('\n', fp) == EOF)
+            claws_fputc('\n', fp) == EOF)
                return -1;
 
         /* post global rules */
         if (fprintf(fp, "[postglobal]\n") < 0 ||
             prefs_filtering_write(fp, post_global_processing) < 0 ||
-            fputc('\n', fp) == EOF)
+            claws_fputc('\n', fp) == EOF)
                return -1;
         
         /* filtering rules */
        if (fprintf(fp, "[filtering]\n") < 0 ||
             prefs_filtering_write(fp, filtering_rules) < 0 ||
-            fputc('\n', fp) == EOF)
+            claws_fputc('\n', fp) == EOF)
                return -1;
 
        return 0;
@@ -2629,11 +2630,11 @@ void prefs_matcher_read_config(void)
 
        rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, NULL);
 
-       f = g_fopen(rcpath, "rb");
+       f = claws_fopen(rcpath, "rb");
        g_free(rcpath);
 
        if (f != NULL) {
                matcher_parser_start_parsing(f);
-               fclose(matcher_parserin);
+               claws_fclose(matcher_parserin);
        }
 }
index 68b3d01..9047fbb 100644 (file)
@@ -23,7 +23,6 @@
 #endif
 
 
-#define _GNU_SOURCE
 #include <stdio.h>
 
 #ifdef USE_PTHREAD
 #include "filtering.h"
 #include "alertpanel.h"
 #include "statusbar.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #define MESSAGEBUFSIZE 8192
 
-#ifdef HAVE_FGETS_UNLOCKED
-#define SC_FGETS fgets_unlocked
-#define SC_FPUTS fputs_unlocked
-#define SC_FPUTC fputc_unlocked
-#else
-#define SC_FGETS fgets
-#define SC_FPUTS fputs
-#define SC_FPUTC fputc
-#endif
-
 #define FPUTS_TO_TMP_ABORT_IF_FAIL(s) \
 { \
        lines++; \
-       if (fputs(s, tmp_fp) == EOF) { \
+       if (claws_fputs(s, tmp_fp) == EOF) { \
                g_warning("can't write to temporary file"); \
-               fclose(tmp_fp); \
-               fclose(mbox_fp); \
+               claws_fclose(tmp_fp); \
+               claws_fclose(mbox_fp); \
                claws_unlink(tmp_file); \
                g_free(tmp_file); \
                return -1; \
@@ -103,24 +92,24 @@ gint proc_mbox(FolderItem *dest, const gchar *mbox, gboolean apply_filter,
 
        debug_print("Getting messages from %s into %s...\n", mbox, dest->path);
 
-       if ((mbox_fp = g_fopen(mbox, "rb")) == NULL) {
-               FILE_OP_ERROR(mbox, "fopen");
+       if ((mbox_fp = claws_fopen(mbox, "rb")) == NULL) {
+               FILE_OP_ERROR(mbox, "claws_fopen");
                alertpanel_error(_("Could not open mbox file:\n%s\n"), mbox);
                return -1;
        }
 
        /* ignore empty lines on the head */
        do {
-               if (fgets(buf, sizeof(buf), mbox_fp) == NULL) {
+               if (claws_fgets(buf, sizeof(buf), mbox_fp) == NULL) {
                        g_warning("can't read mbox file.");
-                       fclose(mbox_fp);
+                       claws_fclose(mbox_fp);
                        return -1;
                }
        } while (buf[0] == '\n' || buf[0] == '\r');
 
        if (strncmp(buf, "From ", 5) != 0) {
                g_warning("invalid mbox format: %s", mbox);
-               fclose(mbox_fp);
+               claws_fclose(mbox_fp);
                return -1;
        }
 
@@ -148,10 +137,10 @@ gint proc_mbox(FolderItem *dest, const gchar *mbox, gboolean apply_filter,
                        GTK_EVENTS_FLUSH();
                }
        
-               if ((tmp_fp = g_fopen(tmp_file, "wb")) == NULL) {
-                       FILE_OP_ERROR(tmp_file, "fopen");
+               if ((tmp_fp = claws_fopen(tmp_file, "wb")) == NULL) {
+                       FILE_OP_ERROR(tmp_file, "claws_fopen");
                        g_warning("can't open temporary file");
-                       fclose(mbox_fp);
+                       claws_fclose(mbox_fp);
                        g_free(tmp_file);
                        return -1;
                }
@@ -163,7 +152,7 @@ gint proc_mbox(FolderItem *dest, const gchar *mbox, gboolean apply_filter,
                lines = 0;
 
                /* process all lines from mboxrc file */
-               while (fgets(buf, sizeof(buf), mbox_fp) != NULL) {
+               while (claws_fgets(buf, sizeof(buf), mbox_fp) != NULL) {
                        int offset;
 
                        /* eat empty lines */
@@ -219,22 +208,22 @@ gint proc_mbox(FolderItem *dest, const gchar *mbox, gboolean apply_filter,
                }
 
                /* more emails to expect? */
-               more = !feof(mbox_fp);
+               more = !claws_feof(mbox_fp);
 
                /* warn if email part is empty (it's the minimum check 
                   we can do */
                if (lines == 0) {
                        g_warning("malformed mbox: %s: message %d is empty", mbox, msgs);
-                       fclose(tmp_fp);
-                       fclose(mbox_fp);
+                       claws_fclose(tmp_fp);
+                       claws_fclose(mbox_fp);
                        claws_unlink(tmp_file);
                        return -1;
                }
 
-               if (safe_fclose(tmp_fp) == EOF) {
-                       FILE_OP_ERROR(tmp_file, "fclose");
+               if (claws_safe_fclose(tmp_fp) == EOF) {
+                       FILE_OP_ERROR(tmp_file, "claws_fclose");
                        g_warning("can't write to temporary file");
-                       fclose(mbox_fp);
+                       claws_fclose(mbox_fp);
                        claws_unlink(tmp_file);
                        g_free(tmp_file);
                        return -1;
@@ -242,7 +231,7 @@ gint proc_mbox(FolderItem *dest, const gchar *mbox, gboolean apply_filter,
 
                if (apply_filter) {
                        if ((msgnum = folder_item_add_msg(dropfolder, tmp_file, NULL, TRUE)) < 0) {
-                               fclose(mbox_fp);
+                               claws_fclose(mbox_fp);
                                claws_unlink(tmp_file);
                                g_free(tmp_file);
                                return -1;
@@ -303,7 +292,7 @@ gint proc_mbox(FolderItem *dest, const gchar *mbox, gboolean apply_filter,
        folder_item_update_thaw();
        
        g_free(tmp_file);
-       fclose(mbox_fp);
+       claws_fclose(mbox_fp);
        debug_print("%d messages found.\n", msgs);
 
        return msgs;
@@ -320,8 +309,8 @@ gint lock_mbox(const gchar *base, LockType type)
                FILE *lockfp;
 
                lockfile = g_strdup_printf("%s.%d", base, getpid());
-               if ((lockfp = g_fopen(lockfile, "wb")) == NULL) {
-                       FILE_OP_ERROR(lockfile, "fopen");
+               if ((lockfp = claws_fopen(lockfile, "wb")) == NULL) {
+                       FILE_OP_ERROR(lockfile, "claws_fopen");
                        g_warning("can't create lock file '%s', use 'flock' instead of 'file' if possible.", lockfile);
                        g_free(lockfile);
                        return -1;
@@ -330,12 +319,12 @@ gint lock_mbox(const gchar *base, LockType type)
                if (fprintf(lockfp, "%d\n", getpid()) < 0) {
                        FILE_OP_ERROR(lockfile, "fprintf");
                        g_free(lockfile);
-                       fclose(lockfp);
+                       claws_fclose(lockfp);
                        return -1;
                }
 
-               if (safe_fclose(lockfp) == EOF) {
-                       FILE_OP_ERROR(lockfile, "fclose");
+               if (claws_safe_fclose(lockfp) == EOF) {
+                       FILE_OP_ERROR(lockfile, "claws_fclose");
                        g_free(lockfile);
                        return -1;
                }
@@ -485,8 +474,8 @@ gint copy_mbox(gint srcfd, const gchar *dest)
                return -1;
        }
 
-       if ((dest_fp = g_fopen(dest, "wb")) == NULL) {
-               FILE_OP_ERROR(dest, "fopen");
+       if ((dest_fp = claws_fopen(dest, "wb")) == NULL) {
+               FILE_OP_ERROR(dest, "claws_fopen");
                return -1;
        }
 
@@ -496,9 +485,9 @@ gint copy_mbox(gint srcfd, const gchar *dest)
        }
 
        while ((n_read = read(srcfd, buf, sizeof(buf))) > 0) {
-               if (fwrite(buf, 1, n_read, dest_fp) < n_read) {
+               if (claws_fwrite(buf, 1, n_read, dest_fp) < n_read) {
                        g_warning("writing to %s failed.", dest);
-                       fclose(dest_fp);
+                       claws_fclose(dest_fp);
                        claws_unlink(dest);
                        return -1;
                }
@@ -510,8 +499,8 @@ gint copy_mbox(gint srcfd, const gchar *dest)
                err = TRUE;
        }
 
-       if (safe_fclose(dest_fp) == EOF) {
-               FILE_OP_ERROR(dest, "fclose");
+       if (claws_safe_fclose(dest_fp) == EOF) {
+               FILE_OP_ERROR(dest, "claws_fclose");
                err = TRUE;
        }
 
@@ -527,12 +516,12 @@ void empty_mbox(const gchar *mbox)
 {
        FILE *fp;
 
-       if ((fp = g_fopen(mbox, "wb")) == NULL) {
-               FILE_OP_ERROR(mbox, "fopen");
+       if ((fp = claws_fopen(mbox, "wb")) == NULL) {
+               FILE_OP_ERROR(mbox, "claws_fopen");
                g_warning("can't truncate mailbox to zero.");
                return;
        }
-       safe_fclose(fp);
+       claws_safe_fclose(fp);
 }
 
 gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
@@ -556,16 +545,12 @@ gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
                }
        }
 
-       if ((mbox_fp = g_fopen(mbox, "wb")) == NULL) {
-               FILE_OP_ERROR(mbox, "fopen");
+       if ((mbox_fp = claws_fopen(mbox, "wb")) == NULL) {
+               FILE_OP_ERROR(mbox, "claws_fopen");
                alertpanel_error(_("Could not create mbox file:\n%s\n"), mbox);
                return -1;
        }
 
-#ifdef HAVE_FGETS_UNLOCKED
-       flockfile(mbox_fp);
-#endif
-
        statusbar_print_all(_("Exporting to mbox..."));
        for (cur = mlist; cur != NULL; cur = cur->next) {
                int len;
@@ -577,9 +562,6 @@ gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
                        continue;
                }
 
-#ifdef HAVE_FGETS_UNLOCKED
-               flockfile(msg_fp);
-#endif
                strncpy2(buf,
                         msginfo->from ? msginfo->from :
                         cur_account && cur_account->address ?
@@ -590,17 +572,14 @@ gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
                if (fprintf(mbox_fp, "From %s %s",
                        buf, ctime_r(&msginfo->date_t, buft)) < 0) {
                        err = -1;
-#ifdef HAVE_FGETS_UNLOCKED
-                       funlockfile(msg_fp);
-#endif
-                       fclose(msg_fp);
+                       claws_fclose(msg_fp);
                        goto out;
                }
 
                buf[0] = '\0';
                
                /* write email to mboxrc */
-               while (SC_FGETS(buf, sizeof(buf), msg_fp) != NULL) {
+               while (claws_fgets(buf, sizeof(buf), msg_fp) != NULL) {
                        /* quote any From, >From, >>From, etc., according to mbox format specs */
                        int offset;
 
@@ -610,21 +589,15 @@ gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
                                offset++;
                        }
                        if (!strncmp(buf+offset, "From ", 5)) {
-                               if (SC_FPUTC('>', mbox_fp) == EOF) {
+                               if (claws_fputc('>', mbox_fp) == EOF) {
                                        err = -1;
-#ifdef HAVE_FGETS_UNLOCKED
-                                       funlockfile(msg_fp);
-#endif
-                                       fclose(msg_fp);
+                                       claws_fclose(msg_fp);
                                        goto out;
                                }
                        }
-                       if (SC_FPUTS(buf, mbox_fp) == EOF) {
+                       if (claws_fputs(buf, mbox_fp) == EOF) {
                                err = -1;
-#ifdef HAVE_FGETS_UNLOCKED
-                               funlockfile(msg_fp);
-#endif
-                               fclose(msg_fp);
+                               claws_fclose(msg_fp);
                                goto out;
                        }
                }
@@ -634,31 +607,22 @@ gint export_list_to_mbox(GSList *mlist, const gchar *mbox)
                if (len > 0) {
                        len--;
                        if ((buf[len] != '\n') && (buf[len] != '\r')) {
-                               if (SC_FPUTC('\n', mbox_fp) == EOF) {
+                               if (claws_fputc('\n', mbox_fp) == EOF) {
                                        err = -1;
-#ifdef HAVE_FGETS_UNLOCKED
-                                       funlockfile(msg_fp);
-#endif
-                                       fclose(msg_fp);
+                                       claws_fclose(msg_fp);
                                        goto out;
                                }
                        }
                }
 
                /* add a trailing empty line */
-               if (SC_FPUTC('\n', mbox_fp) == EOF) {
+               if (claws_fputc('\n', mbox_fp) == EOF) {
                        err = -1;
-#ifdef HAVE_FGETS_UNLOCKED
-                       funlockfile(msg_fp);
-#endif
-                       fclose(msg_fp);
+                       claws_fclose(msg_fp);
                        goto out;
                }
 
-#ifdef HAVE_FGETS_UNLOCKED
-               funlockfile(msg_fp);
-#endif
-               safe_fclose(msg_fp);
+               claws_safe_fclose(msg_fp);
                statusbar_progress_all(msgs++,total, 500);
                if (msgs%500 == 0)
                        GTK_EVENTS_FLUSH();
@@ -668,10 +632,7 @@ out:
        statusbar_progress_all(0,0,0);
        statusbar_pop_all();
 
-#ifdef HAVE_FGETS_UNLOCKED
-       funlockfile(mbox_fp);
-#endif
-       safe_fclose(mbox_fp);
+       claws_safe_fclose(mbox_fp);
 
        return err;
 }
index 0f54089..829f158 100644 (file)
@@ -67,7 +67,7 @@
 #include "statusbar.h"
 #include "folder_item_prefs.h"
 #include "avatars.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 #ifndef USE_ALT_ADDRBOOK
        #include "addressbook.h"
@@ -893,8 +893,8 @@ static gint disposition_notification_send(MsgInfo *msginfo)
        g_snprintf(tmp, sizeof(tmp), "%s%ctmpmsg%p",
                   get_rc_dir(), G_DIR_SEPARATOR, msginfo);
 
-       if ((fp = g_fopen(tmp, "wb")) == NULL) {
-               FILE_OP_ERROR(tmp, "fopen");
+       if ((fp = claws_fopen(tmp, "wb")) == NULL) {
+               FILE_OP_ERROR(tmp, "claws_fopen");
                return -1;
        }
 
@@ -1062,8 +1062,8 @@ static gint disposition_notification_send(MsgInfo *msginfo)
        if (ok < 0)
                goto FILE_ERROR;        
 
-       if (safe_fclose(fp) == EOF) {
-               FILE_OP_ERROR(tmp, "fclose");
+       if (claws_safe_fclose(fp) == EOF) {
+               FILE_OP_ERROR(tmp, "claws_fclose");
                claws_unlink(tmp);
                return -1;
        }
@@ -1100,7 +1100,7 @@ static gint disposition_notification_send(MsgInfo *msginfo)
        return ok;
 
 FILE_ERROR:
-       fclose(fp);
+       claws_fclose(fp);
        claws_unlink(tmp);
        return -1;
 }
index 69d0eca..e7d0d6c 100644 (file)
--- a/src/mh.c
+++ b/src/mh.c
@@ -44,7 +44,7 @@
 #include "gtkutils.h"
 #include "timing.h"
 #include "msgcache.h"
-#include "claws_io.h"
+#include "file-utils.h"
 
 /* Define possible missing constants for Windows. */
 #ifdef G_OS_WIN32
@@ -1011,8 +1011,8 @@ static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
        path = folder_item_get_path(new_item);
        mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
                                            ".mh_sequences", NULL);
-       if ((mh_sequences_file = g_fopen(mh_sequences_filename, "a+b")) != NULL) {
-               fclose(mh_sequences_file);
+       if ((mh_sequences_file = claws_fopen(mh_sequences_filename, "a+b")) != NULL) {
+               claws_fclose(mh_sequences_file);
        }
        g_free(mh_sequences_filename);
        g_free(path);
@@ -1324,9 +1324,9 @@ static gchar *get_unseen_seq_name(void)
                gchar *profile_path = g_strconcat(
                        get_home_dir(), G_DIR_SEPARATOR_S,
                        ".mh_profile", NULL);
-               FILE *fp = g_fopen(profile_path, "r");
+               FILE *fp = claws_fopen(profile_path, "r");
                if (fp) {
-                       while (fgets(buf, sizeof(buf), fp) != NULL) {
+                       while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
                                if (!strncmp(buf, "Unseen-Sequence:", strlen("Unseen-Sequence:"))) {
                                        gchar *seq_tmp = buf+strlen("Unseen-Sequence:");
                                        while (*seq_tmp == ' ')
@@ -1336,7 +1336,7 @@ static gchar *get_unseen_seq_name(void)
                                        break;
                                }
                        }
-                       fclose(fp);
+                       claws_fclose(fp);
                }
                if (!seq_name)
                        seq_name = g_strdup("unseen");
@@ -1365,7 +1365,7 @@ static void mh_write_sequences(FolderItem *item, gboolean remove_unseen)
                                            ".mh_sequences", NULL);
        mh_sequences_new = g_strconcat(path, G_DIR_SEPARATOR_S,
                                            ".mh_sequences.new", NULL);
-       if ((mh_sequences_new_fp = g_fopen(mh_sequences_new, "w+b")) != NULL) {
+       if ((mh_sequences_new_fp = claws_fopen(mh_sequences_new, "w+b")) != NULL) {
                GSList *msglist = folder_item_get_msg_list(item);
                GSList *cur;
                MsgInfo *info = NULL;
@@ -1411,18 +1411,18 @@ static void mh_write_sequences(FolderItem *item, gboolean remove_unseen)
                                        get_unseen_seq_name(), sequence);
                }
                /* rewrite the rest of the file */
-               if ((mh_sequences_old_fp = g_fopen(mh_sequences_old, "r+b")) != NULL) {
-                       while (fgets(buf, sizeof(buf), mh_sequences_old_fp) != NULL) {
+               if ((mh_sequences_old_fp = claws_fopen(mh_sequences_old, "r+b")) != NULL) {
+                       while (claws_fgets(buf, sizeof(buf), mh_sequences_old_fp) != NULL) {
                                if (strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name())))
                                        if (fprintf(mh_sequences_new_fp, "%s", buf) < 0) {
                                                err = TRUE;
                                                break;
                                        }
                        }
-                       fclose(mh_sequences_old_fp);
+                       claws_fclose(mh_sequences_old_fp);
                }
                
-               if (safe_fclose(mh_sequences_new_fp) == EOF)
+               if (claws_safe_fclose(mh_sequences_new_fp) == EOF)
                        err = TRUE;
 
                if (!err) {
index 614bcd7..e1598f3 100644 (file)
@@ -55,6 +55,7 @@
 #include "timing.h"
 #include "manage_window.h"
 #include "privacy.h"
+#include "file-utils.h"
 
 typedef enum
 {
@@ -792,21 +793,21 @@ static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
        fname = mimeview->file;
        if (!fname) return;
 
-       if ((fp = g_fopen(fname, "rb")) == NULL) {
-               FILE_OP_ERROR(fname, "fopen");
+       if ((fp = claws_fopen(fname, "rb")) == NULL) {
+               FILE_OP_ERROR(fname, "claws_fopen");
                return;
        }
 
        if (fseek(fp, partinfo->offset, SEEK_SET) < 0) {
                FILE_OP_ERROR(mimeview->file, "fseek");
-               fclose(fp);
+               claws_fclose(fp);
                return;
        }
 
        mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
        textview_show_part(mimeview->textview, partinfo, fp);
 
-       fclose(fp);
+       claws_fclose(fp);
 }
 
 static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
@@ -1692,7 +1693,7 @@ static void mimeview_drag_data_get(GtkWidget          *widget,
                GPtrArray *headers = NULL;
                FILE *fp;
 
-               fp = g_fopen(partinfo->data.filename, "rb");
+               fp = claws_fopen(partinfo->data.filename, "rb");
                if (fp != NULL && fseek(fp, partinfo->offset, SEEK_SET) == 0) {
                        headers = procheader_get_header_array_asis(fp);
                        if (headers) {
@@ -1709,7 +1710,7 @@ static void mimeview_drag_data_get(GtkWidget          *widget,
                        }
                }
                if (fp != NULL)
-                       fclose(fp);
+                       claws_fclose(fp);
                if (name)
                        filename = g_path_get_basename(name);
                g_free(name);
index 8909ea7..3910255 100644 (file)
@@ -24,7 +24,6 @@
 
 #include "defs.h"
 
-#define _GNU_SOURCE
 #include <stdio.h>
 
 #include <glib.h>
 #include "timing.h"
 #include "tags.h"
 #include "prefs_common.h"
-#include "claws_io.h"
-
-#ifdef HAVE_FWRITE_UNLOCKED
-#define SC_FWRITE fwrite_unlocked
-#else
-#define SC_FWRITE fwrite
-#endif
+#include "file-utils.h"
 
 #if G_BYTE_ORDER == G_BIG_ENDIAN
 #define bswap_32(x) \
@@ -312,7 +305,7 @@ gint msgcache_get_memory_usage(MsgCache *cache)
        guint32 idata; \
        size_t ni; \
  \
-       if ((ni = fread(&idata, sizeof(idata), 1, fp)) != 1) { \
+       if ((ni = claws_fread(&idata, sizeof(idata), 1, fp)) != 1) { \
                g_warning("read_int: Cache data corrupted, read %zd of %zd at " \
                          "offset %ld", ni, sizeof(idata), ftell(fp)); \
                procmsg_msginfo_free(&msginfo); \
@@ -357,7 +350,7 @@ gint msgcache_get_memory_usage(MsgCache *cache)
        guint32 idata;                                  \
                                                        \
        idata = (guint32)bswap_32(n);                   \
-       if (SC_FWRITE(&idata, sizeof(idata), 1, fp) != 1)       \
+       if (claws_fwrite(&idata, sizeof(idata), 1, fp) != 1)    \
                w_err = 1;                              \
        wrote += 4;                                     \
 }
@@ -381,7 +374,7 @@ gint msgcache_get_memory_usage(MsgCache *cache)
                len = strlen(data);                     \
        WRITE_CACHE_DATA_INT(len, fp);                  \
        if (w_err == 0 && len > 0) {                    \
-               if (SC_FWRITE(data, 1, len, fp) != len) \
+               if (claws_fwrite(data, 1, len, fp) != len)      \
                        w_err = 1;                      \
                wrote += len;                           \
        } \
@@ -413,8 +406,8 @@ static FILE *msgcache_open_data_file(const gchar *file, guint version,
 
        if (mode == DATA_WRITE) {
                int w_err = 0, wrote = 0;
-               if ((fp = g_fopen(file, "wb")) == NULL) {
-                       FILE_OP_ERROR(file, "fopen");
+               if ((fp = claws_fopen(file, "wb")) == NULL) {
+                       FILE_OP_ERROR(file, "claws_fopen");
                        return NULL;
                }
                if (change_file_mode_rw(fp, file) < 0)
@@ -423,23 +416,23 @@ static FILE *msgcache_open_data_file(const gchar *file, guint version,
                WRITE_CACHE_DATA_INT(version, fp);
                if (w_err != 0) {
                        g_warning("failed to write int");
-                       fclose(fp);
+                       claws_fclose(fp);
                        return NULL;
                }
                return fp;
        }
 
        /* check version */
-       if ((fp = g_fopen(file, "rb")) == NULL)
+       if ((fp = claws_fopen(file, "rb")) == NULL)
                debug_print("Mark/Cache file '%s' not found\n", file);
        else {
                if (buf && buf_size > 0)
                        setvbuf(fp, buf, _IOFBF, buf_size);
-               if (fread(&data_ver, sizeof(data_ver), 1, fp) != 1 ||
+               if (claws_fread(&data_ver, sizeof(data_ver), 1, fp) != 1 ||
                         version != bswap_32(data_ver)) {
                        g_message("%s: Mark/Cache version is different (%u != %u).\n",
                                  file, bswap_32(data_ver), version);
-                       fclose(fp);
+                       claws_fclose(fp);
                        fp = NULL;
                }
                data_ver = bswap_32(data_ver);
@@ -450,9 +443,9 @@ static FILE *msgcache_open_data_file(const gchar *file, guint version,
 
        if (fp) {
                /* reopen with append mode */
-               fclose(fp);
-               if ((fp = g_fopen(file, "ab")) == NULL)
-                       FILE_OP_ERROR(file, "fopen");
+               claws_fclose(fp);
+               if ((fp = claws_fopen(file, "ab")) == NULL)
+                       FILE_OP_ERROR(file, "claws_fopen");
        } else {
                /* open with overwrite mode if mark file doesn't exist or
                   version is different */
@@ -472,7 +465,7 @@ static gint msgcache_read_cache_data_str(FILE *fp, gchar **str,
 
        *str = NULL;
        if (!swapping) {
-               if ((ni = fread(&len, sizeof(len), 1, fp) != 1) ||
+               if ((ni = claws_fread(&len, sizeof(len), 1, fp) != 1) ||
                    len > G_MAXINT) {
                        g_warning("read_data_str: Cache data (len) corrupted, read %zd "
                                  "of %zd bytes at offset %ld", ni, sizeof(len),
@@ -480,7 +473,7 @@ static gint msgcache_read_cache_data_str(FILE *fp, gchar **str,
                        return -1;
                }
        } else {
-               if ((ni = fread(&len, sizeof(len), 1, fp) != 1) ||
+               if ((ni = claws_fread(&len, sizeof(len), 1, fp) != 1) ||
                    bswap_32(len) > G_MAXINT) {
                        g_warning("read_data_str: Cache data (len) corrupted, read %zd "
                                  "of %zd bytes at offset %ld", ni, sizeof(len),
@@ -499,7 +492,7 @@ static gint msgcache_read_cache_data_str(FILE *fp, gchar **str,
                return -1;
        }
 
-       if ((ni = fread(tmpstr, 1, len, fp)) != len) {
+       if ((ni = claws_fread(tmpstr, 1, len, fp)) != len) {
                g_warning("read_data_str: Cache data corrupted, read %zd of %u "
                          "bytes at offset %ld",
                          ni, len, ftell(fp));
@@ -612,7 +605,7 @@ MsgCache *msgcache_read_cache(FolderItem *item, const gchar *cache_file)
        }
 
        if (msgcache_read_cache_data_str(fp, &srccharset, NULL) < 0) {
-               fclose(fp);
+               claws_fclose(fp);
                return NULL;
        }
        dstcharset = CS_UTF_8;
@@ -718,7 +711,7 @@ MsgCache *msgcache_read_cache(FolderItem *item, const gchar *cache_file)
                                g_hash_table_insert(cache->msgid_table, msginfo->msgid, msginfo);
                }
        } else {
-               while (fread(&num, sizeof(num), 1, fp) == 1) {
+               while (claws_fread(&num, sizeof(num), 1, fp) == 1) {
                        if (swapping)
                                num = bswap_32(num);
 
@@ -776,7 +769,7 @@ bail_err:
                munmap(cache_data, map_len);
 #endif
        }
-       fclose(fp);
+       claws_fclose(fp);
        if (conv != NULL) {
                if (conv->free != NULL)
                        conv->free(conv);
@@ -863,10 +856,10 @@ void msgcache_read_mark(MsgCache *cache, const gchar *mark_file)
                        }
                }
        } else {
-               while (fread(&num, sizeof(num), 1, fp) == 1) {
+               while (claws_fread(&num, sizeof(num), 1, fp) == 1) {
                        if (swapping)
                                num = bswap_32(num);
-                       if (fread(&perm_flags, sizeof(perm_flags), 1, fp) != 1) {
+                       if (claws_fread(&perm_flags, sizeof(perm_flags), 1, fp) != 1) {
                                error = TRUE;
                                break;
                        }
@@ -886,7 +879,7 @@ bail_err:
                munmap(cache_data, map_len);
 #endif
        }
-       fclose(fp);
+       claws_fclose(fp);
        if (error) {
                debug_print("error reading cache mark from %s\n", mark_file);
        }
@@ -967,7 +960,7 @@ void msgcache_read_tags(MsgCache *cache, const gchar *tags_file)
                        }
                }
        } else {
-               while (fread(&num, sizeof(num), 1, fp) == 1) {
+               while (claws_fread(&num, sizeof(num), 1, fp) == 1) {
                        gint id = -1;
                        if (swapping)
                                num = bswap_32(num);
@@ -976,7 +969,7 @@ void msgcache_read_tags(MsgCache *cache, const gchar *tags_file)
                                g_slist_free(msginfo->tags);
                                msginfo->tags = NULL;
                                do {
-                                       if (fread(&id, sizeof(id), 1, fp) != 1) 
+                                       if (claws_fread(&id, sizeof(id), 1, fp) != 1) 
                                                id = -1;
                                        if (swapping)
                                                id = bswap_32(id);
@@ -998,7 +991,7 @@ bail_err:
                munmap(cache_data, map_len);
 #endif
        }
-       fclose(fp);
+       claws_fclose(fp);
        if (error) {
                debug_print("error reading cache tags from %s\n", tags_file);
        }
@@ -1144,7 +1137,7 @@ gint msgcache_write(const gchar *cache_file, const gchar *mark_file, const gchar
        if (w_err != 0) {
                g_warning("failed to write charset");
                if (write_fps.cache_fp)
-                       fclose(write_fps.cache_fp);
+                       claws_fclose(write_fps.cache_fp);
                claws_unlink(new_cache);
                g_free(new_cache);
                g_free(new_mark);
@@ -1157,7 +1150,7 @@ gint msgcache_write(const gchar *cache_file, const gchar *mark_file, const gchar
                        DATA_WRITE, NULL, 0);
                if (write_fps.mark_fp == NULL) {
                        if (write_fps.cache_fp)
-                               fclose(write_fps.cache_fp);
+                               claws_fclose(write_fps.cache_fp);
                        claws_unlink(new_cache);
                        g_free(new_cache);
                        g_free(new_mark);
@@ -1173,9 +1166,9 @@ gint msgcache_write(const gchar *cache_file, const gchar *mark_file, const gchar
                        DATA_WRITE, NULL, 0);
                if (write_fps.tags_fp == NULL) {
                        if (write_fps.cache_fp)
-                               fclose(write_fps.cache_fp);
+                               claws_fclose(write_fps.cache_fp);
                        if (write_fps.mark_fp)
-                               fclose(write_fps.mark_fp);
+                               claws_fclose(write_fps.mark_fp);
                        claws_unlink(new_cache);
                        claws_unlink(new_mark);
                        g_free(new_cache);
@@ -1200,34 +1193,16 @@ gint msgcache_write(const gchar *cache_file, const gchar *mark_file, const gchar
        if (write_fps.tags_fp)
                write_fps.tags_size = ftell(write_fps.ta