~ubuntu-branches/ubuntu/raring/transmission/raring

« back to all changes in this revision

Viewing changes to .pc/0114-dont_move_file_if_src_and_dest_are_the_same.patch/libtransmission/utils.h

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Klimonda
  • Date: 2011-03-22 18:59:19 UTC
  • Revision ID: james.westby@ubuntu.com-20110322185919-ceizoba779xccx1r
Tags: 2.13-0ubuntu7
* debian/patches/0108-sync_web_interface_turtle_mode_with_gui.patch
  - Keep GUI and web interface in sync when toggling speed limit mode.
    (LP: #727629)
* debian/patches/0109-fix_crash_on_missing_script.patch (LP: #710003)
  - Fix crash when the configured script is not found.
* debian/patches/0110-dont_truncate_string.patch:
  - Don't truncace string when transmission-edit -r option is used. 
* debian/patches/0111-call_guessPacketOverhead_for_reads.patch
  - Fix peer-io code so packet overhead is calculated for socket reads.
* 0112-fix_check_for_result_of_tr_torrentGetMetadataPercent.patch:
  - tr_torrentGetMetadataPercent() may return NULL; fix the check in
    torrent-magnet.c to handle it.
* debian/patches/0113-fix_support_for_ipv6_trackers.patch:
  - Fix support for IPv6-only trackers.
* debian/patches/0114-dont_move_file_if_src_and_dest_are_the_same.patch:
  - Don't move files and directories if the destination path is the same
    as origin.
* debian/patches/0115-fix_tall_argument.patch:
  - Fix the "-tall" argument in transmission-remote.
* debian/patches/0116-fix_segfault_in_gtk_client.patch:
  - Fix crash in the Gtk+ client when adding many torrents remotely.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file Copyright (C) 2009-2010 Mnemosyne LLC
 
3
 *
 
4
 * This file is licensed by the GPL version 2.  Works owned by the
 
5
 * Transmission project are granted a special exemption to clause 2(b)
 
6
 * so that the bulk of its code can remain under the MIT license.
 
7
 * This exemption does not extend to derived works not owned by
 
8
 * the Transmission project.
 
9
 *
 
10
 * $Id: utils.h 11490 2010-12-08 14:57:34Z charles $
 
11
 */
 
12
 
 
13
#ifndef TR_UTILS_H
 
14
#define TR_UTILS_H 1
 
15
 
 
16
#include <inttypes.h>
 
17
#include <stddef.h> /* size_t */
 
18
#include <stdio.h> /* FILE* */
 
19
#include <string.h> /* memcpy()* */
 
20
#include <stdlib.h> /* malloc() */
 
21
#include <time.h> /* time_t */
 
22
 
 
23
#ifdef __cplusplus
 
24
extern "C" {
 
25
#endif
 
26
 
 
27
/***
 
28
****
 
29
***/
 
30
 
 
31
/**
 
32
 * @addtogroup utils Utilities
 
33
 * @{
 
34
 */
 
35
 
 
36
#ifndef FALSE
 
37
 #define FALSE 0
 
38
#endif
 
39
 
 
40
#ifndef TRUE
 
41
 #define TRUE 1
 
42
#endif
 
43
 
 
44
#ifndef UNUSED
 
45
 #ifdef __GNUC__
 
46
  #define UNUSED __attribute__ ( ( unused ) )
 
47
 #else
 
48
  #define UNUSED
 
49
 #endif
 
50
#endif
 
51
 
 
52
#ifndef TR_GNUC_PRINTF
 
53
 #ifdef __GNUC__
 
54
  #define TR_GNUC_PRINTF( fmt, args ) __attribute__ ( ( format ( printf, fmt, args ) ) )
 
55
 #else
 
56
  #define TR_GNUC_PRINTF( fmt, args )
 
57
 #endif
 
58
#endif
 
59
 
 
60
#ifndef TR_GNUC_NONNULL
 
61
 #ifdef __GNUC__
 
62
  #define TR_GNUC_NONNULL( ... ) __attribute__((nonnull (__VA_ARGS__)))
 
63
 #else
 
64
  #define TR_GNUC_NONNULL( ... )
 
65
 #endif
 
66
#endif
 
67
 
 
68
#ifndef TR_GNUC_NULL_TERMINATED
 
69
 #if __GNUC__ > 4 || ( __GNUC__ == 4 && __GNUC_MINOR__ >= 3 )
 
70
  #define TR_GNUC_NULL_TERMINATED __attribute__ ( ( __sentinel__ ) )
 
71
  #define TR_GNUC_HOT __attribute ( ( hot ) )
 
72
 #else
 
73
  #define TR_GNUC_NULL_TERMINATED
 
74
  #define TR_GNUC_HOT
 
75
 #endif
 
76
#endif
 
77
 
 
78
#if __GNUC__ > 2 || ( __GNUC__ == 2 && __GNUC_MINOR__ >= 96 )
 
79
 #define TR_GNUC_PURE __attribute__ ( ( __pure__ ) )
 
80
 #define TR_GNUC_MALLOC __attribute__ ( ( __malloc__ ) )
 
81
#else
 
82
 #define TR_GNUC_PURE
 
83
 #define TR_GNUC_MALLOC
 
84
#endif
 
85
 
 
86
 
 
87
/***
 
88
****
 
89
***/
 
90
 
 
91
const char * tr_strip_positional_args( const char * fmt );
 
92
 
 
93
#if !defined( _ )
 
94
 #if defined( HAVE_LIBINTL_H ) && !defined( SYS_DARWIN )
 
95
  #include <libintl.h>
 
96
  #define _( a ) gettext ( a )
 
97
 #else
 
98
  #define _( a ) ( a )
 
99
 #endif
 
100
#endif
 
101
 
 
102
/* #define DISABLE_GETTEXT */
 
103
#ifndef DISABLE_GETTEXT
 
104
 #if defined(WIN32) || defined(TR_EMBEDDED)
 
105
   #define DISABLE_GETTEXT
 
106
 #endif
 
107
#endif
 
108
#ifdef DISABLE_GETTEXT
 
109
 #undef _
 
110
 #define _( a ) tr_strip_positional_args( a )
 
111
#endif
 
112
 
 
113
/****
 
114
*****
 
115
****/
 
116
 
 
117
#define TR_MAX_MSG_LOG 10000
 
118
 
 
119
extern tr_msg_level messageLevel;
 
120
 
 
121
static inline tr_bool tr_msgLoggingIsActive( tr_msg_level level )
 
122
{
 
123
    return messageLevel >= level;
 
124
}
 
125
 
 
126
void tr_msg( const char * file, int line,
 
127
             tr_msg_level level,
 
128
             const char * torrent,
 
129
             const char * fmt, ... ) TR_GNUC_PRINTF( 5, 6 );
 
130
 
 
131
#define tr_nerr( n, ... ) \
 
132
    do { \
 
133
        if( tr_msgLoggingIsActive( TR_MSG_ERR ) ) \
 
134
            tr_msg( __FILE__, __LINE__, TR_MSG_ERR, n, __VA_ARGS__ ); \
 
135
    } while( 0 )
 
136
 
 
137
#define tr_ninf( n, ... ) \
 
138
    do { \
 
139
        if( tr_msgLoggingIsActive( TR_MSG_INF) ) \
 
140
            tr_msg( __FILE__, __LINE__, TR_MSG_INF, n, __VA_ARGS__ ); \
 
141
    } while( 0 )
 
142
 
 
143
#define tr_ndbg( n, ... ) \
 
144
    do { \
 
145
        if( tr_msgLoggingIsActive( TR_MSG_DBG) ) \
 
146
            tr_msg( __FILE__, __LINE__, TR_MSG_DBG, n, __VA_ARGS__ ); \
 
147
    } while( 0 )
 
148
 
 
149
#define tr_torerr( tor, ... ) \
 
150
    do { \
 
151
        if( tr_msgLoggingIsActive( TR_MSG_ERR ) ) \
 
152
            tr_msg( __FILE__, __LINE__, TR_MSG_ERR, tor->info.name, __VA_ARGS__ ); \
 
153
    } while( 0 )
 
154
 
 
155
#define tr_torinf( tor, ... ) \
 
156
    do { \
 
157
        if( tr_msgLoggingIsActive( TR_MSG_INF ) ) \
 
158
            tr_msg( __FILE__, __LINE__, TR_MSG_INF, tor->info.name, __VA_ARGS__ ); \
 
159
    } while( 0 )
 
160
 
 
161
#define tr_tordbg( tor, ... ) \
 
162
    do { \
 
163
        if( tr_msgLoggingIsActive( TR_MSG_DBG ) ) \
 
164
            tr_msg( __FILE__, __LINE__, TR_MSG_DBG, tor->info.name, __VA_ARGS__ ); \
 
165
    } while( 0 )
 
166
 
 
167
#define tr_err( ... ) \
 
168
    do { \
 
169
        if( tr_msgLoggingIsActive( TR_MSG_ERR ) ) \
 
170
            tr_msg( __FILE__, __LINE__, TR_MSG_ERR, NULL, __VA_ARGS__ ); \
 
171
    } while( 0 )
 
172
 
 
173
#define tr_inf( ... ) \
 
174
    do { \
 
175
        if( tr_msgLoggingIsActive( TR_MSG_INF ) ) \
 
176
            tr_msg( __FILE__, __LINE__, TR_MSG_INF, NULL, __VA_ARGS__ ); \
 
177
    } while( 0 )
 
178
 
 
179
#define tr_dbg( ... ) \
 
180
    do { \
 
181
        if( tr_msgLoggingIsActive( TR_MSG_DBG ) ) \
 
182
            tr_msg( __FILE__, __LINE__, TR_MSG_DBG, NULL, __VA_ARGS__ ); \
 
183
    } while( 0 )
 
184
 
 
185
 
 
186
 
 
187
FILE*          tr_getLog( void );
 
188
 
 
189
/** @brief return true if deep logging has been enabled by the user; false otherwise */
 
190
tr_bool tr_deepLoggingIsActive( void );
 
191
 
 
192
void           tr_deepLog( const char * file,
 
193
                           int          line,
 
194
                           const char * name,
 
195
                           const char * fmt,
 
196
                           ... ) TR_GNUC_PRINTF( 4, 5 ) TR_GNUC_NONNULL(1,4);
 
197
 
 
198
/** @brief set the buffer with the current time formatted for deep logging. */
 
199
char* tr_getLogTimeStr( char * buf, int buflen ) TR_GNUC_NONNULL(1);
 
200
 
 
201
 
 
202
/**
 
203
 * @brief Rich Salz's classic implementation of shell-style pattern matching for ?, \, [], and * characters.
 
204
 * @return 1 if the pattern matches, 0 if it doesn't, or -1 if an error occured
 
205
 */
 
206
int tr_wildmat( const char * text, const char * pattern ) TR_GNUC_NONNULL(1,2);
 
207
 
 
208
/** @brief Portability wrapper for basename() that uses the system implementation if available */
 
209
char* tr_basename( const char * path ) TR_GNUC_MALLOC;
 
210
 
 
211
/** @brief Portability wrapper for dirname() that uses the system implementation if available */
 
212
char* tr_dirname( const char * path ) TR_GNUC_MALLOC;
 
213
 
 
214
/**
 
215
 * @brief Portability wrapper for mkdir()
 
216
 *
 
217
 * A portability wrapper around mkdir().
 
218
 * On WIN32, the `permissions' argument is unused.
 
219
 *
 
220
 * @return zero on success, or -1 if an error occurred
 
221
 * (in which case errno is set appropriately).
 
222
 */
 
223
int tr_mkdir( const char * path, int permissions ) TR_GNUC_NONNULL(1);
 
224
 
 
225
/**
 
226
 * Like mkdir, but makes parent directories as needed.
 
227
 *
 
228
 * @return zero on success, or -1 if an error occurred
 
229
 * (in which case errno is set appropriately).
 
230
 */
 
231
int tr_mkdirp( const char * path, int permissions ) TR_GNUC_NONNULL(1);
 
232
 
 
233
 
 
234
/**
 
235
 * @brief Loads a file and returns its contents.
 
236
 * On failure, NULL is returned and errno is set.
 
237
 */
 
238
uint8_t* tr_loadFile( const char * filename, size_t * size ) TR_GNUC_MALLOC
 
239
                                                             TR_GNUC_NONNULL(1);
 
240
 
 
241
 
 
242
/** @brief build a filename from a series of elements using the
 
243
           platform's correct directory separator. */
 
244
char* tr_buildPath( const char * first_element, ... ) TR_GNUC_NULL_TERMINATED
 
245
                                                      TR_GNUC_MALLOC;
 
246
 
 
247
struct event;
 
248
 
 
249
/**
 
250
 * @brief Convenience wrapper around timer_add() to have a timer wake up in a number of seconds and microseconds
 
251
 * @param timer
 
252
 * @param seconds
 
253
 * @param microseconds
 
254
 */
 
255
void tr_timerAdd( struct event * timer, int seconds, int microseconds ) TR_GNUC_NONNULL(1);
 
256
 
 
257
/**
 
258
 * @brief Convenience wrapper around timer_add() to have a timer wake up in a number of milliseconds
 
259
 * @param timer
 
260
 * @param milliseconds
 
261
 */
 
262
void tr_timerAddMsec( struct event * timer, int milliseconds ) TR_GNUC_NONNULL(1);
 
263
 
 
264
 
 
265
/** @brief return the current date in milliseconds */
 
266
uint64_t tr_time_msec( void );
 
267
 
 
268
/** @brief sleep the specified number of milliseconds */
 
269
void tr_wait_msec( long int delay_milliseconds );
 
270
 
 
271
/**
 
272
 * @brief make a copy of 'str' whose non-utf8 content has been corrected or stripped
 
273
 * @return a newly-allocated string that must be freed with tr_free()
 
274
 * @param str the string to make a clean copy of
 
275
 * @param len the length of the string to copy.  If -1, the entire string is used.
 
276
 */
 
277
char* tr_utf8clean( const char * str, int len ) TR_GNUC_MALLOC;
 
278
 
 
279
 
 
280
/***
 
281
****
 
282
***/
 
283
 
 
284
/* Sometimes the system defines MAX/MIN, sometimes not.
 
285
   In the latter case, define those here since we will use them */
 
286
#ifndef MAX
 
287
 #define MAX( a, b ) ( ( a ) > ( b ) ? ( a ) : ( b ) )
 
288
#endif
 
289
#ifndef MIN
 
290
 #define MIN( a, b ) ( ( a ) > ( b ) ? ( b ) : ( a ) )
 
291
#endif
 
292
 
 
293
/***
 
294
****
 
295
***/
 
296
 
 
297
/** @brief Portability wrapper around malloc() in which `0' is a safe argument */
 
298
void* tr_malloc( size_t size );
 
299
 
 
300
/** @brief Portability wrapper around calloc() in which `0' is a safe argument */
 
301
void* tr_malloc0( size_t size );
 
302
 
 
303
/** @brief Portability wrapper around free() in which `NULL' is a safe argument */
 
304
void tr_free( void * p );
 
305
 
 
306
/**
 
307
 * @brief make a newly-allocated copy of a chunk of memory
 
308
 * @param src the memory to copy
 
309
 * @param byteCount the number of bytes to copy
 
310
 * @return a newly-allocated copy of `src' that can be freed with tr_free()
 
311
 */
 
312
void* tr_memdup( const void * src, size_t byteCount );
 
313
 
 
314
#define tr_new( struct_type, n_structs )           \
 
315
    ( (struct_type *) tr_malloc ( ( (size_t) sizeof ( struct_type ) ) * ( ( size_t) ( n_structs ) ) ) )
 
316
 
 
317
#define tr_new0( struct_type, n_structs )          \
 
318
    ( (struct_type *) tr_malloc0 ( ( (size_t) sizeof ( struct_type ) ) * ( ( size_t) ( n_structs ) ) ) )
 
319
 
 
320
#define tr_renew( struct_type, mem, n_structs )    \
 
321
    ( (struct_type *) realloc ( ( mem ), ( (size_t) sizeof ( struct_type ) ) * ( ( size_t) ( n_structs ) ) ) )
 
322
 
 
323
void* tr_valloc( size_t bufLen );
 
324
 
 
325
/**
 
326
 * @brief make a newly-allocated copy of a substring
 
327
 * @param in is a void* so that callers can pass in both signed & unsigned without a cast
 
328
 * @param len length of the substring to copy.  if a length less than zero is passed in, strlen( len ) is used
 
329
 * @return a newly-allocated copy of `in' that can be freed with tr_free()
 
330
 */
 
331
char* tr_strndup( const void * in, int len ) TR_GNUC_MALLOC;
 
332
 
 
333
/**
 
334
 * @brief make a newly-allocated copy of a string
 
335
 * @param in is a void* so that callers can pass in both signed & unsigned without a cast
 
336
 * @return a newly-allocated copy of `in' that can be freed with tr_free()
 
337
 */
 
338
char* tr_strdup( const void * in );
 
339
 
 
340
/** @brief similar to bsearch() but returns the index of the lower bound */
 
341
int tr_lowerBound( const void * key,
 
342
                   const void * base,
 
343
                   size_t       nmemb,
 
344
                   size_t       size,
 
345
                   int       (* compar)(const void* key, const void* arrayMember),
 
346
                   tr_bool    * exact_match ) TR_GNUC_HOT TR_GNUC_NONNULL(1,5,6);
 
347
 
 
348
 
 
349
/**
 
350
 * @brief sprintf() a string into a newly-allocated buffer large enough to hold it
 
351
 * @return a newly-allocated string that can be freed with tr_free()
 
352
 */
 
353
char* tr_strdup_printf( const char * fmt, ... ) TR_GNUC_PRINTF( 1, 2 )
 
354
                                                TR_GNUC_MALLOC;
 
355
 
 
356
/**
 
357
 * @brief Translate a block of bytes into base64
 
358
 * @return a newly-allocated string that can be freed with tr_free()
 
359
 */
 
360
char* tr_base64_encode( const void * input,
 
361
                        int          inlen,
 
362
                        int        * outlen ) TR_GNUC_MALLOC;
 
363
 
 
364
/**
 
365
 * @brief Translate a block of bytes from base64 into raw form
 
366
 * @return a newly-allocated string that can be freed with tr_free()
 
367
 */
 
368
char* tr_base64_decode( const void * input,
 
369
                        int          inlen,
 
370
                        int        * outlen ) TR_GNUC_MALLOC;
 
371
 
 
372
/** @brief Portability wrapper for strlcpy() that uses the system implementation if available */
 
373
size_t tr_strlcpy( char * dst, const void * src, size_t siz );
 
374
 
 
375
/** @brief Portability wrapper for snprintf() that uses the system implementation if available */
 
376
int tr_snprintf( char * buf, size_t buflen,
 
377
                 const char * fmt, ... ) TR_GNUC_PRINTF( 3, 4 ) TR_GNUC_NONNULL(1,3);
 
378
 
 
379
/** @brief Convenience wrapper around strerorr() guaranteed to not return NULL
 
380
    @param errno */
 
381
const char* tr_strerror( int );
 
382
 
 
383
/** @brief strips leading and trailing whitspace from a string
 
384
    @return the stripped string */
 
385
char* tr_strstrip( char * str );
 
386
 
 
387
/** @brief Returns true if the string ends with the specified case-insensitive suffix */
 
388
tr_bool tr_str_has_suffix( const char *str, const char *suffix );
 
389
 
 
390
 
 
391
/** @brief Portability wrapper for memmem() that uses the system implementation if available */
 
392
const char* tr_memmem( const char * haystack, size_t haystack_len,
 
393
                       const char * needle, size_t needle_len );
 
394
 
 
395
/** @brief Portability wrapper for strsep() that uses the system implementation if available */
 
396
char* tr_strsep( char ** str, const char * delim );
 
397
 
 
398
/***
 
399
****
 
400
***/
 
401
 
 
402
typedef void ( tr_set_func )( void * element, void * userData );
 
403
 
 
404
/**
 
405
 * @brief find the differences and commonalities in two sorted sets
 
406
 * @param a the first set
 
407
 * @param aCount the number of elements in the set 'a'
 
408
 * @param b the second set
 
409
 * @param bCount the number of elements in the set 'b'
 
410
 * @param compare the sorting method for both sets
 
411
 * @param elementSize the sizeof the element in the two sorted sets
 
412
 * @param in_a called for items in set 'a' but not set 'b'
 
413
 * @param in_b called for items in set 'b' but not set 'a'
 
414
 * @param in_both called for items that are in both sets
 
415
 * @param userData user data passed along to in_a, in_b, and in_both
 
416
 */
 
417
void tr_set_compare( const void * a, size_t aCount,
 
418
                     const void * b, size_t bCount,
 
419
                     int compare( const void * a, const void * b ),
 
420
                     size_t elementSize,
 
421
                     tr_set_func in_a_cb,
 
422
                     tr_set_func in_b_cb,
 
423
                     tr_set_func in_both_cb,
 
424
                     void * userData );
 
425
 
 
426
int compareInt( const void * va, const void * vb );
 
427
 
 
428
void tr_sha1_to_hex( char * out, const uint8_t * sha1 ) TR_GNUC_NONNULL(1,2);
 
429
 
 
430
void tr_hex_to_sha1( uint8_t * out, const char * hex ) TR_GNUC_NONNULL(1,2);
 
431
 
 
432
/** @brief convenience function to determine if an address is an IP address (IPv4 or IPv6) */
 
433
tr_bool tr_addressIsIP( const char * address );
 
434
 
 
435
/** @brief return TRUE if the url is a http or https url that Transmission understands */
 
436
tr_bool tr_urlIsValidTracker( const char * url ) TR_GNUC_NONNULL(1);
 
437
 
 
438
/** @brief return TRUE if the url is a [ http, https, ftp, ftps ] url that Transmission understands */
 
439
tr_bool tr_urlIsValid( const char * url, int url_len ) TR_GNUC_NONNULL(1);
 
440
 
 
441
/** @brief parse a URL into its component parts
 
442
    @return zero on success or an error number if an error occurred */
 
443
int  tr_urlParse( const char * url,
 
444
                  int          url_len,
 
445
                  char      ** setme_scheme,
 
446
                  char      ** setme_host,
 
447
                  int        * setme_port,
 
448
                  char      ** setme_path ) TR_GNUC_NONNULL(1);
 
449
 
 
450
 
 
451
/** @brief return TR_RATIO_NA, TR_RATIO_INF, or a number in [0..1]
 
452
    @return TR_RATIO_NA, TR_RATIO_INF, or a number in [0..1] */
 
453
double tr_getRatio( uint64_t numerator, uint64_t denominator );
 
454
 
 
455
/**
 
456
 * @brief Given a string like "1-4" or "1-4,6,9,14-51", this returns a
 
457
 *        newly-allocated array of all the integers in the set.
 
458
 * @return a newly-allocated array of integers that must be freed with tr_free(),
 
459
 *         or NULL if a fragment of the string can't be parsed.
 
460
 *
 
461
 * For example, "5-8" will return [ 5, 6, 7, 8 ] and setmeCount will be 4.
 
462
 */
 
463
int* tr_parseNumberRange( const char * str,
 
464
                          int str_len,
 
465
                          int * setmeCount ) TR_GNUC_MALLOC TR_GNUC_NONNULL(1);
 
466
 
 
467
 
 
468
/**
 
469
 * @brief truncate a double value at a given number of decimal places.
 
470
 *
 
471
 * this can be used to prevent a printf() call from rounding up:
 
472
 * call with the decimal_places argument equal to the number of
 
473
 * decimal places in the printf()'s precision:
 
474
 *
 
475
 * - printf("%.2f%%",           99.999    ) ==> "100.00%"
 
476
 *
 
477
 * - printf("%.2f%%", tr_truncd(99.999, 2)) ==>  "99.99%"
 
478
 *             ^                        ^
 
479
 *             |   These should match   |
 
480
 *             +------------------------+
 
481
 */
 
482
double tr_truncd( double x, int decimal_places );
 
483
 
 
484
/* return a percent formatted string of either x.xx, xx.x or xxx */
 
485
char* tr_strpercent( char * buf, double x, size_t buflen );
 
486
 
 
487
/**
 
488
 * @param buf the buffer to write the string to
 
489
 * @param buflef buf's size
 
490
 * @param ratio the ratio to convert to a string
 
491
 * @param the string represntation of "infinity"
 
492
 */
 
493
char* tr_strratio( char * buf, size_t buflen, double ratio, const char * infinity ) TR_GNUC_NONNULL(1,4);
 
494
 
 
495
/* return a truncated double as a string */
 
496
char* tr_strtruncd( char * buf, double x, int precision, size_t buflen );
 
497
 
 
498
/** @brief Portability wrapper for localtime_r() that uses the system implementation if available */
 
499
struct tm * tr_localtime_r( const time_t *_clock, struct tm *_result );
 
500
 
 
501
 
 
502
/**
 
503
 * @brief move a file
 
504
 * @return 0 on success; otherwise, return -1 and set errno
 
505
 */
 
506
int tr_moveFile( const char * oldpath, const char * newpath,
 
507
                 tr_bool * renamed ) TR_GNUC_NONNULL(1,2);
 
508
 
 
509
/** @brief convenience function to remove an item from an array */
 
510
void tr_removeElementFromArray( void         * array,
 
511
                                unsigned int   index_to_remove,
 
512
                                size_t         sizeof_element,
 
513
                                size_t         nmemb );
 
514
 
 
515
/***
 
516
****
 
517
***/
 
518
 
 
519
/** @brief Private libtransmission variable that's visible only for inlining in tr_time() */
 
520
extern time_t transmission_now;
 
521
 
 
522
/**
 
523
 * @brief very inexpensive form of time(NULL)
 
524
 * @return the current epoch time in seconds
 
525
 *
 
526
 * This function returns a second counter that is updated once per second.
 
527
 * If something blocks the libtransmission thread for more than a second,
 
528
 * that counter may be thrown off, so this function is not guaranteed
 
529
 * to always be accurate.  However, it is *much* faster when 100% accuracy
 
530
 * isn't needed
 
531
 */
 
532
static inline time_t tr_time( void ) { return transmission_now; }
 
533
 
 
534
/** @brief Private libtransmission function to update tr_time()'s counter */
 
535
static inline void tr_timeUpdate( time_t now ) { transmission_now = now; }
 
536
 
 
537
/** @brief Portability wrapper for realpath() that uses the system implementation if available */
 
538
char* tr_realpath( const char *path, char * resolved_path );
 
539
 
 
540
/***
 
541
****
 
542
***/
 
543
 
 
544
/* example: tr_formatter_size_init( 1024, _("KiB"), _("MiB"), _("GiB"), _("TiB") ); */
 
545
 
 
546
void tr_formatter_size_init( unsigned int kilo, const char * kb, const char * mb,
 
547
                                                const char * gb, const char * tb );
 
548
 
 
549
void tr_formatter_speed_init( unsigned int kilo, const char * kb, const char * mb,
 
550
                                                 const char * gb, const char * tb );
 
551
 
 
552
void tr_formatter_mem_init( unsigned int kilo, const char * kb, const char * mb,
 
553
                                               const char * gb, const char * tb );
 
554
 
 
555
extern unsigned int tr_speed_K;
 
556
extern unsigned int tr_mem_K;
 
557
extern unsigned int tr_size_K;
 
558
 
 
559
/* format a speed from KBps into a user-readable string. */
 
560
char* tr_formatter_speed_KBps( char * buf, double KBps, size_t buflen );
 
561
 
 
562
/* format a memory size from bytes into a user-readable string. */
 
563
char* tr_formatter_mem_B( char * buf, uint64_t bytes, size_t buflen );
 
564
 
 
565
/* format a memory size from MB into a user-readable string. */
 
566
static inline char* tr_formatter_mem_MB( char * buf, double MBps, size_t buflen ) { return tr_formatter_mem_B( buf, MBps * tr_mem_K * tr_mem_K, buflen ); }
 
567
 
 
568
/* format a file size from bytes into a user-readable string. */
 
569
char* tr_formatter_size_B( char * buf, uint64_t bytes, size_t buflen );
 
570
 
 
571
void tr_formatter_get_units( struct tr_benc * dict );
 
572
 
 
573
/***
 
574
****
 
575
***/
 
576
 
 
577
#ifdef __cplusplus
 
578
}
 
579
#endif
 
580
 
 
581
/** @} */
 
582
 
 
583
#endif