C
C
Set a Remote File's POSIX Permissions
See more SFTP Examples
Demonstrates the Chilkat SFtp.SetPermissions method, which sets the POSIX permission bits of a remote item. The arguments are the remote path (or handle), whether the first argument is a handle, and the permission bits. Pass the permission bits only, not the file-type bits.
Background: This is the SFTP equivalent of
chmod. Common values, written as octal, are 0644 for a normal file, 0600 for a private file, and 0755 for an executable or a directory. In languages without octal literals the same values must be given in decimal or hexadecimal (for example 0755 is decimal 493). Setting an executable bit after uploading a script is the classic use.Chilkat C Downloads
#include <C_CkSFtp.h>
void ChilkatSample(void)
{
BOOL success;
HCkSFtp sftp;
int port;
const char *password;
BOOL isHandle;
int permissions;
success = FALSE;
// Demonstrates the SFtp.SetPermissions method, which sets the POSIX permission bits of a remote
// item. The 1st argument is the remote path (or handle), the 2nd (isHandle) indicates which,
// and the 3rd is the permission bits.
sftp = CkSFtp_Create();
// Connect, authenticate, and initialize the SFTP subsystem.
port = 22;
success = CkSFtp_Connect(sftp,"sftp.example.com",port);
if (success == FALSE) {
printf("%s\n",CkSFtp_lastErrorText(sftp));
CkSFtp_Dispose(sftp);
return;
}
// Normally you would not hard-code the password in source. You should instead obtain it
// from an interactive prompt, environment variable, or a secrets vault.
password = "mySshPassword";
success = CkSFtp_AuthenticatePw(sftp,"mySshLogin",password);
if (success == FALSE) {
printf("%s\n",CkSFtp_lastErrorText(sftp));
CkSFtp_Dispose(sftp);
return;
}
success = CkSFtp_InitializeSftp(sftp);
if (success == FALSE) {
printf("%s\n",CkSFtp_lastErrorText(sftp));
CkSFtp_Dispose(sftp);
return;
}
// The 1st argument is a path, so isHandle is FALSE.
isHandle = FALSE;
// Pass the permission bits, not the file-type bits. 0755 gives the owner read/write/execute and
// read/execute to group and others -- a common mode for an executable script.
permissions = 493;
success = CkSFtp_SetPermissions(sftp,"subdir/script.sh",isHandle,permissions);
if (success == FALSE) {
printf("%s\n",CkSFtp_lastErrorText(sftp));
CkSFtp_Dispose(sftp);
return;
}
printf("Permissions set.\n");
CkSFtp_Disconnect(sftp);
CkSFtp_Dispose(sftp);
}