Changeset 8929


Ignore:
Timestamp:
Aug 14, 2009, 12:53:08 PM (13 years ago)
Author:
charles
Message:

(trunk) move tr_lockfile from libtransmission/ to gtk/, since the gtk client is the only one who uses it anymore

Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/gtk/conf.c

    r8892 r8929  
    7676/* errstr may be NULL, this might be called before GTK is initialized */
    7777static gboolean
    78 lockfile( const char *        filename,
    79           tr_lockfile_state_t *tr_state,
    80           char **              errstr )
    81 {
    82     const tr_lockfile_state_t state = tr_lockfile( filename );
    83     const gboolean            success = state == TR_LOCKFILE_SUCCESS;
     78lockfile( const char             * filename,
     79          gtr_lockfile_state_t   * tr_state,
     80          char                  ** errstr )
     81{
     82    const gtr_lockfile_state_t state = gtr_lockfile( filename );
     83    const gboolean success = state == GTR_LOCKFILE_SUCCESS;
    8484
    8585    if( errstr ) switch( state )
    8686        {
    87             case TR_LOCKFILE_EOPEN:
     87            case GTR_LOCKFILE_EOPEN:
    8888                *errstr =
    8989                    g_strdup_printf( _( "Couldn't open \"%1$s\": %2$s" ),
     
    9191                break;
    9292
    93             case TR_LOCKFILE_ELOCK:
     93            case GTR_LOCKFILE_ELOCK:
    9494                *errstr = g_strdup_printf( _( "%s is already running." ),
    9595                                          g_get_application_name( ) );
    9696                break;
    9797
    98             case TR_LOCKFILE_SUCCESS:
     98            case GTR_LOCKFILE_SUCCESS:
    9999                *errstr = NULL;
    100100                break;
     
    126126/* errstr may be NULL, this might be called before GTK is initialized */
    127127gboolean
    128 cf_lock( tr_lockfile_state_t *tr_state,
    129          char **              errstr )
     128cf_lock( gtr_lockfile_state_t * tr_state, char ** errstr )
    130129{
    131130    char *         path = getLockFilename( );
  • trunk/gtk/conf.h

    r8047 r8929  
    2828#include <inttypes.h>
    2929#include <libtransmission/transmission.h>
     30#include "util.h" /* gtr_lockfile */
    3031
    3132int64_t          pref_int_get           ( const char * key );
     
    6869                  char **     errstr );
    6970
    70 gboolean cf_lock( tr_lockfile_state_t *tr_state,
    71                   char **              errstr );
     71gboolean cf_lock( gtr_lockfile_state_t  * tr_state,
     72                  char                 ** errstr );
    7273
    7374#endif /* TG_CONF_H */
  • trunk/gtk/main.c

    r8892 r8929  
    326326
    327327int
    328 main( int     argc,
    329       char ** argv )
    330 {
    331     char *              err = NULL;
    332     GSList *            argfiles;
    333     GError *            gerr;
    334     gboolean            didinit = FALSE;
    335     gboolean            didlock = FALSE;
    336     gboolean            showversion = FALSE;
    337     gboolean            startpaused = FALSE;
    338     gboolean            startminimized = FALSE;
    339     const char *        domain = MY_NAME;
    340     char *              configDir = NULL;
    341     tr_lockfile_state_t tr_state;
     328main( int argc, char ** argv )
     329{
     330    char * err = NULL;
     331    GSList * argfiles;
     332    GError * gerr;
     333    gboolean didinit = FALSE;
     334    gboolean didlock = FALSE;
     335    gboolean showversion = FALSE;
     336    gboolean startpaused = FALSE;
     337    gboolean startminimized = FALSE;
     338    const char * domain = MY_NAME;
     339    char * configDir = NULL;
     340    gtr_lockfile_state_t tr_state;
    342341
    343342    GOptionEntry entries[] = {
     
    416415        }
    417416    }
    418     else if( ( !didlock ) && ( tr_state == TR_LOCKFILE_ELOCK ) )
     417    else if( ( !didlock ) && ( tr_state == GTR_LOCKFILE_ELOCK ) )
    419418    {
    420419        /* There's already another copy of Transmission running,
  • trunk/gtk/util.c

    r8889 r8929  
    1717#include <string.h> /* strcmp() */
    1818
     19#include <sys/types.h> /* for gtr_lockfile()'s open() */
     20#include <sys/stat.h> /* for gtr_lockfile()'s open() */
     21#include <fcntl.h> /* for gtr_lockfile()'s open() */
     22
    1923#include <gtk/gtk.h>
    2024#include <glib/gi18n.h>
     
    3741#include "tr-prefs.h"
    3842#include "util.h"
     43
     44gtr_lockfile_state_t
     45gtr_lockfile( const char * filename )
     46{
     47    gtr_lockfile_state_t ret;
     48
     49#ifdef WIN32
     50
     51    HANDLE file = CreateFile( filename,
     52                              GENERIC_READ | GENERIC_WRITE,
     53                              FILE_SHARE_READ | FILE_SHARE_WRITE,
     54                              NULL,
     55                              OPEN_ALWAYS,
     56                              FILE_ATTRIBUTE_NORMAL,
     57                              NULL );
     58    if( file == INVALID_HANDLE_VALUE )
     59        ret = GTR_LOCKFILE_EOPEN;
     60    else if( !LockFile( file, 0, 0, 1, 1 ) )
     61        ret = GTR_LOCKFILE_ELOCK;
     62    else
     63        ret = GTR_LOCKFILE_SUCCESS;
     64
     65#else
     66
     67    int fd = open( filename, O_RDWR | O_CREAT, 0666 );
     68    if( fd < 0 )
     69        ret = GTR_LOCKFILE_EOPEN;
     70    else {
     71        struct flock lk;
     72        memset( &lk, 0,  sizeof( lk ) );
     73        lk.l_start = 0;
     74        lk.l_len = 0;
     75        lk.l_type = F_WRLCK;
     76        lk.l_whence = SEEK_SET;
     77        if( -1 == fcntl( fd, F_SETLK, &lk ) )
     78            ret = GTR_LOCKFILE_ELOCK;
     79        else
     80            ret = GTR_LOCKFILE_SUCCESS;
     81    }
     82
     83#endif
     84
     85    return ret;
     86}
     87
     88/***
     89****
     90***/
     91
    3992
    4093char*
  • trunk/gtk/util.h

    r8889 r8929  
    1515
    1616#include <sys/types.h>
     17#include <gtk/gtk.h>
    1718
    1819/* macro to shut up "unused parameter" warnings */
     
    6263***/
    6364
     65typedef enum
     66{
     67    GTR_LOCKFILE_SUCCESS = 0,
     68    GTR_LOCKFILE_EOPEN,
     69    GTR_LOCKFILE_ELOCK
     70}
     71gtr_lockfile_state_t;
     72
     73gtr_lockfile_state_t gtr_lockfile( const char * filename );
     74
     75/***
     76****
     77***/
     78
    6479void        gtr_open_file( const char * path );
    6580
     
    7388****
    7489***/
    75 
    76 /* GTK-related utilities */
    77 #ifdef GTK_MAJOR_VERSION
    7890
    7991/* backwards-compatible wrapper around g_mkdir_with_parents() */
     
    122134int tr_file_trash_or_remove( const char * filename );
    123135
    124 #endif /* GTK_MAJOR_VERSION */
    125 
    126136#endif /* GTR_UTIL_H */
  • trunk/libtransmission/platform.c

    r8889 r8929  
    664664***/
    665665
    666 tr_lockfile_state_t
    667 tr_lockfile( const char * filename )
    668 {
    669     tr_lockfile_state_t ret;
    670 
    671 #ifdef WIN32
    672 
    673     HANDLE              file = CreateFile(
    674         filename,
    675         GENERIC_READ | GENERIC_WRITE,
    676         FILE_SHARE_READ |
    677         FILE_SHARE_WRITE,
    678         NULL,
    679         OPEN_ALWAYS,
    680         FILE_ATTRIBUTE_NORMAL,
    681         NULL );
    682     if( file == INVALID_HANDLE_VALUE )
    683         ret = TR_LOCKFILE_EOPEN;
    684     else if( !LockFile( file, 0, 0, 1, 1 ) )
    685         ret = TR_LOCKFILE_ELOCK;
    686     else
    687         ret = TR_LOCKFILE_SUCCESS;
    688 
    689 #else
    690 
    691     int fd = open( filename, O_RDWR | O_CREAT, 0666 );
    692     if( fd < 0 )
    693         ret = TR_LOCKFILE_EOPEN;
    694     else
    695     {
    696         struct flock lk;
    697         memset( &lk, 0,  sizeof( lk ) );
    698         lk.l_start = 0;
    699         lk.l_len = 0;
    700         lk.l_type = F_WRLCK;
    701         lk.l_whence = SEEK_SET;
    702         if( -1 == fcntl( fd, F_SETLK, &lk ) )
    703             ret = TR_LOCKFILE_ELOCK;
    704         else
    705             ret = TR_LOCKFILE_SUCCESS;
    706     }
    707 
    708 #endif
    709 
    710     return ret;
    711 }
    712 
    713666#ifdef WIN32
    714667
  • trunk/libtransmission/platform.h

    r8889 r8929  
    6464int                 tr_lockHave( const tr_lock * );
    6565
    66 tr_lockfile_state_t tr_lockfile( const char * filename );
    67 
    6866#ifdef WIN32
    6967void *              mmap( void *ptr,
  • trunk/libtransmission/transmission.h

    r8903 r8929  
    13571357tr_torrent_activity tr_torrentGetActivity( tr_torrent * );
    13581358
    1359 typedef enum
    1360 {
    1361     TR_LOCKFILE_SUCCESS = 0,
    1362     TR_LOCKFILE_EOPEN,
    1363     TR_LOCKFILE_ELOCK
    1364 }
    1365 tr_lockfile_state_t;
    1366 
    13671359enum
    13681360{
Note: See TracChangeset for help on using the changeset viewer.