Skip to content

Commit

Permalink
Resolve symbolic links in readContentsAtPath
Browse files Browse the repository at this point in the history
  • Loading branch information
bummoblizard committed Dec 11, 2023
1 parent 2649dc1 commit 68d4728
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
9 changes: 9 additions & 0 deletions NMSSH/NMSFTP.m
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,15 @@ - (BOOL)readContentsAtPath:(NSString *)path toStream:(NSOutputStream *)outputStr
NMSSHLogWarn(@"contentsAtPath:progress: failed to get file attributes");
return NO;
}

if ([file isSymbolicLink]) {
NSString *linkPath = [self resolveSymbolicLinkAtPath:path];
if (!linkPath) {
NMSSHLogWarn(@"contentsAtPath:progress: failed to resolve symbolic link");
return NO;
}
return [self readContentsAtPath:linkPath toStream:outputStream progress:progress];
}

if ([outputStream streamStatus] == NSStreamStatusNotOpen) {
[outputStream open];
Expand Down
3 changes: 3 additions & 0 deletions NMSSH/NMSFTPFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
/** Property that declares whether the file is a directory or a regular file */
@property (nonatomic, readonly) BOOL isDirectory;

/** Property that declares whether the file is a symbolic link */
@property (nonatomic, readonly) BOOL isSymbolicLink;

/** Returns the last modification date of the file */
@property (nonatomic, nullable, readonly) NSDate *modificationDate;

Expand Down
3 changes: 3 additions & 0 deletions NMSSH/NMSFTPFile.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
@interface NMSFTPFile ()
@property (nonatomic, strong) NSString *filename;
@property (nonatomic, readwrite) BOOL isDirectory;
@property (nonatomic, readwrite) BOOL isSymbolicLink;
@property (nonatomic, strong) NSDate *modificationDate;
@property (nonatomic, strong) NSDate *lastAccess;
@property (nonatomic, strong) NSNumber *fileSize;
Expand Down Expand Up @@ -35,6 +36,7 @@ - (void)populateValuesFromSFTPAttributes:(LIBSSH2_SFTP_ATTRIBUTES)fileAttributes
[self setOwnerGroupID:fileAttributes.gid];
[self setPermissions:[self convertPermissionToSymbolicNotation:fileAttributes.permissions]];
[self setIsDirectory:LIBSSH2_SFTP_S_ISDIR(fileAttributes.permissions)];
[self setIsSymbolicLink:LIBSSH2_SFTP_S_ISLNK(fileAttributes.permissions)];
[self setFlags:fileAttributes.flags];
}

Expand Down Expand Up @@ -164,6 +166,7 @@ - (id)copyWithZone:(NSZone *)zone {
object.fileSize = [self.fileSize copyWithZone:zone];
object.permissions = [self.permissions copyWithZone:zone];
object.isDirectory = self.isDirectory;
object.isSymbolicLink = self.isSymbolicLink;
object.ownerUserID = self.ownerUserID;
object.ownerGroupID = self.ownerGroupID;
object.flags = self.flags;
Expand Down

0 comments on commit 68d4728

Please sign in to comment.