[Root] / Xceed Data Manipulation Components for .NET / [Root] / Basic Concepts / [Root] / SFTP Capabilities / POSIX Attributes

In This Topic
    POSIX Attributes
    In This Topic

    The Xceed FileSystem API with its generic properties for files and folders are usually sufficient to manage the metadata around files and folders.

    Sometimes, it might be necessary to directly manipulate the low-level POSIX attributes and permissions around which the SFtp protocol is based. The SFtpFile and SFtpFolder classes contain properties that get and set these attributes.

    Not all servers support POSIX attributes and permissions. Servers that are Windows-based tend to ignore or return empty values for these attributes. Servers that run on POSIX operating systems like Linux, Unix, etc, tend to fully support them.

    Below are examples that show each property in action in typical usage.

    Get POSIX attributes from files

    To get the POSIX attributes from a file, get a logical representation of a remote file by getting a SFtpFile object. The key step is to down-cast any AbstractFile objects to SFtpFile because the PosIX-related properties are specific to the SFtpFile class. Then, the properties mentioned above are available to get the information. It is also possible to test if the server supports an attribute.

    C#
    Copy Code
    using( SFtpSession sftp = new SFtpSession( ssh ) )
    {
      int uid;
      string owner;
      int gid;
      string group;
      PosixFilePermissions permissions;
      PosixFileType fileType;
    
      // Create a logical representation of an existing remote file
      SFtpFile sftpFile = new SFtpFile( sftp, "20070703_004837_GREEN.xml" );
    
      /* The usual ways to get a file can be used also. The important step is to down-cast the AbstractFile
       * object to SFtpFile because the PosIX-related properties are specific to SFtpFile */
      
      // Alternative: Get a logical representation of an existing remote file from a SFtpFolder object
      //SFtpFolder root = new SFtpFolder( sftp );
      //sftpFile = ( SFtpFile ) root.GetFile( "20070703_004837_GREEN.xml" );
    
      // Alternative: Get a list of logical representations of existing remote files from a SFtpFolder object
      //AbstractFile[] files = root.GetFiles( false );
      //sftpFile = ( SFtpFile ) files[ 0 ];
    
      // Get the uid
      uid = sftpFile.OwnerUserID;
    
      // Get the user name that corresponds to the uid
      owner = sftpFile.OwnerUserName;
    
      // Get the gid
      gid = sftpFile.OwnerGroupID;
    
      // Get the group name that corresponds to the gid
      group = sftpFile.OwnerGroupName;
    
      // Get the PosIX permissions
      permissions = sftpFile.Permissions;
    
      // Get the PosIX file type
      fileType = sftpFile.FileType;
    }
    VB.NET
    Copy Code
    Using sftp As New SFtpSession(ssh)
        Dim uid As Integer
        Dim owner As String
        Dim gid As Integer
        Dim group As String
        Dim permissions As PosixFilePermissions
        Dim fileType As PosixFileType
    
        ' Create a logical representation of an existing remote file
        Dim sftpFile As New SFtpFile(sftp, "20070703_004837_GREEN.xml")
    
        '           The usual ways to get a file can be used also. The important step is to down-cast the AbstractFile
        '           * object to SFtpFile because the PosIX-related properties are specific to SFtpFile 
    
        ' Alternative: Get a logical representation of an existing remote file from a SFtpFolder object
        'SFtpFolder root = new SFtpFolder( sftp );
        'sftpFile = ( SFtpFile ) root.GetFile( "20070703_004837_GREEN.xml" );
    
        ' Alternative: Get a list of logical representations of existing remote files from a SFtpFolder object
        'AbstractFile[] files = root.GetFiles( false );
        'sftpFile = ( SFtpFile ) files[ 0 ];
    
        ' Get the uid
        uid = sftpFile.OwnerUserID
    
        ' Get the user name that corresponds to the uid
        owner = sftpFile.OwnerUserName
    
        ' Get the gid
        gid = sftpFile.OwnerGroupID
    
        ' Get the group name that corresponds to the gid
        group = sftpFile.OwnerGroupName
    
        ' Get the PosIX permissions
        permissions = sftpFile.Permissions
    
        ' Get the PosIX file type
        fileType = sftpFile.FileType
    End Using

    Get POSIX attributes from folders

    To get the POSIX attributes from a folder, get a logical representation of a remote folder by getting a SFtpFolder object. The key step is to down-cast any AbstractFolder objects to SFtpFolder because the PosIX-related properties are specific to the SFtpFolder class. Then, the properties mentioned above are available to get the information. It is also possible to test if the server supports an attribute.

    C#
    Copy Code
    using( SFtpSession sftp = new SFtpSession( ssh ) )
    {
      int uid;
      string owner;
      int gid;
      string group;
      PosixFilePermissions permissions;
      PosixFileType fileType;
    
      // Create a logical representation of an existing remote folder
      SFtpFolder sftpFolder = new SFtpFolder( sftp, "MyFolder" );
    
      /* The usual ways to get a folder can be used also. The important step is to down-cast the AbstractFolder
       * object to SFtpFolder because the PosIX-related properties are specific to SFtpFolder */
      
      // Alternative: Get a logical representation of an existing remote folder from a SFtpFolder object
      //SFtpFolder root = new SFtpFolder( sftp );
      //sftpFolder = ( SFtpFolder ) root.GetFolder( "MyFolder" );
    
      // Alternative: Get a list of logical representations of existing remote folders from a SFtpFolder object
      //AbstractFolder[] folders = root.GetFolders( false );
      //sftpFolder = ( SFtpFolder ) folders[ 0 ];
    
      // Get the uid
      uid = sftpFolder.OwnerUserID;
    
      // Get the user name that corresponds to the uid
      owner = sftpFolder.OwnerUserName;
    
      // Get the gid
      gid = sftpFolder.OwnerGroupID;
    
      // Get the group name that corresponds to the gid
      group = sftpFolder.OwnerGroupName;
    
      // Get the PosIX permissions
      permissions = sftpFolder.Permissions;
    
      /* In PosIX, everything is a file. Folders are simply a form of file with a different type */
      // Get the PosIX file type
      fileType = sftpFolder.FileType;
    }
    VB.NET
    Copy Code
    Using sftp As New SFtpSession(ssh)
        Dim uid As Integer
        Dim owner As String
        Dim gid As Integer
        Dim group As String
        Dim permissions As PosixFilePermissions
        Dim fileType As PosixFileType
    
        ' Create a logical representation of an existing remote folder
        Dim sftpFolder As New SFtpFolder(sftp, "MyFolder")
    
        '           The usual ways to get a folder can be used also. The important step is to down-cast the AbstractFolder
        '           * object to SFtpFolder because the PosIX-related properties are specific to SFtpFolder 
    
        ' Alternative: Get a logical representation of an existing remote folder from a SFtpFolder object
        'SFtpFolder root = new SFtpFolder( sftp );
        'sftpFolder = ( SFtpFolder ) root.GetFolder( "MyFolder" );
    
        ' Alternative: Get a list of logical representations of existing remote folders from a SFtpFolder object
        'AbstractFolder[] folders = root.GetFolders( false );
        'sftpFolder = ( SFtpFolder ) folders[ 0 ];
    
        ' Get the uid
        uid = sftpFolder.OwnerUserID
    
        ' Get the user name that corresponds to the uid
        owner = sftpFolder.OwnerUserName
    
        ' Get the gid
        gid = sftpFolder.OwnerGroupID
    
        ' Get the group name that corresponds to the gid
        group = sftpFolder.OwnerGroupName
    
        ' Get the PosIX permissions
        permissions = sftpFolder.Permissions
    
        ' In PosIX, everything is a file. Folders are simply a form of file with a different type 
        ' Get the PosIX file type
        fileType = sftpFolder.FileType
    End Using

    Set POSIX attributes to existing files

    To set the POSIX attributes to an existing file, using a batch update is the recommended way to go.

    C#
    Copy Code
    using( SFtpSession sftp = new SFtpSession( ssh ) )
    {
      // Create a logical representation of an existing file
      SFtpFile sftpFile = new SFtpFile( sftp, "20070703_004837_GREEN.xml" );
    
      // Make sure the file exists
      System.Diagnostics.Debug.Assert( sftpFile.Exists );
    
      try
      {
        // Optimize changes to the PosIX properties by batching the operations in a single network operation
        using( AutoBatchUpdate batch = new AutoBatchUpdate( sftpFile ) )
        {
          // OPTIONAL: If the server supports the uid property
          if( sftpFile.HasOwnerUserID.GetValueOrDefault( false ) )
          {
            /* Most servers, especially those running a PosIX operating system, will ignore
             * or reject attempts to set the uid unless the authenticated user is 'root'. */
    
            // Set the uid
            //sftpFile.OwnerUserID = 1003;
          }
    
          // OPTIONAL: If the server supports the gid property
          if( sftpFile.HasOwnerGroupID.GetValueOrDefault( false ) )
          {
            // Set the gid
            sftpFile.OwnerGroupID = 1002;
          }
    
          // OPTIONAL: If the server supports the gid property
          if( sftpFile.HasPermissions.GetValueOrDefault( false ) )
          {
            // Set the permissions
            sftpFile.Permissions = PosixFilePermissions.UserReadWrite | PosixFilePermissions.GroupReadWrite | PosixFilePermissions.OtherRead;
    
            // Example: let's change our mind and replace the group permissions, without a network penalty because we are in a batch
            sftpFile.Permissions = ( sftpFile.Permissions & ~PosixFilePermissions.GroupMask ) | PosixFilePermissions.GroupRead;
          }
    
          /* NOTE: The file type cannot be changed */
    
          /* At the closing of this 'using' block, the actual SFtp network operation will be performed once for all the changes made on the file */
        }
      }
      catch( ItemAccessDeniedException exception )
      {
    
      }
    }
    VB.NET
    Copy Code
    Using sftp As New SFtpSession(ssh)
        ' Create a logical representation of an existing file
        Dim sftpFile As New SFtpFile(sftp, "20070703_004837_GREEN.xml")
    
        ' Make sure the file exists
        System.Diagnostics.Debug.Assert(sftpFile.Exists)
    
        Try
            ' Optimize changes to the PosIX properties by batching the operations in a single network operation
            Using batch As New AutoBatchUpdate(sftpFile)
                ' OPTIONAL: If the server supports the uid property
                If sftpFile.HasOwnerUserID.GetValueOrDefault(False) Then
                    '                 Most servers, especially those running a PosIX operating system, will ignore
                    '                 * or reject attempts to set the uid unless the authenticated user is 'root'. 
    
                    ' Set the uid
                    'sftpFile.OwnerUserID = 1003;
                End If
    
                ' OPTIONAL: If the server supports the gid property
                If sftpFile.HasOwnerGroupID.GetValueOrDefault(False) Then
                    ' Set the gid
                    sftpFile.OwnerGroupID = 1002
                End If
    
                ' OPTIONAL: If the server supports the gid property
                If sftpFile.HasPermissions.GetValueOrDefault(False) Then
                    ' Set the permissions
                    sftpFile.Permissions = PosixFilePermissions.UserReadWrite Or PosixFilePermissions.GroupReadWrite Or PosixFilePermissions.OtherRead
    
                    ' Example: let's change our mind and replace the group permissions, without a network penalty because we are in a batch
                    sftpFile.Permissions = (sftpFile.Permissions And Not PosixFilePermissions.GroupMask) Or PosixFilePermissions.GroupRead
                End If
    
                ' NOTE: The file type cannot be changed 
    
                ' At the closing of this 'using' block, the actual SFtp network operation will be performed once for all the changes made on the file 
            End Using
        Catch exception As ItemAccessDeniedException
    
        End Try
    End Using

    Set POSIX attributes to existing folders

    To set the POSIX attributes to an existing folder, using a batch update is the recommended way to go.

    C#
    Copy Code
    using( SFtpSession sftp = new SFtpSession( ssh ) )
    {
      // Create a logical representation of an existing folder
      SFtpFolder sftpFolder = new SFtpFolder( sftp, "MyFolder" );
    
      // Make sure the folder exists
      System.Diagnostics.Debug.Assert( sftpFolder.Exists );
    
      try
      {
        // Optimize changes to the PosIX properties by batching the operations in a single network operation
        using( AutoBatchUpdate batch = new AutoBatchUpdate( sftpFolder ) )
        {
          // OPTIONAL: If the server supports the uid property
          if( sftpFolder.HasOwnerUserID.GetValueOrDefault( false ) )
          {
            /* Most servers, especially those running a PosIX operating system, will ignore
             * or reject attempts to set the uid unless the authenticated user is 'root'. */
    
            // Set the uid
            //sftpFolder.OwnerUserID = 1003;
          }
    
          // OPTIONAL: If the server supports the gid property
          if( sftpFolder.HasOwnerGroupID.GetValueOrDefault( false ) )
          {
            // Set the gid
            sftpFolder.OwnerGroupID = 1002;
          }
    
          // OPTIONAL: If the server supports the gid property
          if( sftpFolder.HasPermissions.GetValueOrDefault( false ) )
          {
            // Set the permissions
            sftpFolder.Permissions = PosixFilePermissions.UserReadWriteExecute | PosixFilePermissions.GroupReadWriteExecute | PosixFilePermissions.OtherReadExecute;
    
            // Example: let's change our mind and replace the group permissions, without a network penalty because we are in a batch
            sftpFolder.Permissions = ( sftpFolder.Permissions & ~PosixFilePermissions.GroupMask ) | PosixFilePermissions.GroupReadExecute;
          }
    
          /* NOTE: The file type cannot be changed */
    
          /* At the closing of this 'using' block, the actual SFtp network operation will be performed once for all the changes made on the folder */
        }
      }
      catch( ItemAccessDeniedException exception )
      {
    
      }
    }
    VB.NET
    Copy Code
    Using sftp As New SFtpSession(ssh)
        ' Create a logical representation of an existing folder
        Dim sftpFolder As New SFtpFolder(sftp, "MyFolder")
    
        ' Make sure the folder exists
        System.Diagnostics.Debug.Assert(sftpFolder.Exists)
    
        Try
            ' Optimize changes to the PosIX properties by batching the operations in a single network operation
            Using batch As New AutoBatchUpdate(sftpFolder)
                ' OPTIONAL: If the server supports the uid property
                If sftpFolder.HasOwnerUserID.GetValueOrDefault(False) Then
                    '                 Most servers, especially those running a PosIX operating system, will ignore
                    '                 * or reject attempts to set the uid unless the authenticated user is 'root'. 
    
                    ' Set the uid
                    'sftpFolder.OwnerUserID = 1003;
                End If
    
                ' OPTIONAL: If the server supports the gid property
                If sftpFolder.HasOwnerGroupID.GetValueOrDefault(False) Then
                    ' Set the gid
                    sftpFolder.OwnerGroupID = 1002
                End If
    
                ' OPTIONAL: If the server supports the gid property
                If sftpFolder.HasPermissions.GetValueOrDefault(False) Then
                    ' Set the permissions
                    sftpFolder.Permissions = PosixFilePermissions.UserReadWriteExecute Or PosixFilePermissions.GroupReadWriteExecute Or PosixFilePermissions.OtherReadExecute
    
                    ' Example: let's change our mind and replace the group permissions, without a network penalty because we are in a batch
                    sftpFolder.Permissions = (sftpFolder.Permissions And Not PosixFilePermissions.GroupMask) Or PosixFilePermissions.GroupReadExecute
                End If
    
                ' NOTE: The file type cannot be changed 
    
                ' At the closing of this 'using' block, the actual SFtp network operation will be performed once for all the changes made on the folder 
            End Using
        Catch exception As ItemAccessDeniedException
    
        End Try
    End Using

    Set POSIX attributes to new files

    To set the POSIX attributes to a new file, using a batch update is the recommended way to go, with the call to Create inside the batch. Most servers do to allow setting PosIX attribute values during file/folder creation for security reasons. Using a batch update solves this by automatically delaying the setting of attributes until the end of the batch operation, after the create operations have been completed.

    C#
    Copy Code
    using( SFtpSession sftp = new SFtpSession( ssh ) )
    {
      // Create a logical representation of a new file
      SFtpFile sftpFile = new SFtpFile( sftp, "MyNewFile.dat" );
    
      // Make sure the file does not exist
      System.Diagnostics.Debug.Assert( !sftpFile.Exists );
    
      try
      {
        /* Most servers do to allow setting PosIX attribute values during file/folder creation for security reasons. Using a batch update solves this
         * by automatically delaying the setting of attributes until the end of the batch operation, after the create operations have been completed. */
    
        // Optimize changes to the PosIX properties by batching the operations in a single network operation
        using( AutoBatchUpdate batch = new AutoBatchUpdate( sftpFile ) )
        {
            /* Most servers, especially those running a PosIX operating system, will ignore
             * or reject attempts to set the uid unless the authenticated user is 'root'. */
    
            // Set the uid
            //sftpFile.OwnerUserID = 1003;
    
            // Set the gid
            sftpFile.OwnerGroupID = 1002;
    
            // Set the permissions
            sftpFile.Permissions = PosixFilePermissions.UserReadWrite | PosixFilePermissions.GroupReadWrite | PosixFilePermissions.OtherRead;
    
            // Example: let's change our mind and replace the group permissions, without a network penalty because we are in a batch
            sftpFile.Permissions = ( sftpFile.Permissions & ~PosixFilePermissions.GroupMask ) | PosixFilePermissions.GroupRead;
    
          /* NOTE: The file type cannot be set. It is handled automatically by the component. */
    
          // Create the file
          sftpFile.Create();
    
          /* Create() will create the file immediately on the server. The setting of PosIX attributes is delayed until the end of the 'using' block */
        }
      }
      catch( ItemAccessDeniedException exception )
      {
    
      }
    }
    VB.NET
    Copy Code
    Using sftp As New SFtpSession(ssh)
        ' Create a logical representation of a new file
        Dim sftpFile As New SFtpFile(sftp, "MyNewFile.dat")
    
        ' Make sure the file does not exist
        System.Diagnostics.Debug.Assert(Not sftpFile.Exists)
    
        Try
            '             Most servers do to allow setting PosIX attribute values during file/folder creation for security reasons. Using a batch update solves this
            '             * by automatically delaying the setting of attributes until the end of the batch operation, after the create operations have been completed. 
    
            ' Optimize changes to the PosIX properties by batching the operations in a single network operation
            Using batch As New AutoBatchUpdate(sftpFile)
                '                 Most servers, especially those running a PosIX operating system, will ignore
                '                 * or reject attempts to set the uid unless the authenticated user is 'root'. 
    
                ' Set the uid
                'sftpFile.OwnerUserID = 1003;
    
                ' Set the gid
                sftpFile.OwnerGroupID = 1002
    
                ' Set the permissions
                sftpFile.Permissions = PosixFilePermissions.UserReadWrite Or PosixFilePermissions.GroupReadWrite Or PosixFilePermissions.OtherRead
    
                ' Example: let's change our mind and replace the group permissions, without a network penalty because we are in a batch
                sftpFile.Permissions = (sftpFile.Permissions And Not PosixFilePermissions.GroupMask) Or PosixFilePermissions.GroupRead
    
                ' NOTE: The file type cannot be set. It is handled automatically by the component. 
    
                ' Create the file
                sftpFile.Create()
    
                ' Create() will create the file immediately on the server. The setting of PosIX attributes is delayed until the end of the 'using' block 
            End Using
        Catch exception As ItemAccessDeniedException
    
        End Try
    End Using

    Set POSIX attributes to new folders

    To set the POSIX attributes to a new folder, using a batch update is the recommended way to go, with the call to Create inside the batch. Most servers do to allow setting PosIX attribute values during file/folder creation for security reasons. Using a batch update solves this by automatically delaying the setting of attributes until the end of the batch operation, after the create operations have been completed.

    C#
    Copy Code
    using( SFtpSession sftp = new SFtpSession( ssh ) )
    {
      // Create a logical representation of a new folder
      SFtpFolder sftpFolder = new SFtpFolder( sftp, "MyNewFolder.dat" );
    
      // Make sure the folder does not exist
      System.Diagnostics.Debug.Assert( !sftpFolder.Exists );
    
      try
      {
        /* Most servers do to allow setting PosIX attribute values during file/folder creation for security reasons. Using a batch update solves this
         * by automatically delaying the setting of attributes until the end of the batch operation, after the create operations have been completed. */
    
        // Optimize changes to the PosIX properties by batching the operations in a single network operation
        using( AutoBatchUpdate batch = new AutoBatchUpdate( sftpFolder ) )
        {
            /* Most servers, especially those running a PosIX operating system, will ignore
             * or reject attempts to set the uid unless the authenticated user is 'root'. */
    
            // Set the uid
            //sftpFolder.OwnerUserID = 1003;
    
            // Set the gid
            sftpFolder.OwnerGroupID = 1002;
    
            // Set the permissions
            sftpFolder.Permissions = PosixFilePermissions.UserReadWriteExecute | PosixFilePermissions.GroupReadWriteExecute | PosixFilePermissions.OtherReadExecute;
    
            // Example: let's change our mind and replace the group permissions, without a network penalty because we are in a batch
            sftpFolder.Permissions = ( sftpFolder.Permissions & ~PosixFilePermissions.GroupMask ) | PosixFilePermissions.GroupReadExecute;
    
          /* NOTE: The file type cannot be set. It is handled automatically by the component. */
    
          // Create the folder
          sftpFolder.Create();
    
          /* Create() will create the folderimmediately on the server. The setting of PosIX attributes is delayed until the end of the 'using' block */
        }
      }
      catch( ItemAccessDeniedException exception )
      {
    
      }
    }
    VB.NET
    Copy Code
    Using sftp As New SFtpSession(ssh)
        ' Create a logical representation of a new folder
        Dim sftpFolder As New SFtpFolder(sftp, "MyNewFolder.dat")
    
        ' Make sure the folder does not exist
        System.Diagnostics.Debug.Assert(Not sftpFolder.Exists)
    
        Try
            '             Most servers do to allow setting PosIX attribute values during file/folder creation for security reasons. Using a batch update solves this
            '             * by automatically delaying the setting of attributes until the end of the batch operation, after the create operations have been completed. 
    
            ' Optimize changes to the PosIX properties by batching the operations in a single network operation
            Using batch As New AutoBatchUpdate(sftpFolder)
                '                 Most servers, especially those running a PosIX operating system, will ignore
                '                 * or reject attempts to set the uid unless the authenticated user is 'root'. 
    
                ' Set the uid
                'sftpFolder.OwnerUserID = 1003;
    
                ' Set the gid
                sftpFolder.OwnerGroupID = 1002
    
                ' Set the permissions
                sftpFolder.Permissions = PosixFilePermissions.UserReadWriteExecute Or PosixFilePermissions.GroupReadWriteExecute Or PosixFilePermissions.OtherReadExecute
    
                ' Example: let's change our mind and replace the group permissions, without a network penalty because we are in a batch
                sftpFolder.Permissions = (sftpFolder.Permissions And Not PosixFilePermissions.GroupMask) Or PosixFilePermissions.GroupReadExecute
    
                ' NOTE: The file type cannot be set. It is handled automatically by the component. 
    
                ' Create the folder
                sftpFolder.Create()
    
                ' Create() will create the folderimmediately on the server. The setting of PosIX attributes is delayed until the end of the 'using' block 
            End Using
        Catch exception As ItemAccessDeniedException
    
        End Try
    End Using
    See Also