Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

Bug in SftpATTRS.cs, consts are not octals

2011-12-23
2013-05-28
  • Scott Leckie
    Scott Leckie
    2011-12-23

    Hi all,

    I was using the SFTP functionality where I needed to get the specific attributes, including mtime and perms for a given file.
    The inbuilt utility SftpATTRS.getPermissionsString() was returning what seemed to be just random settings.

    On closer inspection, Tamir Gal has listed a number of pseudo-consts (I think he expects them to be consts, but they are just static ints) for the well-known permissions flags. For example, he lists "static  int S_ISUID = 04000;"

    The problem is that he expects that the C# compiler will treat a number with a leading "0" as an octal, as a C compiler would. But it doesn't…
    After much head scratching, trying to figure out why the mask of octal 04000 was not masking correctly, it dawned on me that the value is actually 4,000 decimal, not 04000 octal.

    So the solution (given that C# does not have a built in octal converter) was to restate the values in hex.
    The entire update, including the originals, commented out, is shown below the ==== lines

    Thanks,
       Scott

    ==========

            /* Scott, 21/12/2011 - the original constants had leading zeroes which, in C, would
             * make the octals. Not in C#, though, where they come through as decimals.
             * Replaced the following with definitions in hex, which isn't quite as easy to read
             * but does have the benefit of being accurate…
    static  int S_ISUID = 04000; // set user ID on execution
    static  int S_ISGID = 02000; // set group ID on execution
    static  int S_ISVTX = 01000; // sticky bit   ****** NOT DOCUMENTED *****

    static  int S_IRUSR = 00400; // read by owner
    static  int S_IWUSR = 00200; // write by owner
    static  int S_IXUSR = 00100; // execute/search by owner
    static  int S_IREAD = 00400; // read by owner
    static  int S_IWRITE= 00200; // write by owner
    static  int S_IEXEC = 00100; // execute/search by owner

    static  int S_IRGRP = 00040; // read by group
    static  int S_IWGRP = 00020; // write by group
    static  int S_IXGRP = 00010; // execute/search by group

    static  int S_IROTH = 00004; // read by others
    static  int S_IWOTH = 00002; // write by others
    static  int S_IXOTH = 00001; // execute/search by others
            */

            static int S_ISUID = 0x800; // set user ID on execution
            static int S_ISGID = 0x400; // set group ID on execution
            static int S_ISVTX = 0x200; // sticky bit   ****** NOT DOCUMENTED *****

            static int S_IRUSR = 0x100; // read by owner
            static int S_IWUSR = 0x080; // write by owner
            static int S_IXUSR = 0x040; // execute/search by owner
            static int S_IREAD = 0x100; // read by owner
            static int S_IWRITE =0x080; // write by owner
            static int S_IEXEC = 0x040; // execute/search by owner

            static int S_IRGRP = 0x020; // read by group
            static int S_IWGRP = 0x010; // write by group
            static int S_IXGRP = 0x008; // execute/search by group

            static int S_IROTH = 00004; // read by others
            static int S_IWOTH = 00002; // write by others
            static int S_IXOTH = 00001; // execut/search by others

            private static int pmask = 0xFFF