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-09-25 23:10:34 +00:00
|
|
|
#include <assert.h>
|
2007-07-29 18:11:21 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2007-07-13 00:15:45 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2007-07-12 17:51:45 +00:00
|
|
|
#include <unistd.h>
|
2007-07-14 16:29:21 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
2007-09-25 23:10:34 +00:00
|
|
|
#include <evutil.h>
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
#include "transmission.h"
|
2007-07-31 14:26:44 +00:00
|
|
|
#include "net.h"
|
|
|
|
#include "platform.h"
|
2007-07-30 18:04:10 +00:00
|
|
|
#include "utils.h"
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
#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
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_lock * lock;
|
|
|
|
tr_cond * 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-08-01 00:40:49 +00:00
|
|
|
static int TrOpenFile( int i, const char * folder, const char * name, int write );
|
|
|
|
static void TrCloseFile( int i );
|
2007-01-14 12:00:21 +00:00
|
|
|
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* tr_fdInit
|
|
|
|
**********************************************************************/
|
2007-07-31 14:26:44 +00:00
|
|
|
void tr_fdInit( void )
|
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-07-30 15:27:52 +00:00
|
|
|
gFd->lock = tr_lockNew( );
|
|
|
|
gFd->cond = tr_condNew( );
|
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
|
2007-09-25 23:10:34 +00:00
|
|
|
closesocket( s[j] );
|
2007-01-21 19:42:11 +00:00
|
|
|
#else
|
2007-09-25 23:10:34 +00:00
|
|
|
EVUTIL_CLOSESOCKET( s[j] );
|
2007-01-21 19:42:11 +00:00
|
|
|
#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-07-30 15:27:52 +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-07-30 15:27:52 +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-08-01 00:40:49 +00:00
|
|
|
TrCloseFile( 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-08-01 00:40:49 +00:00
|
|
|
TrCloseFile( winner );
|
2006-07-16 19:39:23 +00:00
|
|
|
goto open;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* All used! Wait a bit and try again */
|
2007-07-30 15:27:52 +00:00
|
|
|
tr_condWait( gFd->cond, gFd->lock );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
open:
|
2007-08-01 00:40:49 +00:00
|
|
|
if( ( ret = TrOpenFile( winner, folder, name, write ) ) )
|
2007-01-14 12:00:21 +00:00
|
|
|
{
|
2007-07-30 15:27:52 +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();
|
2007-07-30 15:27:52 +00:00
|
|
|
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-07-30 15:27:52 +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-07-30 15:27:52 +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-07-30 15:27:52 +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-08-01 00:40:49 +00:00
|
|
|
TrCloseFile( i );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-30 15:27:52 +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-07-30 15:27:52 +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 )
|
2007-08-02 21:58:34 +00:00
|
|
|
tr_err( "Couldn't create socket (%s)", strerror( sockerrno ) );
|
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-07-30 15:27:52 +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-09-25 23:10:34 +00:00
|
|
|
int
|
|
|
|
tr_fdSocketAccept( int b, struct in_addr * addr, tr_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-09-25 23:10:34 +00:00
|
|
|
assert( addr != NULL );
|
|
|
|
assert( port != NULL );
|
|
|
|
|
2007-07-30 15:27:52 +00:00
|
|
|
tr_lockLock( gFd->lock );
|
2007-01-21 19:42:11 +00:00
|
|
|
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-09-25 23:10:34 +00:00
|
|
|
*addr = sock.sin_addr;
|
|
|
|
*port = sock.sin_port;
|
2007-01-21 19:42:11 +00:00
|
|
|
gFd->normal++;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-07-30 15:27:52 +00:00
|
|
|
tr_lockUnlock( gFd->lock );
|
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
|
|
|
/***********************************************************************
|
|
|
|
* tr_fdSocketClose
|
|
|
|
**********************************************************************/
|
|
|
|
void tr_fdSocketClose( int s )
|
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
if( s >= 0 )
|
|
|
|
{
|
|
|
|
tr_lockLock( gFd->lock );
|
2007-01-21 19:42:11 +00:00
|
|
|
#ifdef BEOS_NETSERVER
|
2007-09-20 16:32:01 +00:00
|
|
|
closesocket( s );
|
2007-01-21 19:42:11 +00:00
|
|
|
#else
|
2007-09-25 23:10:34 +00:00
|
|
|
EVUTIL_CLOSESOCKET( s );
|
2007-01-21 19:42:11 +00:00
|
|
|
#endif
|
2007-09-20 16:32:01 +00:00
|
|
|
if( SocketGetPriority( s ) )
|
|
|
|
gFd->reserved--;
|
|
|
|
else
|
|
|
|
gFd->normal--;
|
|
|
|
tr_lockUnlock( gFd->lock );
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-01-14 12:00:21 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* tr_fdClose
|
|
|
|
**********************************************************************/
|
2007-07-31 14:26:44 +00:00
|
|
|
void tr_fdClose( void )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-07-30 15:27:52 +00:00
|
|
|
tr_lockFree( gFd->lock );
|
|
|
|
tr_condFree( gFd->cond );
|
2007-01-21 07:16:18 +00:00
|
|
|
free( gFd );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-01-14 12:00:21 +00:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* Local functions
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* CheckFolder
|
|
|
|
***********************************************************************
|
|
|
|
*
|
|
|
|
**********************************************************************/
|
2007-08-01 00:40:49 +00:00
|
|
|
static int TrOpenFile( 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-08-02 23:33:40 +00:00
|
|
|
int flags;
|
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;
|
|
|
|
|
2007-08-02 23:33:40 +00:00
|
|
|
while( ( s = strchr( p, TR_PATH_DELIMITER ) ) )
|
2007-01-14 12:00:21 +00:00
|
|
|
{
|
|
|
|
*s = '\0';
|
|
|
|
if( stat( path, &sb ) )
|
|
|
|
{
|
2007-08-01 00:40:49 +00:00
|
|
|
if( tr_mkdir( path, 0777 ) )
|
2007-01-14 12:00:21 +00:00
|
|
|
{
|
2007-04-20 02:05:07 +00:00
|
|
|
ret = tr_ioErrorFromErrno();
|
2007-08-02 21:58:34 +00:00
|
|
|
tr_err( "Couldn't 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;
|
|
|
|
}
|
|
|
|
}
|
2007-08-02 23:33:40 +00:00
|
|
|
*s = TR_PATH_DELIMITER;
|
2007-01-14 12:00:21 +00:00
|
|
|
p = s + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now try to really open the file */
|
2007-07-13 22:22:46 +00:00
|
|
|
errno = 0;
|
2007-08-02 23:33:40 +00:00
|
|
|
flags = 0;
|
|
|
|
#ifdef WIN32
|
|
|
|
flags |= O_BINARY;
|
|
|
|
#endif
|
|
|
|
flags |= write ? (O_RDWR | O_CREAT) : O_RDONLY;
|
|
|
|
file->file = open( path, flags, 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
2007-08-01 00:40:49 +00:00
|
|
|
* TrCloseFile
|
2007-01-14 12:00:21 +00:00
|
|
|
***********************************************************************
|
|
|
|
* 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-08-01 00:40:49 +00:00
|
|
|
static void TrCloseFile( 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-07-30 15:27:52 +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 )
|
|
|
|
{
|
2007-08-01 00:40:49 +00:00
|
|
|
tr_err( "TrCloseFile: closing a file that's being used!" );
|
2007-01-14 12:00:21 +00:00
|
|
|
}
|
|
|
|
tr_dbg( "Closing %s in %s (%d)", file->name, file->folder, file->write );
|
|
|
|
file->status = STATUS_CLOSING;
|
2007-07-30 15:27:52 +00:00
|
|
|
tr_lockUnlock( gFd->lock );
|
2007-01-14 12:00:21 +00:00
|
|
|
close( file->file );
|
2007-07-30 15:27:52 +00:00
|
|
|
tr_lockLock( gFd->lock );
|
2007-01-14 12:00:21 +00:00
|
|
|
file->status = STATUS_INVALID;
|
2007-07-30 15:27:52 +00:00
|
|
|
tr_condSignal( gFd->cond );
|
2007-01-14 12:00:21 +00:00
|
|
|
}
|
|
|
|
|