Setting File Permissions with the FTP Component


The FTP protocol does not include a standard command for changing file permissions. However, many FTP servers (particularly those running on Unix and Linux) support the non-standard SITE CHMOD command, which changes the permissions of a file or directory in the same way as the Unix chmod command.

The FTP component does not have a dedicated method for this command, but it can be sent using the SendCommand method, which sends a command to the server exactly as it is provided. After calling SendCommand, the server's response is available in the LastReply property.

Changing Permissions After an Upload

The example below uploads a file and then changes its permissions so that the owner can read and write it, and everyone else can only read it.

FTP ftp = new FTP(); ftp.RemoteHost = "ftp.example.com"; ftp.User = "USER"; ftp.Password = "PASSWORD"; ftp.Logon(); // Upload the file. ftp.LocalFile = "C:\\reports\\summary.txt"; ftp.RemoteFile = "/reports/summary.txt"; ftp.Upload(); // Change the permissions of the uploaded file. try { ftp.SendCommand("SITE CHMOD 644 /reports/summary.txt"); Console.WriteLine("Server response: " + ftp.LastReply); } catch (IPWorksException ex) { // The server does not support SITE CHMOD, or the permissions could not be changed. Console.WriteLine("Could not change permissions: " + ex.Message); } ftp.Logoff();

The path passed to SITE CHMOD can be either an absolute path or a path relative to the current directory on the server (as set by the ChangeRemotePath method). Paths that contain spaces are generally accepted as-is, since the server treats everything after the permission mask as the file name.

Permission Masks

The permission mask is a three-digit octal number, where the digits specify the permissions of the file's owner, group, and all other users, respectively. Each digit is the sum of 4 (read), 2 (write), and 1 (execute). Some common values are:

Mask Owner Group Others Typical Use
644 Read, write Read Read Regular files
600 Read, write None None Private files
755 Read, write, execute Read, execute Read, execute Directories and executable files
700 Read, write, execute None None Private directories

Notes

  • If the server rejects the command (for example, with a 500 or 502 reply because SITE CHMOD is not supported), SendCommand throws an exception with error code 141, and the exception's message includes the server's reply.
  • Many Windows-based FTP servers, such as IIS, do not support SITE CHMOD, since Windows uses a different permission model.
  • The current permissions of the files in a directory can be checked by calling ListDirectoryLong and inspecting the Entry field of each item in the DirList collection, which contains the full listing line returned by the server (for example, -rw-r--r-- at the start of a Unix-style listing).
  • The same approach works for FTPS (FTP over TLS), which is supported by the FTP component through the SSLStartMode property.
  • For SFTP servers, use the SFTPClient component (included in IPWorks SFTP and IPWorks SSH), which can set file permissions directly using the Permissions field of its FileAttributes property and the UpdateFileAttributes method.

Note that in older versions of IPWorks, commands were sent by setting the Command property, which has been replaced by the SendCommand method.

We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.