Changeset 5167 for trunk/gtk/conf.c
- Timestamp:
- Feb 28, 2008, 7:06:23 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gtk/conf.c
r5127 r5167 29 29 #include <string.h> 30 30 31 #include <fcntl.h>32 #include <sys/types.h>33 #include <sys/stat.h>34 31 #include <unistd.h> 35 32 … … 40 37 #include <libtransmission/transmission.h> 41 38 #include <libtransmission/bencode.h> 39 #include <libtransmission/platform.h> 42 40 43 41 #include "conf.h" … … 73 71 74 72 /* errstr may be NULL, this might be called before GTK is initialized */ 75 static int 76 lockfile(const char *file, char **errstr) 77 { 78 int fd; 79 struct flock lk; 80 81 if( errstr ) 82 *errstr = NULL; 83 84 fd = open( file, O_RDWR | O_CREAT, 0666 ); 85 if( fd < 0 ) 86 { 87 const int savederr = errno; 88 if( errstr ) 89 *errstr = g_strdup_printf( 90 _("Failed to open the file %s for writing:\n%s"), 91 file, g_strerror( errno ) ); 92 errno = savederr; 93 return -1; 94 } 95 96 memset( &lk, 0, sizeof( lk ) ); 97 lk.l_start = 0; 98 lk.l_len = 0; 99 lk.l_type = F_WRLCK; 100 lk.l_whence = SEEK_SET; 101 if( -1 == fcntl( fd, F_SETLK, &lk ) ) 102 { 103 const int savederr = errno; 104 if( errstr ) 105 *errstr = ( errno == EAGAIN ) 106 ? g_strdup_printf( _( "Another copy of %s is already running." ), 107 g_get_application_name( ) ) 108 : g_strdup_printf( _( "Failed to lock the file %s:\n%s" ), 109 file, g_strerror( errno ) ); 110 close( fd ); 111 errno = savederr; 112 return -1; 113 } 114 115 return fd; 73 static gboolean 74 lockfile(const char * filename, char **errstr) 75 { 76 const int state = tr_lockfile( filename ); 77 const gboolean success = state == TR_LOCKFILE_SUCCESS; 78 79 if( errstr ) switch( state ) { 80 case TR_LOCKFILE_EOPEN: 81 *errstr = g_strdup_printf( _("Failed to open lockfile %s: %s"), 82 filename, g_strerror( errno ) ); 83 break; 84 case TR_LOCKFILE_ELOCK: 85 *errstr = g_strdup_printf( _( "%s is already running." ), 86 g_get_application_name( ) ); 87 break; 88 case TR_LOCKFILE_SUCCESS: 89 *errstr = NULL; 90 break; 91 } 92 93 return success; 116 94 } 117 95 … … 135 113 { 136 114 char * path = getLockFilename( ); 137 int fd= lockfile( path, errstr );138 if( fd >= 0)115 const gboolean didLock = lockfile( path, errstr ); 116 if( didLock ) 139 117 gl_lockpath = g_strdup( path ); 140 118 g_atexit( cf_removelocks ); 141 119 g_free( path ); 142 return fd >= 0;120 return didLock; 143 121 } 144 122
Note: See TracChangeset
for help on using the changeset viewer.