Skip to content

Commit

Permalink
Better path related methods...
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-David Gadina committed Feb 20, 2012
1 parent edcd8c5 commit 442227a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 29 deletions.
5 changes: 3 additions & 2 deletions EOSFTPServer/FTPServer/Classes/EOSFTPServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ FOUNDATION_EXPORT EOSFTPServerCommand EOSFTPServerCommandNOOP;
@class EOSFTPServerUser;
@class EOSFTPServerConnection;
@class AsyncSocket;
@class EOSFile;

@interface EOSFTPServer: NSObject
{
Expand Down Expand Up @@ -138,8 +139,8 @@ FOUNDATION_EXPORT EOSFTPServerCommand EOSFTPServerCommandNOOP;
- ( NSString * )messageForReplyCode: ( EOSFTPServerReplyCode )code;
- ( void )processCommand: ( NSString * )command connection: ( EOSFTPServerConnection * )connection;
- ( NSString * )formattedMessage: ( NSString * )message code: ( EOSFTPServerReplyCode )code;
- ( NSString * )absolutePathForConnection: ( EOSFTPServerConnection * )connection subPath: ( NSString * )path;
- ( NSString * )serverPathForConnection: ( EOSFTPServerConnection * )connection subPath: ( NSString * )path;
- ( EOSFile * )fileAtPath: ( NSString * )path connection: ( EOSFTPServerConnection * )connection;
- ( NSString * )serverPathForFile: ( EOSFile * )file;
- ( NSUInteger )getPASVDataPort;

@end
98 changes: 71 additions & 27 deletions EOSFTPServer/FTPServer/Classes/EOSFTPServer.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#import "EOSFTPServerConnection.h"
#import "NSString+EOS.h"
#import "AsyncSocket.h"
#import "EOSFile.h"

NSString * const EOSFTPServerException = @"EOSFTPServerException";

Expand Down Expand Up @@ -93,7 +94,6 @@ @implementation EOSFTPServer
@synthesize welcomeMessage = _welcomeMessage;
@synthesize quitMessage = _quitMessage;
@synthesize chroot = _chroot;
@synthesize rootDirectory = _rootDirectory;
@synthesize allowAnonymousUsers = _allowAnonymousUsers;
@synthesize delegate = _delegate;

Expand Down Expand Up @@ -946,50 +946,56 @@ - ( NSString * )formattedMessage: ( NSString * )message code: ( EOSFTPServerRepl
return formattedMessage;
}

- ( NSString * )absolutePathForConnection: ( EOSFTPServerConnection * )connection subPath: ( NSString * )path
- ( EOSFile * )fileAtPath: ( NSString * )path connection: ( EOSFTPServerConnection * )connection
{
NSString * root;

root = ( [ _rootDirectory hasSuffix: @"/" ] ) ? [ _rootDirectory substringToIndex: _rootDirectory.length - 2 ] : _rootDirectory;

if( [ path hasPrefix: @"/" ] == NO )
if( path.length == 0 )
{
path = connection.currentDirectory;
}
else if( [ path hasPrefix: @"/" ] == NO )
{
path = [ connection.currentDirectory stringByAppendingPathComponent: path ];
}
else if( _chroot == YES && [ path isEqualToString: @"/" ] == YES )
{
path = _rootDirectory;
}
else if( _chroot == YES && [ path hasPrefix: @"/" ] == YES )
{
path = [ _rootDirectory stringByAppendingPathComponent: path ];
}

if( _chroot == YES )
if( _chroot == YES && [ path hasPrefix: _rootDirectory ] == NO )
{
if( [ path hasPrefix: root ] == NO )
{
return nil;
}
return nil;
}

if( [ [ NSFileManager defaultManager ] fileExistsAtPath: path ] == NO )
{
return nil;
}

return ( [ [ NSFileManager defaultManager ] fileExistsAtPath: path ] ) ? path : nil;
return [ EOSFile fileWithPath: path ];
}

- ( NSString * )serverPathForConnection: ( EOSFTPServerConnection * )connection subPath: ( NSString * )path
- ( NSString * )serverPathForFile: ( EOSFile * )file
{
NSString * root;
NSString * absolutePath;


root = ( [ _rootDirectory hasSuffix: @"/" ] ) ? [ _rootDirectory substringToIndex: _rootDirectory.length - 2 ] : _rootDirectory;
absolutePath = [ self absolutePathForConnection: connection subPath: path ];

if( absolutePath == nil )
if( file == nil )
{
return nil;
}

if( _chroot == YES )
if( _chroot == NO )
{
absolutePath = [ absolutePath substringFromIndex: root.length ];

return ( [ absolutePath hasPrefix: @"/" ] == YES ) ? [ absolutePath substringFromIndex: 1 ] : absolutePath;
return file.path;
}

if( [ file.path hasPrefix: _rootDirectory ] == YES )
{
return [ file.path substringFromIndex: _rootDirectory.length - 1 ];
}

return absolutePath;
return nil;
}

- ( NSUInteger )getPASVDataPort
Expand All @@ -1011,4 +1017,42 @@ - ( NSUInteger )getPASVDataPort
return port;
}

- ( NSString * )rootDirectory
{
@synchronized( self )
{
return _rootDirectory;
}
}

- ( void )setRootDirectory: ( NSString * )path
{
BOOL isDir;

@synchronized( self )
{
[ _rootDirectory release ];

isDir = NO;
_rootDirectory = nil;

if( [ path hasSuffix: @"/" ] == NO )
{
path = [ path stringByAppendingString: @"/" ];
}

if( [ [ NSFileManager defaultManager ] fileExistsAtPath: path isDirectory: &isDir ] == NO )
{
@throw [ NSException exceptionWithName: EOSFTPServerException reason: [ NSString stringWithFormat: @"Path %@ does not exist", path ] userInfo: nil ];
}

if( isDir == NO )
{
@throw [ NSException exceptionWithName: EOSFTPServerException reason: [ NSString stringWithFormat: @"Path %@ is not a directory", path ] userInfo: nil ];
}

_rootDirectory = [ path copy ];
}
}

@end

0 comments on commit 442227a

Please sign in to comment.