Unix style permissions

Feb 22, 2014 at 6:07 PM
I have searched but cannot find how to set FtpClient to display traditional Unix style permissions, i.e drwxr--r--. I can write code to translate the Permission enum values to a string and assign the letters but would like to use the inbuilt functions wherever possible. So what is the proper setting?
Coordinator
Feb 22, 2014 at 6:18 PM
System.Net.FtpClient doesn't control how permissions are display in any way. At most calling .ToString() on the enum returns the .net default for enums which is typically to list them out separated by commas, I believe.
Feb 24, 2014 at 9:50 PM
Edited Feb 24, 2014 at 9:52 PM
The code is kind of inelegant, but I've written the following to parse permissions into Unix style. Pass in the current FtpListItem to ParseFlags() when looping through the FtpListItem array returned from GetListing().
private string ParseFlags(FtpListItem item)
{
    string flags = String.Empty;

    if (item.Type == FtpFileSystemObjectType.Directory) flags = "d";
    else if (item.Type == FtpFileSystemObjectType.File) flags = "-";
    else if (item.Type == FtpFileSystemObjectType.Link) flags = "l";

    flags += DecodePermissions(item.OwnerPermissions);
    flags += DecodePermissions(item.GroupPermissions);
    flags += DecodePermissions(item.OthersPermissions);

    return flags;
}

private string DecodePermissions(FtpPermission perm)
{
    string flags = (perm == (FtpPermission)7) ? "rwx" : "";
    flags += (perm == (FtpPermission)6) ? "rw-" : "";
    flags += (perm == (FtpPermission)5) ? "r-x" : "";
    flags += (perm == (FtpPermission)4) ? "r--" : "";
    flags += (perm == (FtpPermission)3) ? "-wx" : "";
    flags += (perm == (FtpPermission)2) ? "-w-" : "";
    flags += (perm == (FtpPermission)1) ? "--x" : "";
    flags += (perm == (FtpPermission)0) ? "---" : "";
    return flags;
}