Changeset 1200


Ignore:
Timestamp:
Dec 13, 2006, 9:32:54 PM (15 years ago)
Author:
titer
Message:

Don't try to create the parent folder, and only create sub-folders for read-write access

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/io/libtransmission/fdlimit.c

    r1199 r1200  
    6161 * Local prototypes
    6262 **********************************************************************/
     63static int  ErrorFromErrno();
    6364static void CloseFile( tr_fd_t * f, int i );
    64 static int  CheckCanOpen( char * folder, char * name, int write );
     65static int  CheckFolder( char * folder, char * name, int write );
    6566
    6667
     
    151152
    152153    /* We'll need to open it, make sure that we can */
    153     if( ( ret = CheckCanOpen( folder, name, write ) ) )
     154    if( ( ret = CheckFolder( folder, name, write ) ) )
    154155    {
    155156        tr_err( "Can not open %s in %s (%d)", name, folder, ret );
     
    206207    if( f->open[winner].file < 0 )
    207208    {
    208         tr_err( "Could not open %s in %s (%d)", name, folder, errno );
     209        ret = ErrorFromErrno();
    209210        tr_lockUnlock( &f->lock );
    210         return TR_ERROR_IO_OTHER;
     211        tr_err( "Could not open %s in %s (%d)", name, folder, ret );
     212        return ret;
    211213    }
    212214    snprintf( f->open[winner].folder, MAX_PATH_LENGTH, "%s", folder );
     
    339341 * Local functions
    340342 **********************************************************************/
     343
     344/***********************************************************************
     345 * ErrorFromErrno
     346 **********************************************************************/
     347static int ErrorFromErrno()
     348{
     349    if( errno == EACCES || errno == EROFS )
     350        return TR_ERROR_IO_PERMISSIONS;
     351    return TR_ERROR_IO_OTHER;
     352}
    341353
    342354/***********************************************************************
     
    363375
    364376/***********************************************************************
    365  * CheckCanOpen
     377 * CheckFolder
    366378 ***********************************************************************
    367379 *
    368380 **********************************************************************/
    369 static int CheckCanOpen( char * folder, char * name, int write )
    370 {
    371     return 0;
    372 }
     381static int CheckFolder( char * folder, char * name, int write )
     382{
     383    struct stat sb;
     384    char * path, * p, * s;
     385    int ret = 0;
     386
     387    if( stat( folder, &sb ) || !S_ISDIR( sb.st_mode ) )
     388    {
     389        return TR_ERROR_IO_PARENT;
     390    }
     391
     392    asprintf( &path, "%s/%s", folder, name );
     393
     394    p = path + strlen( folder ) + 1;;
     395    while( ( s = strchr( p, '/' ) ) )
     396    {
     397        *s = '\0';
     398        if( stat( folder, &sb ) )
     399        {
     400            /* Sub-folder does not exist */
     401            if( !write )
     402            {
     403                ret = TR_ERROR_IO_OTHER;
     404                break;
     405            }
     406            if( mkdir( path, 0777 ) )
     407            {
     408                ret = ErrorFromErrno();
     409                break;
     410            }
     411        }
     412        else
     413        {
     414            if( !S_ISDIR( sb.st_mode ) )
     415            {
     416                ret = TR_ERROR_IO_OTHER;
     417                break;
     418            }
     419        }
     420        *s = '/';
     421    }
     422    free( path );
     423
     424    return ret;
     425}
Note: See TracChangeset for help on using the changeset viewer.