1 | /****************************************************************************** |
---|
2 | * $Id$ |
---|
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 | #ifdef SYS_DARWIN |
---|
30 | #include <sys/types.h> |
---|
31 | #include <dirent.h> |
---|
32 | #endif |
---|
33 | |
---|
34 | #include "transmission.h" |
---|
35 | |
---|
36 | char * tr_getPrefsDirectory() |
---|
37 | { |
---|
38 | static char prefsDirectory[MAX_PATH_LENGTH]; |
---|
39 | static int init = 0; |
---|
40 | |
---|
41 | if( init ) |
---|
42 | { |
---|
43 | return prefsDirectory; |
---|
44 | } |
---|
45 | |
---|
46 | #ifdef SYS_BEOS |
---|
47 | find_directory( B_USER_SETTINGS_DIRECTORY, dev_for_path("/boot"), |
---|
48 | true, prefsDirectory, MAX_PATH_LENGTH ); |
---|
49 | strcat( prefsDirectory, "/Transmission" ); |
---|
50 | #elif defined( SYS_DARWIN ) |
---|
51 | snprintf( prefsDirectory, MAX_PATH_LENGTH, |
---|
52 | "%s/Library/Caches/Transmission", getenv( "HOME" ) ); |
---|
53 | #else |
---|
54 | snprintf( prefsDirectory, MAX_PATH_LENGTH, "%s/.transmission", |
---|
55 | getenv( "HOME" ) ); |
---|
56 | #endif |
---|
57 | |
---|
58 | mkdir( prefsDirectory, 0755 ); |
---|
59 | init = 1; |
---|
60 | |
---|
61 | #ifdef SYS_DARWIN |
---|
62 | DIR * dirh; |
---|
63 | struct dirent * dirp; |
---|
64 | char oldDirectory[MAX_PATH_LENGTH]; |
---|
65 | char oldFile[MAX_PATH_LENGTH]; |
---|
66 | char newFile[MAX_PATH_LENGTH]; |
---|
67 | snprintf( oldDirectory, MAX_PATH_LENGTH, "%s/.transmission", |
---|
68 | getenv( "HOME" ) ); |
---|
69 | if( ( dirh = opendir( oldDirectory ) ) ) |
---|
70 | { |
---|
71 | while( ( dirp = readdir( dirh ) ) ) |
---|
72 | { |
---|
73 | if( !strcmp( ".", dirp->d_name ) || |
---|
74 | !strcmp( "..", dirp->d_name ) ) |
---|
75 | { |
---|
76 | continue; |
---|
77 | } |
---|
78 | snprintf( oldFile, MAX_PATH_LENGTH, "%s/%s", |
---|
79 | oldDirectory, dirp->d_name ); |
---|
80 | snprintf( newFile, MAX_PATH_LENGTH, "%s/%s", |
---|
81 | prefsDirectory, dirp->d_name ); |
---|
82 | rename( oldFile, newFile ); |
---|
83 | } |
---|
84 | |
---|
85 | closedir( dirh ); |
---|
86 | rmdir( oldDirectory ); |
---|
87 | } |
---|
88 | #endif |
---|
89 | |
---|
90 | return prefsDirectory; |
---|
91 | } |
---|
92 | |
---|
93 | void tr_threadCreate( tr_thread_t * t, void (*func)(void *), void * arg ) |
---|
94 | { |
---|
95 | #ifdef SYS_BEOS |
---|
96 | *t = spawn_thread( (void *) func, "torrent-tx", B_NORMAL_PRIORITY, arg ); |
---|
97 | resume_thread( *t ); |
---|
98 | #else |
---|
99 | pthread_create( t, NULL, (void *) func, arg ); |
---|
100 | #endif |
---|
101 | } |
---|
102 | |
---|
103 | void tr_threadJoin( tr_thread_t * t ) |
---|
104 | { |
---|
105 | #ifdef SYS_BEOS |
---|
106 | long exit; |
---|
107 | wait_for_thread( *t, &exit ); |
---|
108 | #else |
---|
109 | pthread_join( *t, NULL ); |
---|
110 | #endif |
---|
111 | } |
---|
112 | |
---|
113 | void tr_lockInit( tr_lock_t * l ) |
---|
114 | { |
---|
115 | #ifdef SYS_BEOS |
---|
116 | *l = create_sem( 1, "" ); |
---|
117 | #else |
---|
118 | pthread_mutex_init( l, NULL ); |
---|
119 | #endif |
---|
120 | } |
---|
121 | |
---|
122 | void tr_lockClose( tr_lock_t * l ) |
---|
123 | { |
---|
124 | #ifdef SYS_BEOS |
---|
125 | delete_sem( *l ); |
---|
126 | #else |
---|
127 | pthread_mutex_destroy( l ); |
---|
128 | #endif |
---|
129 | } |
---|
130 | |
---|