Changeset 5167 for trunk/gtk/conf.c


Ignore:
Timestamp:
Feb 28, 2008, 7:06:23 PM (14 years ago)
Author:
charles
Message:

add win32/posix wrapper function tr_lockfile() to libT and use it in daemon/gtk.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gtk/conf.c

    r5127 r5167  
    2929#include <string.h>
    3030
    31 #include <fcntl.h>
    32 #include <sys/types.h>
    33 #include <sys/stat.h>
    3431#include <unistd.h>
    3532
     
    4037#include <libtransmission/transmission.h>
    4138#include <libtransmission/bencode.h>
     39#include <libtransmission/platform.h>
    4240
    4341#include "conf.h"
     
    7371
    7472/* 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;
     73static gboolean
     74lockfile(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;
    11694}
    11795
     
    135113{
    136114    char * path = getLockFilename( );
    137     int fd = lockfile( path, errstr );
    138     if( fd >= 0 )
     115    const gboolean didLock = lockfile( path, errstr );
     116    if( didLock )
    139117        gl_lockpath = g_strdup( path );
    140118    g_atexit( cf_removelocks );
    141119    g_free( path );
    142     return fd >= 0;
     120    return didLock;
    143121}
    144122
Note: See TracChangeset for help on using the changeset viewer.