data/sylpheed-locking.diff (2712B) - raw
1 --- libsylph/procmsg.c.orig 2007-01-16 07:00:10.000000000 +0100 2 +++ libsylph/procmsg.c 2007-08-27 22:48:47.000000000 +0200 3 @@ -24,6 +24,9 @@ 4 #include <stdio.h> 5 #include <stdlib.h> 6 #include <errno.h> 7 +#include <string.h> 8 +#include <fcntl.h> 9 +#include <unistd.h> 10 11 #include "utils.h" 12 #include "procmsg.h" 13 @@ -796,6 +799,20 @@ 14 fclose(fp); 15 } 16 17 +/* play nice with other applications who'd like to read our data files */ 18 +static void procmsg_lock_data_file(int fd) 19 +{ 20 + struct flock lock; 21 + 22 + memset(&lock, 0, sizeof(lock)); 23 + lock.l_start = 0; /* from l_whence */ 24 + lock.l_len = 0; /* to end of file */ 25 + lock.l_type = F_WRLCK; /* write lock */ 26 + lock.l_whence = SEEK_CUR; /* from current position */ 27 + 28 + fcntl(fd, F_SETLKW, &lock); 29 +} 30 + 31 FILE *procmsg_open_data_file(const gchar *file, guint version, 32 DataOpenMode mode, gchar *buf, size_t buf_size) 33 { 34 @@ -805,21 +822,38 @@ 35 g_return_val_if_fail(file != NULL, NULL); 36 37 if (mode == DATA_WRITE) { 38 - if ((fp = g_fopen(file, "wb")) == NULL) { 39 + int fd; 40 + 41 + if ((fd = g_open(file, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) { 42 if (errno == EACCES) { 43 change_file_mode_rw(NULL, file); 44 - if ((fp = g_fopen(file, "wb")) == NULL) { 45 - FILE_OP_ERROR(file, "fopen"); 46 + if ((fd = g_open(file, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) < 0) { 47 + FILE_OP_ERROR(file, "open"); 48 return NULL; 49 } 50 } else { 51 - FILE_OP_ERROR(file, "fopen"); 52 + FILE_OP_ERROR(file, "open"); 53 return NULL; 54 } 55 } 56 + 57 + procmsg_lock_data_file(fd); 58 + 59 + if ((fp = fdopen(fd, "wb")) == NULL) { 60 + close(fd); 61 + FILE_OP_ERROR(file, "fdopen"); 62 + return NULL; 63 + } 64 + 65 if (change_file_mode_rw(fp, file) < 0) 66 FILE_OP_ERROR(file, "chmod"); 67 68 + if (ftruncate(fd, 0) < 0) { 69 + close(fd); 70 + FILE_OP_ERROR(file, "ftruncate"); 71 + return NULL; 72 + } 73 + 74 WRITE_CACHE_DATA_INT(version, fp); 75 return fp; 76 } 77 @@ -859,11 +893,14 @@ 78 change_file_mode_rw(NULL, file); 79 if ((fp = g_fopen(file, "ab")) == NULL) { 80 FILE_OP_ERROR(file, "fopen"); 81 + return NULL; 82 } 83 } else { 84 FILE_OP_ERROR(file, "fopen"); 85 + return NULL; 86 } 87 } 88 + procmsg_lock_data_file(fileno(fp)); 89 } else { 90 /* open with overwrite mode if mark file doesn't exist or 91 version is different */ 92 --- src/main.c.orig 2007-01-12 07:14:58.000000000 +0100 93 +++ src/main.c 2007-08-27 22:50:44.000000000 +0200 94 @@ -368,7 +368,7 @@ 95 } else if (!strncmp(argv[i], "--send", 6)) { 96 cmd.send = TRUE; 97 } else if (!strncmp(argv[i], "--version", 9)) { 98 - puts("Sylpheed version " VERSION); 99 + puts("Sylpheed version " VERSION "+locking"); 100 exit(0); 101 } else if (!strncmp(argv[i], "--status-full", 13)) { 102 const gchar *p = argv[i + 1];