source: trunk/libtransmission/platform.c @ 346

Last change on this file since 346 was 310, checked in by joshe, 15 years ago

Add support to libtransmission and the MacOS X GUI for saving private copies of

torrent files.

  • Property svn:keywords set to Date Rev Author Id
File size: 5.2 KB
Line 
1/******************************************************************************
2 * $Id: platform.c 310 2006-06-09 19:53:35Z titer $
3 *
4 * Copyright (c) 2005 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#ifdef SYS_BEOS
26  #include <fs_info.h>
27  #include <FindDirectory.h>
28#endif
29#include <sys/types.h>
30#include <dirent.h>
31
32#include "transmission.h"
33
34static void
35tr_migrateResume( const char *oldDirectory, const char *newDirectory )
36{
37    DIR * dirh;
38    struct dirent * dirp;
39    char oldFile[MAX_PATH_LENGTH];
40    char newFile[MAX_PATH_LENGTH];
41
42    if( ( dirh = opendir( oldDirectory ) ) )
43    {
44        while( ( dirp = readdir( dirh ) ) )
45        {
46            if( strncmp( "resume.", dirp->d_name, 7 ) )
47            {
48                continue;
49            }
50            snprintf( oldFile, MAX_PATH_LENGTH, "%s/%s",
51                      oldDirectory, dirp->d_name );
52            snprintf( newFile, MAX_PATH_LENGTH, "%s/%s",
53                      newDirectory, dirp->d_name );
54            rename( oldFile, newFile );
55        }
56
57        closedir( dirh );
58    }
59}
60
61char * tr_getPrefsDirectory()
62{
63    static char prefsDirectory[MAX_PATH_LENGTH];
64    static int  init = 0;
65
66    if( init )
67    {
68        return prefsDirectory;
69    }
70
71#ifdef SYS_BEOS
72        find_directory( B_USER_SETTINGS_DIRECTORY, dev_for_path("/boot"),
73                        true, prefsDirectory, MAX_PATH_LENGTH );
74        strcat( prefsDirectory, "/Transmission" );
75#elif defined( SYS_DARWIN )
76    snprintf( prefsDirectory, MAX_PATH_LENGTH,
77              "%s/Library/Caches/Transmission", getenv( "HOME" ) );
78#else
79    snprintf( prefsDirectory, MAX_PATH_LENGTH, "%s/.transmission",
80              getenv( "HOME" ) );
81#endif
82
83    tr_mkdir( prefsDirectory );
84    init = 1;
85
86#ifdef SYS_DARWIN
87    char oldDirectory[MAX_PATH_LENGTH];
88    snprintf( oldDirectory, MAX_PATH_LENGTH, "%s/.transmission",
89              getenv( "HOME" ) );
90    tr_migrateResume( oldDirectory, prefsDirectory );
91    rmdir( oldDirectory );
92#endif
93
94    return prefsDirectory;
95}
96
97char * tr_getCacheDirectory()
98{
99    static char cacheDirectory[MAX_PATH_LENGTH];
100    static int  init = 0;
101
102    if( init )
103    {
104        return cacheDirectory;
105    }
106
107#ifdef SYS_BEOS
108    /* XXX hey Bryan, is this fine with you? */
109    snprintf( cacheDirectory, MAX_PATH_LENGTH, "%s/Cache",
110              tr_getPrefsDirectory() );
111#elif defined( SYS_DARWIN )
112    snprintf( cacheDirectory, MAX_PATH_LENGTH, "%s",
113              tr_getPrefsDirectory() );
114#else
115    snprintf( cacheDirectory, MAX_PATH_LENGTH, "%s/cache",
116              tr_getPrefsDirectory() );
117#endif
118
119    tr_mkdir( cacheDirectory );
120    init = 1;
121
122    if( strcmp( tr_getPrefsDirectory(), cacheDirectory ) )
123    {
124        tr_migrateResume( tr_getPrefsDirectory(), cacheDirectory );
125    }
126
127    return cacheDirectory;
128}
129
130char * tr_getTorrentsDirectory()
131{
132    static char torrentsDirectory[MAX_PATH_LENGTH];
133    static int  init = 0;
134
135    if( init )
136    {
137        return torrentsDirectory;
138    }
139
140#ifdef SYS_BEOS
141    /* XXX hey Bryan, is this fine with you? */
142    snprintf( torrentsDirectory, MAX_PATH_LENGTH, "%s/Torrents",
143              tr_getPrefsDirectory() );
144#elif defined( SYS_DARWIN )
145    snprintf( torrentsDirectory, MAX_PATH_LENGTH,
146              "%s/Library/Application Support/Transmission/Torrents",
147              getenv( "HOME" ) );
148#else
149    snprintf( torrentsDirectory, MAX_PATH_LENGTH, "%s/torrents",
150              tr_getPrefsDirectory() );
151#endif
152
153    tr_mkdir( torrentsDirectory );
154    init = 1;
155
156    return torrentsDirectory;
157}
158
159void tr_threadCreate( tr_thread_t * t, void (*func)(void *), void * arg )
160{
161#ifdef SYS_BEOS
162    *t = spawn_thread( (void *) func, "torrent-tx", B_NORMAL_PRIORITY, arg );
163    resume_thread( *t );
164#else
165    pthread_create( t, NULL, (void *) func, arg );
166#endif
167}
168
169void tr_threadJoin( tr_thread_t * t )
170{
171#ifdef SYS_BEOS
172    long exit;
173    wait_for_thread( *t, &exit );
174#else
175    pthread_join( *t, NULL );
176#endif
177}
178
179void tr_lockInit( tr_lock_t * l )
180{
181#ifdef SYS_BEOS
182    *l = create_sem( 1, "" );
183#else
184    pthread_mutex_init( l, NULL );
185#endif
186}
187
188void tr_lockClose( tr_lock_t * l )
189{
190#ifdef SYS_BEOS
191    delete_sem( *l );
192#else
193    pthread_mutex_destroy( l );
194#endif
195}
196
Note: See TracBrowser for help on using the repository browser.