[evolution-patches] Trivial e-d-s/calendar Win32 patches
- From: Tor Lillqvist <tml novell com>
- To: patches <evolution-patches gnome org>
- Subject: [evolution-patches] Trivial e-d-s/calendar Win32 patches
- Date: Wed, 30 Nov 2005 01:32:34 +0000
Like the camel diff, this is mostly trivialities:
* Use gstdio wrappers.
* Use GMappedFile API instead of reading through a file in a loop.
Well, this is not such a trivial change, but simplifies the
code.
* Use g_filename_to_uri() and g_filename_from_uri() instead of
just prefixing with "file://" or stripping off a "file://"
prefix. That doesn't work in general as URIs are URI-encoded,
and does not work at all on Windows as file: URIs look like
"file:///c:/some/where/foo.bar", i.e. just stripping off the
"file://" prefix leaves "/c:/some/where/foo.bar" which is
invalid.
* Use g_path_get_basename() instead of g_strrstr(fn, "/")+1.
* Define localtime_r() and strtok_r() on Win32.
--tml
Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/evolution-data-server/calendar/ChangeLog,v
retrieving revision 1.530
diff -p -u -2 -r1.530 ChangeLog
--- ChangeLog 28 Nov 2005 19:06:47 -0000 1.530
+++ ChangeLog 30 Nov 2005 00:04:39 -0000
@@ -1,2 +1,41 @@
+2005-11-30 Tor Lillqvist <tml novell com>
+
+ * backends/file/e-cal-backend-file.c
+ * backends/groupwise/e-cal-backend-groupwise-utils.c: Use gstdio
+ wrappers for full non-ASCII filename support on Windows.
+
+ * backends/file/e-cal-backend-file.c (fetch_attachments)
+ * backends/groupwise/e-cal-backend-groupwise-utils.c
+ (e_cal_backend_groupwise_set_attachments_from_comp): Use
+ GMappedFile API instead of reading in a loop.
+
+ * backends/file/e-cal-backend-file.c (fetch_attachments)
+ * backends/groupwise/e-cal-backend-groupwise.c
+ (e_cal_backend_groupwise_open)
+ * libecal/e-cal.c (set_local_attachment_store,
+ e_cal_new_system_calendar, e_cal_new_system_tasks,
+ e_cal_new_system_memos): Use g_filename_to_uri() instead of just
+ prefixing "file:///".
+
+ * backends/file/e-cal-backend-file.c
+ (get_uri_string_for_gnome_vfs): Don't use G_DIR_SEPARATOR when
+ constructing a URI. URIs always use the (forward) slash.
+
+ * backends/groupwise/e-cal-backend-groupwise-utils.c
+ (e_cal_backend_groupwise_set_attachments_from_comp,
+ set_attachments_to_cal_component): Use g_filename_from_uri()
+ instead of just stripping off the file:// prefix.
+
+ * backends/weather/e-weather-source-ccf.c: Define localtime_r()
+ and strtok_r() on Win32 in terms of localtime() and strtok() which
+ are MT-safe in the Microsoft C library.
+ (find_station_url): Construct pathname to locations file at
+ run-time on Win32. Use GMappedFile and xmlParseMemory() for
+ simplicity, xmlParseFile() doesn't do UTF-8 file names on Win32.
+ (tokenize): No need to allocate a buffer for strtok_r(), that's
+ not how it works.
+
+ * libecal/Makefile.am: Drop unused EVOLUTION_LOCALEDIR.
+
2005-11-28 Chenthill Palanisamy <pchenthill novell com>
Index: libecal/Makefile.am
===================================================================
RCS file: /cvs/gnome/evolution-data-server/calendar/libecal/Makefile.am,v
retrieving revision 1.13
diff -p -u -2 -r1.13 Makefile.am
--- libecal/Makefile.am 10 Aug 2005 22:26:36 -0000 1.13
+++ libecal/Makefile.am 30 Nov 2005 00:04:39 -0000
@@ -1,4 +1,3 @@
INCLUDES = \
- -DEVOLUTION_LOCALEDIR=\""$(localedir)"\" \
-DG_LOG_DOMAIN=\"libecal\" \
-I$(top_srcdir)/calendar \
Index: backends/file/e-cal-backend-file.c
===================================================================
RCS file: /cvs/gnome/evolution-data-server/calendar/backends/file/e-cal-backend-file.c,v
retrieving revision 1.67
diff -p -u -2 -r1.67 e-cal-backend-file.c
--- backends/file/e-cal-backend-file.c 28 Nov 2005 19:06:48 -0000 1.67
+++ backends/file/e-cal-backend-file.c 30 Nov 2005 00:04:41 -0000
@@ -31,4 +31,5 @@
#include <bonobo/bonobo-exception.h>
#include <bonobo/bonobo-moniker-util.h>
+#include <glib/gstdio.h>
#include <glib/gi18n-lib.h>
#include <libgnomevfs/gnome-vfs.h>
@@ -42,4 +43,8 @@
#include "e-cal-backend-file-events.h"
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
@@ -617,5 +622,5 @@ get_uri_string_for_gnome_vfs (ECalBacken
}
- full_uri = g_strdup_printf ("%s%s%s", master_uri, G_DIR_SEPARATOR_S, priv->file_name);
+ full_uri = g_strdup_printf ("%s/%s", master_uri, priv->file_name);
uri = gnome_vfs_uri_new (full_uri);
g_free (full_uri);
@@ -886,7 +891,7 @@ e_cal_backend_file_open (ECalBackendSync
return GNOME_Evolution_Calendar_OtherError;
- if (access (str_uri, R_OK) == 0) {
+ if (g_access (str_uri, R_OK) == 0) {
status = open_cal (cbfile, str_uri);
- if (access (str_uri, W_OK) != 0)
+ if (g_access (str_uri, W_OK) != 0)
priv->read_only = TRUE;
} else {
@@ -923,5 +928,5 @@ e_cal_backend_file_remove (ECalBackendSy
return GNOME_Evolution_Calendar_OtherError;
- if (access (str_uri, W_OK) != 0) {
+ if (g_access (str_uri, W_OK) != 0) {
g_free (str_uri);
@@ -943,5 +948,5 @@ e_cal_backend_file_remove (ECalBackendSy
full_path = g_build_filename (dirname, fname, NULL);
- if (unlink (full_path) != 0) {
+ if (g_unlink (full_path) != 0) {
g_free (full_path);
g_free (str_uri);
@@ -956,5 +961,5 @@ e_cal_backend_file_remove (ECalBackendSy
/* remove the directory itself */
- success = rmdir (dirname) == 0;
+ success = g_rmdir (dirname) == 0;
g_dir_close (dir);
@@ -2159,4 +2164,8 @@ check_tzids (icalparameter *param, void
}
+
+/* This function is largely duplicated in
+ * ../groupwise/e-cal-backend-groupwise.c
+ */
static void
fetch_attachments (ECalBackendSync *backend, ECalComponent *comp)
@@ -2164,68 +2173,50 @@ fetch_attachments (ECalBackendSync *back
GSList *attach_list = NULL, *new_attach_list = NULL;
GSList *l;
- char *attach_store, *filename, *file_contents;
+ char *attach_store;
char *dest_url, *dest_file;
- int fd, len;
- int len_read = 0;
- char buf[1024];
- struct stat sb;
+ int fd;
const char *uid;
-
e_cal_component_get_attachment_list (comp, &attach_list);
e_cal_component_get_uid (comp, &uid);
/*FIXME get the uri rather than computing the path */
- attach_store = g_strconcat (g_get_home_dir (), "/",
+ attach_store = g_build_filename (g_get_home_dir (),
".evolution/calendar/local/system", NULL);
for (l = attach_list; l ; l = l->next) {
char *sfname = (char *)l->data;
-
- filename = g_strrstr (sfname, "/") + 1;
-
- // open the file using the data
- fd = open (sfname, O_RDONLY);
- if (fd == -1) {
- /* TODO handle error conditions */
- g_message ("DEBUG: could not open the file descriptor\n");
- continue;
- }
- if (fstat (fd, &sb) == -1) {
- /* TODO handle error conditions */
- g_message ("DEBUG: could not fstat the attachment file\n");
+ char *filename, *new_filename;
+ GMappedFile *mapped_file;
+ GError *error = NULL;
+
+ mapped_file = g_mapped_file_new (sfname, FALSE, &error);
+ if (!mapped_file) {
+ g_message ("DEBUG: could not map %s: %s\n",
+ sfname, error->message);
+ g_error_free (error);
continue;
}
- len = sb.st_size;
- file_contents = g_malloc (len + 1);
-
- while (len_read < len) {
- int c = read (fd, buf, sizeof (buf));
-
- if (c == -1)
- break;
-
- memcpy (&file_contents[len_read], buf, c);
- len_read += c;
- }
- file_contents [len_read] = 0;
-
- /* write*/
- dest_file = g_strconcat (attach_store, "/", uid, "-",
- filename, NULL);
- fd = open (dest_file, O_RDWR|O_CREAT|O_TRUNC, 0600);
+ filename = g_path_get_basename (sfname);
+ new_filename = g_strconcat (uid, "-", filename, NULL);
+ g_free (filename);
+ dest_file = g_build_filename (attach_store, new_filename, NULL);
+ g_free (new_filename);
+ fd = g_open (dest_file, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0600);
if (fd == -1) {
/* TODO handle error conditions */
- g_message ("DEBUG: could not serialize attachments\n");
- }
-
- if (write (fd, file_contents, len_read) == -1) {
+ g_message ("DEBUG: could not open %s for writing\n",
+ dest_file);
+ } else if (write (fd, g_mapped_file_get_contents (mapped_file),
+ g_mapped_file_get_length (mapped_file)) == -1) {
/* TODO handle error condition */
g_message ("DEBUG: attachment write failed.\n");
}
- dest_url = g_strconcat ("file:///", dest_file, NULL);
- new_attach_list = g_slist_append (new_attach_list, dest_url);
+ g_mapped_file_free (mapped_file);
+ close (fd);
+ dest_url = g_filename_to_uri (dest_file, NULL, NULL);
g_free (dest_file);
+ new_attach_list = g_slist_append (new_attach_list, dest_url);
}
g_free (attach_store);
@@ -2603,7 +2594,7 @@ e_cal_backend_file_reload (ECalBackendFi
return GNOME_Evolution_Calendar_OtherError;
- if (access (str_uri, R_OK) == 0) {
+ if (g_access (str_uri, R_OK) == 0) {
status = reload_cal (cbfile, str_uri);
- if (access (str_uri, W_OK) != 0)
+ if (g_access (str_uri, W_OK) != 0)
priv->read_only = TRUE;
} else {
Index: backends/groupwise/e-cal-backend-groupwise-utils.c
===================================================================
RCS file: /cvs/gnome/evolution-data-server/calendar/backends/groupwise/e-cal-backend-groupwise-utils.c,v
retrieving revision 1.72
diff -p -u -2 -r1.72 e-cal-backend-groupwise-utils.c
--- backends/groupwise/e-cal-backend-groupwise-utils.c 11 Oct 2005 07:56:18 -0000 1.72
+++ backends/groupwise/e-cal-backend-groupwise-utils.c 30 Nov 2005 00:04:42 -0000
@@ -28,8 +28,10 @@
#include <string.h>
#include <sys/types.h>
-#include <glib/gi18n.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
+#include <glib.h>
+#include <glib/gi18n.h>
+#include <glib/gstdio.h>
#include <libgnomevfs/gnome-vfs-mime-utils.h>
#include <e-gw-connection.h>
@@ -266,46 +268,16 @@ e_cal_backend_groupwise_set_attachments_
EGwItemAttachment *attach_item;
- char *file_contents, *encoded_data;
- int fd, len;
- int len_read = 0;
- char buf[1024];
- struct stat sb;
+ char *encoded_data;
char *attach_filename_full, *filename;
const char *uid;
-
-
- attach_filename_full = (char *)l->data + 7;
- attach_item = g_new0 (EGwItemAttachment, 1);
- /* FIXME the member does not follow the naming convention.
- * Should be fixed in e-gw-item*/
- attach_item->contentType = g_strdup (gnome_vfs_get_mime_type (attach_filename_full));
+ GMappedFile *mapped_file;
- /*
- * Would gnome_vfs_async be better suited for this ?
- */
- fd = open (attach_filename_full, O_RDONLY);
- if (fd == -1) {
- g_free (attach_item);
- g_message ("DEBUG: could not open the file descriptor\n");
- }
- if (fstat (fd, &sb) == -1) {
- g_free (attach_item);
- g_message ("DEBUG: could not fstat the attachment file\n");
+ attach_filename_full = g_filename_from_uri ((char *)l->data, NULL, NULL);
+ mapped_file = g_mapped_file_new (attach_filename_full, FALSE, NULL);
+ if (!mapped_file) {
+ g_message ("DEBUG: could not map %s\n", attach_filename_full);
+ g_free (attach_filename_full);
continue;
}
- len = sb.st_size;
-
- file_contents = g_malloc (len + 1);
-
- while (len_read < len) {
- int c = read (fd, buf, sizeof (buf));
-
- if (c == -1)
- break;
-
- memcpy (&file_contents[len_read], buf, c);
- len_read += c;
- }
- file_contents [len_read] = 0;
/* Extract the simple file name from the
@@ -315,22 +287,27 @@ e_cal_backend_groupwise_set_attachments_
e_cal_component_get_uid (comp, &uid);
filename = g_strrstr (attach_filename_full, uid);
-
if (filename == NULL) {
- g_free (attach_item);
- g_message ("DEBUG:\n This is an invalid attachment file\n");
+ g_message ("DEBUG: This is an invalid attachment file\n");
+ g_free (attach_filename_full);
+ g_mapped_file_free (mapped_file);
continue;
- }
+ }
+ attach_item = g_new0 (EGwItemAttachment, 1);
+ /* FIXME the member does not follow the naming convention.
+ * Should be fixed in e-gw-item*/
+ attach_item->contentType = g_strdup (gnome_vfs_get_mime_type (attach_filename_full));
attach_item->name = g_strdup (filename + strlen(uid) + 1);
/* do a base64 encoding so it can be embedded in a soap
* message */
- encoded_data = soup_base64_encode (file_contents, len_read);
+ encoded_data = soup_base64_encode (g_mapped_file_get_contents (mapped_file),
+ g_mapped_file_get_length (mapped_file));
attach_item->data = encoded_data;
attach_item->size = strlen (encoded_data);
- g_free (file_contents);
- close (fd);
+ g_mapped_file_free (mapped_file);
attach_list = g_slist_append (attach_list, attach_item);
+ g_free (attach_filename_full);
}
@@ -793,4 +770,5 @@ set_attachments_to_cal_component (EGwIte
char *attach_data = NULL;
struct stat st;
+ char *filename;
attach_item = (EGwItemAttachment *) l->data;
@@ -798,14 +776,15 @@ set_attachments_to_cal_component (EGwIte
"/", uid, "-", attach_item->name, NULL);
- if (stat (attach_file_url + 7, &st) == -1) {
- if (!get_attach_data_from_server (attach_item, cbgw))
- return; /* Could not get the attachment from the server */
- fd = open (attach_file_url+7, O_RDWR|O_CREAT|O_TRUNC, 0600);
+ filename = g_filename_from_uri (attach_file_url, NULL, NULL);
+ if (g_stat (filename, &st) == -1) {
+ if (!get_attach_data_from_server (attach_item, cbgw)) {
+ g_free (filename);
+ return; /* Could not get the attachment from the server */
+ }
+ fd = g_open (filename, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0600);
if (fd == -1) {
/* skip gracefully */
g_warning ("DEBUG: could not serialize attachments\n");
- }
-
- if (write (fd, attach_item->data, attach_item->size) == -1) {
+ } else if (write (fd, attach_item->data, attach_item->size) == -1) {
/* skip gracefully */
g_warning ("DEBUG: attachment write failed.\n");
@@ -814,4 +793,5 @@ set_attachments_to_cal_component (EGwIte
close (fd);
}
+ g_free (filename);
comp_attachment_list = g_slist_append (comp_attachment_list, attach_file_url);
Index: backends/groupwise/e-cal-backend-groupwise.c
===================================================================
RCS file: /cvs/gnome/evolution-data-server/calendar/backends/groupwise/e-cal-backend-groupwise.c,v
retrieving revision 1.171
diff -p -u -2 -r1.171 e-cal-backend-groupwise.c
--- backends/groupwise/e-cal-backend-groupwise.c 24 Oct 2005 08:45:29 -0000 1.171
+++ backends/groupwise/e-cal-backend-groupwise.c 30 Nov 2005 00:04:45 -0000
@@ -31,4 +31,5 @@
#include <fcntl.h>
#include <unistd.h>
+#include <glib/gstdio.h>
#include <glib/gi18n-lib.h>
#include <libgnomevfs/gnome-vfs-uri.h>
@@ -1074,4 +1075,5 @@ e_cal_backend_groupwise_open (ECalBacken
ECalBackendGroupwisePrivate *priv;
ECalBackendSyncStatus status;
+ char *filename;
char *mangled_uri;
int i;
@@ -1128,8 +1130,12 @@ e_cal_backend_groupwise_open (ECalBacken
}
- priv->local_attachments_store =
- g_strconcat ("file://", g_get_home_dir (), "/", ".evolution/cache/calendar",
- "/", mangled_uri, NULL);
+ filename = g_build_filename (g_get_home_dir (),
+ ".evolution/cache/calendar",
+ mangled_uri,
+ NULL);
g_free (mangled_uri);
+ priv->local_attachments_store =
+ g_filename_to_uri (filename, NULL, NULL);
+ g_free (filename);
/* FIXME: no need to set it online here when we implement the online/offline stuff correctly */
@@ -2050,4 +2056,7 @@ e_cal_backend_groupwise_remove_object (E
}
+/* This function is largely duplicated in
+ * ../file/e-cal-backend-file.c
+ */
static void
fetch_attachments (ECalBackendGroupwise *cbgw, ECalComponent *comp)
@@ -2055,68 +2064,49 @@ fetch_attachments (ECalBackendGroupwise
GSList *attach_list = NULL, *new_attach_list = NULL;
GSList *l;
- char *attach_store, *filename, *file_contents;
+ char *attach_store;
char *dest_url, *dest_file;
- int fd, len;
- int len_read = 0;
- char buf[1024];
- struct stat sb;
+ int fd;
const char *uid;
-
e_cal_component_get_attachment_list (comp, &attach_list);
e_cal_component_get_uid (comp, &uid);
/*FIXME get the uri rather than computing the path */
- attach_store = g_strconcat
- (e_cal_backend_groupwise_get_local_attachments_store (cbgw), NULL);
+ attach_store = g_strdup (e_cal_backend_groupwise_get_local_attachments_store (cbgw));
for (l = attach_list; l ; l = l->next) {
char *sfname = (char *)l->data;
-
- filename = g_strrstr (sfname, "/") + 1;
-
- // open the file using the data
- fd = open (sfname, O_RDONLY);
- if (fd == -1) {
- /* TODO handle error conditions */
- g_message ("DEBUG: could not open the file descriptor\n");
- continue;
- }
- if (fstat (fd, &sb) == -1) {
- /* TODO handle error conditions */
- g_message ("DEBUG: could not fstat the attachment file\n");
+ char *filename, *new_filename;
+ GMappedFile *mapped_file;
+ GError *error = NULL;
+
+ mapped_file = g_mapped_file_new (sfname, FALSE, &error);
+ if (!mapped_file) {
+ g_message ("DEBUG: could not map %s: %s\n",
+ sfname, error->message);
+ g_error_free (error);
continue;
}
- len = sb.st_size;
- file_contents = g_malloc (len + 1);
-
- while (len_read < len) {
- int c = read (fd, buf, sizeof (buf));
-
- if (c == -1)
- break;
-
- memcpy (&file_contents[len_read], buf, c);
- len_read += c;
- }
- file_contents [len_read] = 0;
-
- /* write*/
- dest_file = g_strconcat (attach_store, "/", uid, "-",
- filename, NULL);
- fd = open (dest_file, O_RDWR|O_CREAT|O_TRUNC, 0600);
+ filename = g_path_get_basename (sfname);
+ new_filename = g_strconcat (uid, "-", filename, NULL);
+ g_free (filename);
+ dest_file = g_build_filename (attach_store, new_filename, NULL);
+ g_free (new_filename);
+ fd = g_open (dest_file, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0600);
if (fd == -1) {
/* TODO handle error conditions */
- g_message ("DEBUG: could not serialize attachments\n");
- }
-
- if (write (fd, file_contents, len_read) == -1) {
+ g_message ("DEBUG: could not open %s for writing\n",
+ dest_file);
+ } else if (write (fd, g_mapped_file_get_contents (mapped_file),
+ g_mapped_file_get_length (mapped_file)) == -1) {
/* TODO handle error condition */
g_message ("DEBUG: attachment write failed.\n");
}
- dest_url = g_strconcat ("file:///", dest_file, NULL);
- new_attach_list = g_slist_append (new_attach_list, dest_url);
+ g_mapped_file_free (mapped_file);
+ close (fd);
+ dest_url = g_filename_to_uri (dest_file, NULL, NULL);
g_free (dest_file);
+ new_attach_list = g_slist_append (new_attach_list, dest_url);
}
g_free (attach_store);
Index: backends/weather/e-weather-source-ccf.c
===================================================================
RCS file: /cvs/gnome/evolution-data-server/calendar/backends/weather/e-weather-source-ccf.c,v
retrieving revision 1.3
diff -p -u -2 -r1.3 e-weather-source-ccf.c
--- backends/weather/e-weather-source-ccf.c 31 Aug 2005 04:21:53 -0000 1.3
+++ backends/weather/e-weather-source-ccf.c 30 Nov 2005 00:04:45 -0000
@@ -33,4 +33,23 @@
#define DATA_SIZE 5000
+#ifdef G_OS_WIN32
+
+#include <libedataserver/e-util.h>
+
+/* The localtime_r() in <pthread.h> doesn't guard against localtime()
+ * returning NULL
+ */
+#undef localtime_r
+
+/* The localtime() in Microsoft's C library is MT-safe */
+#define localtime_r(tp,tmp) (localtime(tp)?(*(tmp)=*localtime(tp),(tmp)):0)
+
+/* strtok() is also MT-safe (but not stateless, still uses only one
+ * buffer pointer per thread, but for the use of strtok_r() here
+ * that's enough).
+ */
+#define strtok_r(s,sep,lasts) (*(lasts)=strtok((s),(sep)))
+#endif
+
static gchar *
parse_for_url (char *code, char *name, xmlNode *parent)
@@ -73,8 +92,25 @@ find_station_url (gchar *station, EWeath
gchar **sstation;
gchar *url;
+ gchar *filename;
+ GMappedFile *mapped_file;
sstation = g_strsplit (station, "/", 2);
- doc = xmlParseFile (WEATHER_DATADIR "/Locations.xml");
+#ifndef G_OS_WIN32
+ filename = g_strdup (WEATHER_DATADIR "/Locations.xml");
+#else
+ filename = e_util_replace_prefix (e_util_get_prefix (),
+ WEATHER_DATADIR "/Locations.xml");
+#endif
+
+ mapped_file = g_mapped_file_new (filename, FALSE, NULL);
+ g_free (filename);
+
+ g_assert (mapped_file);
+
+ doc = xmlParseMemory (g_mapped_file_get_contents (mapped_file),
+ g_mapped_file_get_length (mapped_file));
+ g_mapped_file_free (mapped_file);
+
g_assert (doc != NULL);
@@ -107,6 +143,5 @@ tokenize (char *buffer)
{
char *token;
- char *tokbuf = g_strdup (buffer);
- char *buffer2 = tokbuf;
+ char *tokbuf;
GSList *ret;
@@ -115,5 +150,4 @@ tokenize (char *buffer)
while ((token = strtok_r (NULL, " \n/", &tokbuf)))
ret = g_slist_append (ret, g_strdup (token));
- g_free (buffer2);
return ret;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]