I was encountering a "permission" situation, my application recursively scans a root FTP system and plans transfers - it naively added items and attempted to traverse folders to which it had no permissions.
So, while the permissions are accessable in string form - I wrote a little "Info" class to "crack" them into boolean results.
Notes:
- This will only work with Unix file system-type listings. However, my target is a Microsoft IIS/FTP box with the listing type set to "Unix".
- It will not actually "SET/CHMOD" permissions on a remote item.
- VB.NET, sorry - client demands - translate to C# if so desired.
- The primary methods (Load/Save) are shared/static as I am traversing through tens of thousands of entries, and do not want to create/discard thousands of "permission" helpers. So, in my "scanner" class I keep a single instance of "FTPFilePermissionInfo" and then simply call one of the "Load" methods for each file/folder I am traversing/investigating.
- License: PD/BSD/MIT - If you guys want to extend this, roll it into future releases - no problem, there is really nothing "magic" contained below.
Public Class FTPFilePermissionInfo
Public Class FTPPermission
Public ReadIsAllowed As Boolean
Public WriteIsAllowed As Boolean
Public ExecuteIsAllowed As Boolean
End Class
Public IsDirectory As Boolean
Private mOwner As FTPPermission
Public ReadOnly Property Owner() As FTPPermission
Get
Return mOwner
End Get
End Property
Private mGroup As FTPPermission
Public ReadOnly Property Group() As FTPPermission
Get
Return mGroup
End Get
End Property
Private mWorld As FTPPermission
Public ReadOnly Property World() As FTPPermission
Get
Return mWorld
End Get
End Property
Public Sub New()
mOwner = New FTPPermission
mGroup = New FTPPermission
mWorld = New FTPPermission
End Sub
Public Shared Sub Load(ByVal rawPermissions() As Char, ByVal Permissions As FTPFilePermissionInfo)
If rawPermissions.Length <> 10 Then
Throw New ArgumentException("Incorrect number of arguments, cannot determine FTP item permissions!")
End If
With Permissions
.IsDirectory = rawPermissions(0) = "d"c
.Owner.ReadIsAllowed = rawPermissions(1) = "r"c
.Owner.WriteIsAllowed = rawPermissions(2) = "w"c
.Owner.ExecuteIsAllowed = rawPermissions(3) = "x"c
.Group.ReadIsAllowed = rawPermissions(4) = "r"c
.Group.WriteIsAllowed = rawPermissions(5) = "w"c
.Group.ExecuteIsAllowed = rawPermissions(6) = "x"c
.World.ReadIsAllowed = rawPermissions(7) = "r"c
.World.WriteIsAllowed = rawPermissions(8) = "w"c
.World.ExecuteIsAllowed = rawPermissions(9) = "x"c
End With
End Sub
Public Shared Sub Load(ByVal rawPermissions As String, ByVal Permissions As FTPFilePermissionInfo)
Load(rawPermissions.ToCharArray, Permissions)
End Sub
Public Shared Function Load(ByVal rawPermissions As String) As FTPFilePermissionInfo
Dim lResult As New FTPFilePermissionInfo
Load(rawPermissions, lResult)
Return lResult
End Function
Public Shared Function Save(ByVal Permissions As FTPFilePermissionInfo) As String
Dim lResult As String
With Permissions
lResult += CStr(IIf(.IsDirectory, "d", "-"))
lResult += CStr(IIf(.Owner.ReadIsAllowed, "r", "-"))
lResult += CStr(IIf(.Owner.WriteIsAllowed, "w", "-"))
lResult += CStr(IIf(.Owner.ExecuteIsAllowed, "x", "-"))
lResult += CStr(IIf(.Group.ReadIsAllowed, "r", "-"))
lResult += CStr(IIf(.Group.WriteIsAllowed, "w", "-"))
lResult += CStr(IIf(.Group.ExecuteIsAllowed, "x", "-"))
lResult += CStr(IIf(.World.ReadIsAllowed, "r", "-"))
lResult += CStr(IIf(.World.WriteIsAllowed, "w", "-"))
lResult += CStr(IIf(.World.ExecuteIsAllowed, "x", "-"))
End With
Return lResult
End Function
Public Overrides Function ToString() As String
Dim lResult As String
With Me
lResult += "IsDirectory:" & .IsDirectory & ","
lResult += "Owner.ReadIsAllowed:" & .Owner.ReadIsAllowed & ","
lResult += "Owner.WriteIsAllowed:" & .Owner.WriteIsAllowed & ","
lResult += "Owner.ExecuteIsAllowed:" & .Owner.ExecuteIsAllowed & ","
lResult += "Group.ReadIsAllowed:" & .Group.ReadIsAllowed & ","
lResult += "Group.WriteIsAllowed:" & .Group.WriteIsAllowed & ","
lResult += "Group.ExecuteIsAllowed:" & .Group.ExecuteIsAllowed & ","
lResult += "World.ReadIsAllowed:" & .World.ReadIsAllowed & ","
lResult += "World.WriteIsAllowed:" & .World.WriteIsAllowed & ","
lResult += "World.ExecuteIsAllowed:" & .World.ExecuteIsAllowed & "."
End With
Return lResult
End Function
End Class