2006-07-16 19:39:23 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (c) 2005-2006 Transmission authors and contributors
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2007-07-13 00:15:45 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2007-07-14 16:29:21 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2007-07-12 17:51:45 +00:00
|
|
|
#include <unistd.h>
|
2007-07-14 16:29:21 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
|
|
|
|
#define TR_MAX_OPEN_FILES 16 /* That is, real files, not sockets */
|
|
|
|
#define TR_RESERVED_FDS 16 /* Number of sockets reserved for
|
|
|
|
connections to trackers */
|
|
|
|
|
2007-01-21 07:16:18 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* Structures
|
|
|
|
**********************************************************************/
|
2006-07-16 19:39:23 +00:00
|
|
|
typedef struct tr_openFile_s
|
|
|
|
{
|
2007-01-14 12:00:21 +00:00
|
|
|
char folder[MAX_PATH_LENGTH];
|
|
|
|
char name[MAX_PATH_LENGTH];
|
2006-07-16 19:39:23 +00:00
|
|
|
int file;
|
2007-01-14 12:00:21 +00:00
|
|
|
int write;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
#define STATUS_INVALID 1
|
|
|
|
#define STATUS_UNUSED 2
|
|
|
|
#define STATUS_USED 4
|
|
|
|
#define STATUS_CLOSING 8
|
|
|
|
int status;
|
|
|
|
|
|
|
|
uint64_t date;
|
2007-01-21 07:16:18 +00:00
|
|
|
}
|
|
|
|
tr_openFile_t;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-01-21 07:16:18 +00:00
|
|
|
typedef struct tr_fd_s
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
tr_lock_t lock;
|
2007-01-14 12:00:21 +00:00
|
|
|
tr_cond_t cond;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
int reserved;
|
|
|
|
|
|
|
|
int normal;
|
|
|
|
int normalMax;
|
|
|
|
|
|
|
|
tr_openFile_t open[TR_MAX_OPEN_FILES];
|
2007-01-21 07:16:18 +00:00
|
|
|
}
|
|
|
|
tr_fd_t;
|
|
|
|
|
|
|
|
static tr_fd_t * gFd = NULL;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-01-14 12:00:21 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* Local prototypes
|
|
|
|
**********************************************************************/
|
2007-06-09 15:36:46 +00:00
|
|
|
static int OpenFile( int i, const char * folder, const char * name, int write );
|
2007-01-21 07:16:18 +00:00
|
|
|
static void CloseFile( int i );
|
2007-01-14 12:00:21 +00:00
|
|
|
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* tr_fdInit
|
|
|
|
**********************************************************************/
|
2007-01-21 07:16:18 +00:00
|
|
|
void tr_fdInit()
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
int i, j, s[4096];
|
|
|
|
|
2007-01-21 07:16:18 +00:00
|
|
|
if( gFd )
|
|
|
|
{
|
|
|
|
tr_err( "tr_fdInit was called before!" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-06-09 15:36:46 +00:00
|
|
|
gFd = calloc( 1, sizeof( tr_fd_t ) );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-01-14 12:00:21 +00:00
|
|
|
/* Init lock and cond */
|
2007-01-21 07:16:18 +00:00
|
|
|
tr_lockInit( &gFd->lock );
|
|
|
|
tr_condInit( &gFd->cond );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
/* Detect the maximum number of open files or sockets */
|
|
|
|
for( i = 0; i < 4096; i++ )
|
|
|
|
{
|
|
|
|
if( ( s[i] = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for( j = 0; j < i; j++ )
|
|
|
|
{
|
2007-01-21 19:42:11 +00:00
|
|
|
#ifdef BEOS_NETSERVER
|
|
|
|
closesocket( s[j] );
|
|
|
|
#else
|
|
|
|
close( s[j] );
|
|
|
|
#endif
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_dbg( "%d usable file descriptors", i );
|
|
|
|
|
2007-01-21 07:16:18 +00:00
|
|
|
gFd->reserved = 0;
|
|
|
|
gFd->normal = 0;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-01-21 07:16:18 +00:00
|
|
|
gFd->normalMax = i - TR_RESERVED_FDS - 10;
|
2006-07-16 19:39:23 +00:00
|
|
|
/* To be safe, in case the UI needs to write a preferences file
|
|
|
|
or something */
|
|
|
|
|
|
|
|
for( i = 0; i < TR_MAX_OPEN_FILES; i++ )
|
|
|
|
{
|
2007-01-21 07:16:18 +00:00
|
|
|
gFd->open[i].status = STATUS_INVALID;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* tr_fdFileOpen
|
|
|
|
**********************************************************************/
|
2007-06-09 15:36:46 +00:00
|
|
|
int tr_fdFileOpen( const char * folder, const char * name, int write )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-01-14 12:00:21 +00:00
|
|
|
int i, winner, ret;
|
2006-07-16 19:39:23 +00:00
|
|
|
uint64_t date;
|
|
|
|
|
2007-01-21 07:16:18 +00:00
|
|
|
tr_lockLock( &gFd->lock );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
/* Is it already open? */
|
|
|
|
for( i = 0; i < TR_MAX_OPEN_FILES; i++ )
|
|
|
|
{
|
2007-01-21 07:16:18 +00:00
|
|
|
if( gFd->open[i].status & STATUS_INVALID ||
|
|
|
|
strcmp( folder, gFd->open[i].folder ) ||
|
|
|
|
strcmp( name, gFd->open[i].name ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-01-14 12:00:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-01-21 07:16:18 +00:00
|
|
|
if( gFd->open[i].status & STATUS_CLOSING )
|
2007-01-14 12:00:21 +00:00
|
|
|
{
|
|
|
|
/* File is being closed by another thread, wait until
|
|
|
|
* it's done before we reopen it */
|
2007-01-21 07:16:18 +00:00
|
|
|
tr_condWait( &gFd->cond, &gFd->lock );
|
2007-01-14 12:00:21 +00:00
|
|
|
i = -1;
|
|
|
|
continue;
|
|
|
|
}
|
2007-01-21 07:16:18 +00:00
|
|
|
if( gFd->open[i].write < write )
|
2007-01-14 12:00:21 +00:00
|
|
|
{
|
|
|
|
/* File is open read-only and needs to be closed then
|
|
|
|
* re-opened read-write */
|
2007-01-21 07:16:18 +00:00
|
|
|
CloseFile( i );
|
2007-01-14 12:00:21 +00:00
|
|
|
continue;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-01-14 12:00:21 +00:00
|
|
|
winner = i;
|
|
|
|
goto done;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Can we open one more file? */
|
|
|
|
for( i = 0; i < TR_MAX_OPEN_FILES; i++ )
|
|
|
|
{
|
2007-01-21 07:16:18 +00:00
|
|
|
if( gFd->open[i].status & STATUS_INVALID )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
winner = i;
|
|
|
|
goto open;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-14 12:00:21 +00:00
|
|
|
/* All slots taken - close the oldest currently unused file */
|
2006-07-16 19:39:23 +00:00
|
|
|
for( ;; )
|
|
|
|
{
|
|
|
|
date = tr_date() + 1;
|
|
|
|
winner = -1;
|
|
|
|
|
|
|
|
for( i = 0; i < TR_MAX_OPEN_FILES; i++ )
|
|
|
|
{
|
2007-01-21 07:16:18 +00:00
|
|
|
if( !( gFd->open[i].status & STATUS_UNUSED ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2007-01-21 07:16:18 +00:00
|
|
|
if( gFd->open[i].date < date )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
winner = i;
|
2007-01-21 07:16:18 +00:00
|
|
|
date = gFd->open[i].date;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( winner >= 0 )
|
|
|
|
{
|
2007-01-21 07:16:18 +00:00
|
|
|
CloseFile( winner );
|
2006-07-16 19:39:23 +00:00
|
|
|
goto open;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* All used! Wait a bit and try again */
|
2007-01-21 07:16:18 +00:00
|
|
|
tr_condWait( &gFd->cond, &gFd->lock );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
open:
|
2007-01-21 07:16:18 +00:00
|
|
|
if( ( ret = OpenFile( winner, folder, name, write ) ) )
|
2007-01-14 12:00:21 +00:00
|
|
|
{
|
2007-01-21 07:16:18 +00:00
|
|
|
tr_lockUnlock( &gFd->lock );
|
2007-01-14 12:00:21 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2007-01-21 07:16:18 +00:00
|
|
|
snprintf( gFd->open[winner].folder, MAX_PATH_LENGTH, "%s", folder );
|
|
|
|
snprintf( gFd->open[winner].name, MAX_PATH_LENGTH, "%s", name );
|
|
|
|
gFd->open[winner].write = write;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
done:
|
2007-01-21 07:16:18 +00:00
|
|
|
gFd->open[winner].status = STATUS_USED;
|
|
|
|
gFd->open[winner].date = tr_date();
|
|
|
|
tr_lockUnlock( &gFd->lock );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-01-21 07:16:18 +00:00
|
|
|
return gFd->open[winner].file;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* tr_fdFileRelease
|
|
|
|
**********************************************************************/
|
2007-01-21 07:16:18 +00:00
|
|
|
void tr_fdFileRelease( int file )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
int i;
|
2007-01-21 07:16:18 +00:00
|
|
|
tr_lockLock( &gFd->lock );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
for( i = 0; i < TR_MAX_OPEN_FILES; i++ )
|
|
|
|
{
|
2007-01-21 07:16:18 +00:00
|
|
|
if( gFd->open[i].file == file )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-01-21 07:16:18 +00:00
|
|
|
gFd->open[i].status = STATUS_UNUSED;
|
2006-07-16 19:39:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-21 07:16:18 +00:00
|
|
|
tr_condSignal( &gFd->cond );
|
|
|
|
tr_lockUnlock( &gFd->lock );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* tr_fdFileClose
|
|
|
|
**********************************************************************/
|
2007-06-09 15:36:46 +00:00
|
|
|
void tr_fdFileClose( const char * folder, const char * name )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2007-01-21 07:16:18 +00:00
|
|
|
tr_lockLock( &gFd->lock );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
for( i = 0; i < TR_MAX_OPEN_FILES; i++ )
|
|
|
|
{
|
2007-01-21 07:16:18 +00:00
|
|
|
if( gFd->open[i].status & STATUS_INVALID )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2007-01-21 07:16:18 +00:00
|
|
|
if( !strcmp( folder, gFd->open[i].folder ) &&
|
|
|
|
!strcmp( name, gFd->open[i].name ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-01-21 07:16:18 +00:00
|
|
|
CloseFile( i );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-21 07:16:18 +00:00
|
|
|
tr_lockUnlock( &gFd->lock );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-01-21 19:42:11 +00:00
|
|
|
|
2007-01-14 12:00:21 +00:00
|
|
|
/***********************************************************************
|
2007-01-21 19:42:11 +00:00
|
|
|
* Sockets
|
2007-01-14 12:00:21 +00:00
|
|
|
**********************************************************************/
|
2007-01-21 19:42:11 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
int socket;
|
|
|
|
int priority;
|
|
|
|
}
|
|
|
|
tr_socket_t;
|
|
|
|
|
|
|
|
/* Remember the priority of every socket we open, so that we can keep
|
|
|
|
* track of how many reserved file descriptors we are using */
|
|
|
|
static tr_socket_t * gSockets = NULL;
|
|
|
|
static int gSocketsSize = 0;
|
|
|
|
static int gSocketsCount = 0;
|
|
|
|
static void SocketSetPriority( int s, int priority )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-01-21 19:42:11 +00:00
|
|
|
if( gSocketsSize < 1 )
|
|
|
|
{
|
|
|
|
gSocketsSize = 256;
|
|
|
|
gSockets = malloc( gSocketsSize * sizeof( tr_socket_t ) );
|
|
|
|
}
|
|
|
|
if( gSocketsSize <= gSocketsCount )
|
|
|
|
{
|
|
|
|
gSocketsSize *= 2;
|
|
|
|
gSockets = realloc( gSockets, gSocketsSize * sizeof( tr_socket_t ) );
|
|
|
|
}
|
|
|
|
gSockets[gSocketsCount].socket = s;
|
|
|
|
gSockets[gSocketsCount].priority = priority;
|
|
|
|
gSocketsCount++;
|
|
|
|
}
|
|
|
|
static int SocketGetPriority( int s )
|
|
|
|
{
|
|
|
|
int i, ret;
|
|
|
|
for( i = 0; i < gSocketsCount; i++ )
|
|
|
|
if( gSockets[i].socket == s )
|
|
|
|
break;
|
|
|
|
if( i >= gSocketsCount )
|
|
|
|
{
|
|
|
|
tr_err( "could not find that socket (%d)!", s );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ret = gSockets[i].priority;
|
|
|
|
gSocketsCount--;
|
|
|
|
memmove( &gSockets[i], &gSockets[i+1],
|
|
|
|
( gSocketsCount - i ) * sizeof( tr_socket_t ) );
|
2007-01-23 17:59:21 +00:00
|
|
|
return ret;
|
2007-01-21 19:42:11 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-01-21 19:42:11 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* tr_fdSocketCreate
|
|
|
|
**********************************************************************/
|
|
|
|
int tr_fdSocketCreate( int type, int priority )
|
|
|
|
{
|
|
|
|
int s = -1;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-01-21 19:42:11 +00:00
|
|
|
tr_lockLock( &gFd->lock );
|
2007-07-21 17:35:47 +00:00
|
|
|
|
|
|
|
if( priority && gFd->reserved >= TR_RESERVED_FDS )
|
|
|
|
priority = FALSE;
|
|
|
|
|
|
|
|
if( priority || ( gFd->normal < gFd->normalMax ) )
|
2007-01-21 19:42:11 +00:00
|
|
|
if( ( s = socket( AF_INET, type, 0 ) ) < 0 )
|
|
|
|
tr_err( "Could not create socket (%s)", strerror( errno ) );
|
2007-07-21 17:35:47 +00:00
|
|
|
|
2007-01-21 19:42:11 +00:00
|
|
|
if( s > -1 )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-01-21 19:42:11 +00:00
|
|
|
SocketSetPriority( s, priority );
|
|
|
|
if( priority )
|
|
|
|
gFd->reserved++;
|
2006-07-16 19:39:23 +00:00
|
|
|
else
|
2007-01-21 19:42:11 +00:00
|
|
|
gFd->normal++;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-01-21 07:16:18 +00:00
|
|
|
tr_lockUnlock( &gFd->lock );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-01-21 19:42:11 +00:00
|
|
|
return s;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-01-21 19:42:11 +00:00
|
|
|
int tr_fdSocketAccept( int b, struct in_addr * addr, in_port_t * port )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-01-21 19:42:11 +00:00
|
|
|
int s = -1;
|
|
|
|
unsigned len;
|
|
|
|
struct sockaddr_in sock;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-01-21 19:42:11 +00:00
|
|
|
tr_lockLock( &gFd->lock );
|
|
|
|
if( gFd->normal < gFd->normalMax )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-01-21 19:42:11 +00:00
|
|
|
len = sizeof( sock );
|
|
|
|
s = accept( b, (struct sockaddr *) &sock, &len );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-01-21 19:42:11 +00:00
|
|
|
if( s > -1 )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-01-21 19:42:11 +00:00
|
|
|
SocketSetPriority( s, 0 );
|
2007-01-28 00:24:41 +00:00
|
|
|
if( NULL != addr )
|
|
|
|
{
|
|
|
|
*addr = sock.sin_addr;
|
|
|
|
}
|
|
|
|
if( NULL != port )
|
|
|
|
{
|
|
|
|
*port = sock.sin_port;
|
|
|
|
}
|
2007-01-21 19:42:11 +00:00
|
|
|
gFd->normal++;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-01-21 19:42:11 +00:00
|
|
|
tr_lockUnlock( &gFd->lock );
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-01-21 19:42:11 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* tr_fdSocketClose
|
|
|
|
**********************************************************************/
|
|
|
|
void tr_fdSocketClose( int s )
|
|
|
|
{
|
|
|
|
tr_lockLock( &gFd->lock );
|
|
|
|
#ifdef BEOS_NETSERVER
|
|
|
|
closesocket( s );
|
|
|
|
#else
|
|
|
|
close( s );
|
|
|
|
#endif
|
|
|
|
if( SocketGetPriority( s ) )
|
|
|
|
gFd->reserved--;
|
|
|
|
else
|
|
|
|
gFd->normal--;
|
2007-01-21 07:16:18 +00:00
|
|
|
tr_lockUnlock( &gFd->lock );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-01-14 12:00:21 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* tr_fdClose
|
|
|
|
**********************************************************************/
|
2007-01-21 07:16:18 +00:00
|
|
|
void tr_fdClose()
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-01-21 07:16:18 +00:00
|
|
|
tr_lockClose( &gFd->lock );
|
|
|
|
tr_condClose( &gFd->cond );
|
|
|
|
free( gFd );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-01-14 12:00:21 +00:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* Local functions
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* CheckFolder
|
|
|
|
***********************************************************************
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
2007-06-09 15:36:46 +00:00
|
|
|
static int OpenFile( int i, const char * folder, const char * name, int write )
|
2007-01-14 12:00:21 +00:00
|
|
|
{
|
2007-01-21 07:16:18 +00:00
|
|
|
tr_openFile_t * file = &gFd->open[i];
|
2007-01-14 12:00:21 +00:00
|
|
|
struct stat sb;
|
2007-06-09 15:36:46 +00:00
|
|
|
char path[MAX_PATH_LENGTH];
|
2007-04-20 02:05:07 +00:00
|
|
|
int ret;
|
2007-01-14 12:00:21 +00:00
|
|
|
|
|
|
|
tr_dbg( "Opening %s in %s (%d)", name, folder, write );
|
|
|
|
|
|
|
|
/* Make sure the parent folder exists */
|
|
|
|
if( stat( folder, &sb ) || !S_ISDIR( sb.st_mode ) )
|
|
|
|
{
|
|
|
|
return TR_ERROR_IO_PARENT;
|
|
|
|
}
|
|
|
|
|
2007-06-18 19:39:52 +00:00
|
|
|
snprintf( path, sizeof(path), "%s" TR_PATH_DELIMITER_STR "%s",
|
|
|
|
folder,
|
|
|
|
name );
|
2007-01-14 12:00:21 +00:00
|
|
|
|
|
|
|
/* Create subfolders, if any */
|
|
|
|
if( write )
|
|
|
|
{
|
|
|
|
char * p = path + strlen( folder ) + 1;
|
|
|
|
char * s;
|
|
|
|
|
|
|
|
while( ( s = strchr( p, '/' ) ) )
|
|
|
|
{
|
|
|
|
*s = '\0';
|
|
|
|
if( stat( path, &sb ) )
|
|
|
|
{
|
|
|
|
if( mkdir( path, 0777 ) )
|
|
|
|
{
|
2007-04-20 02:05:07 +00:00
|
|
|
ret = tr_ioErrorFromErrno();
|
2007-01-19 11:19:20 +00:00
|
|
|
tr_err( "Could not create folder '%s'", path );
|
2007-04-20 02:05:07 +00:00
|
|
|
return ret;
|
2007-01-14 12:00:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if( !S_ISDIR( sb.st_mode ) )
|
|
|
|
{
|
2007-01-19 11:19:20 +00:00
|
|
|
tr_err( "Is not a folder: '%s'", path );
|
2007-01-14 12:00:21 +00:00
|
|
|
return TR_ERROR_IO_OTHER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*s = '/';
|
|
|
|
p = s + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now try to really open the file */
|
2007-07-13 22:22:46 +00:00
|
|
|
errno = 0;
|
2007-01-14 12:00:21 +00:00
|
|
|
file->file = open( path, write ? ( O_RDWR | O_CREAT ) : O_RDONLY, 0666 );
|
2007-07-22 02:24:13 +00:00
|
|
|
if( write && ( file->file < 0 ) )
|
2007-01-14 12:00:21 +00:00
|
|
|
{
|
2007-04-20 02:05:07 +00:00
|
|
|
ret = tr_ioErrorFromErrno();
|
2007-07-13 22:22:46 +00:00
|
|
|
if( errno )
|
|
|
|
tr_err( "Couldn't open %s in %s: %s", name, folder, strerror(errno) );
|
|
|
|
else
|
|
|
|
tr_err( "Couldn't open %s in %s", name, folder );
|
2007-01-14 12:00:21 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* CloseFile
|
|
|
|
***********************************************************************
|
|
|
|
* We first mark it as closing then release the lock while doing so,
|
|
|
|
* because close() may take same time and we don't want to block other
|
|
|
|
* threads.
|
|
|
|
**********************************************************************/
|
2007-01-21 07:16:18 +00:00
|
|
|
static void CloseFile( int i )
|
2007-01-14 12:00:21 +00:00
|
|
|
{
|
2007-01-21 07:16:18 +00:00
|
|
|
tr_openFile_t * file = &gFd->open[i];
|
2007-01-14 12:00:21 +00:00
|
|
|
|
|
|
|
/* If it's already being closed by another thread, just wait till
|
|
|
|
* it is done */
|
|
|
|
while( file->status & STATUS_CLOSING )
|
|
|
|
{
|
2007-01-21 07:16:18 +00:00
|
|
|
tr_condWait( &gFd->cond, &gFd->lock );
|
2007-01-14 12:00:21 +00:00
|
|
|
}
|
|
|
|
if( file->status & STATUS_INVALID )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Nobody is closing it already, so let's do it */
|
|
|
|
if( file->status & STATUS_USED )
|
|
|
|
{
|
|
|
|
tr_err( "CloseFile: closing a file that's being used!" );
|
|
|
|
}
|
|
|
|
tr_dbg( "Closing %s in %s (%d)", file->name, file->folder, file->write );
|
|
|
|
file->status = STATUS_CLOSING;
|
2007-01-21 07:16:18 +00:00
|
|
|
tr_lockUnlock( &gFd->lock );
|
2007-01-14 12:00:21 +00:00
|
|
|
close( file->file );
|
2007-01-21 07:16:18 +00:00
|
|
|
tr_lockLock( &gFd->lock );
|
2007-01-14 12:00:21 +00:00
|
|
|
file->status = STATUS_INVALID;
|
2007-01-21 07:16:18 +00:00
|
|
|
tr_condSignal( &gFd->cond );
|
2007-01-14 12:00:21 +00:00
|
|
|
}
|
|
|
|
|