pidgin/pidgin

Provide a portable alternative for fstat

2014-05-13, Tomasz Wasilczyk
4bba5ea5d08a
Parents 1bed3dca6bd2
Children 75cbabcd0a4d
Provide a portable alternative for fstat
--- a/libpurple/internal.h Tue May 13 12:02:54 2014 +0200
+++ b/libpurple/internal.h Tue May 13 13:13:57 2014 +0200
@@ -354,4 +354,15 @@
gboolean
_purple_network_set_common_socket_flags(int fd);
+/**
+ * A fstat alternative, like g_stat for stat.
+ *
+ * @param fd The file descriptor.
+ * @param st The stat buffer.
+ *
+ * @return the result just like for fstat.
+ */
+int
+_purple_fstat(int fd, GStatBuf *st);
+
#endif /* _PURPLE_INTERNAL_H_ */
--- a/libpurple/log.c Tue May 13 12:02:54 2014 +0200
+++ b/libpurple/log.c Tue May 13 13:13:57 2014 +0200
@@ -1732,7 +1732,7 @@
g_free(pathstr);
return NULL;
}
- if (fstat(file_fd, &st) == -1) {
+ if (_purple_fstat(file_fd, &st) == -1) {
purple_stringref_unref(pathref);
g_free(pathstr);
fclose(file);
@@ -1745,7 +1745,7 @@
index_fd = g_open(pathstr, 0, O_RDONLY);
if (index_fd != -1) {
- if (fstat(index_fd, &st) != 0) {
+ if (_purple_fstat(index_fd, &st) != 0) {
close(index_fd);
index_fd = -1;
}
--- a/libpurple/protocols/silc/util.c Tue May 13 12:02:54 2014 +0200
+++ b/libpurple/protocols/silc/util.c Tue May 13 13:13:57 2014 +0200
@@ -68,26 +68,6 @@
return FALSE;
}
-/* there is no fstat alternative for GStatBuf */
-static int g_fstat(int fd, GStatBuf *st)
-{
- struct stat sst;
- int ret;
-
- g_return_val_if_fail(st != NULL, -1);
-
- ret = fstat(fd, &sst);
- if (ret != 0)
- return ret;
-
- memset(st, 0, sizeof(GStatBuf));
- /* only these two are used here */
- st->st_uid = sst.st_uid;
- st->st_mode = sst.st_mode;
-
- return 0;
-}
-
/* This checks stats for various SILC files and directories. First it
checks if ~/.silc directory exist and is owned by the correct user. If
it doesn't exist, it will create the directory. After that it checks if
@@ -213,7 +193,7 @@
#endif
if ((fd = g_open(file_private_key, O_RDONLY, 0)) != -1) {
- if ((g_fstat(fd, &st)) == -1) {
+ if (_purple_fstat(fd, &st) == -1) {
purple_debug_error("silc", "Couldn't stat '%s' private key, error: %s\n",
file_private_key, g_strerror(errno));
close(fd);
@@ -235,7 +215,7 @@
}
if ((fd = g_open(file_private_key, O_RDONLY, 0)) != -1) {
- if ((g_fstat(fd, &st)) == -1) {
+ if (_purple_fstat(fd, &st) == -1) {
purple_debug_error("silc", "Couldn't stat '%s' private key, error: %s\n",
file_private_key, g_strerror(errno));
close(fd);
--- a/libpurple/util.c Tue May 13 12:02:54 2014 +0200
+++ b/libpurple/util.c Tue May 13 13:13:57 2014 +0200
@@ -4961,6 +4961,22 @@
return g_strdup(hash2);
}
+int
+_purple_fstat(int fd, GStatBuf *st)
+{
+ int ret;
+
+ g_return_val_if_fail(st != NULL, -1);
+
+#ifdef _WIN32
+ ret = _fstat(fd, st);
+#else
+ ret = fstat(fd, st);
+#endif
+
+ return ret;
+}
+
#if 0
/* Temporarily removed - re-add this when you need ini file support. */
@@ -4975,7 +4991,7 @@
const gchar *header = "[default]\n\n";
int header_len = strlen(header);
int fd;
- struct stat st;
+ GStatBuf st;
gsize file_size, buff_size;
gchar *buff;
GError *error = NULL;
@@ -4993,7 +5009,7 @@
return FALSE;
}
- if (fstat(fd, &st) != 0) {
+ if (_purple_fstat(fd, &st) != 0) {
purple_debug_error("util", "Failed to fstat ini file %s", file);
return FALSE;
}