C
C
Get the Files Transferred by an SFTP Sync
See more SFTP Examples
Demonstrates the Chilkat SFtp.GetSyncedFiles method, which reports the relative paths processed by the most recent SyncTreeUpload or SyncTreeDownload. The only argument is a StringTable that receives the paths. Directory paths end with / so they can be distinguished from files.
Note: This example uses relative local paths, which are resolved against the application's current working directory. Absolute local paths may also be used. Supply the paths appropriate to your own environment.
Background: A sync call reports only overall success, but you usually want to know what actually changed — for logging, for triggering follow-up work on just the new files, or simply to confirm an incremental sync did the minimal transfer. This method provides that list after the fact. An empty result is meaningful too: it means everything was already up to date. Call it immediately after the sync, before another operation replaces the recorded list.
Chilkat C Downloads
#include <C_CkSFtp.h>
#include <C_CkStringTable.h>
void ChilkatSample(void)
{
BOOL success;
HCkSFtp sftp;
int port;
const char *password;
int mode;
BOOL recurse;
HCkStringTable synced;
int n;
int i;
const char *relPath;
success = FALSE;
// Demonstrates the SFtp.GetSyncedFiles method, which reports the relative paths processed by the
// most recent SyncTreeUpload or SyncTreeDownload. The only argument is a StringTable that
// receives the paths.
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;
}
// Perform a sync.
mode = 5;
recurse = TRUE;
success = CkSFtp_SyncTreeDownload(sftp,"/var/www/html","qa_output/website",mode,recurse);
if (success == FALSE) {
printf("%s\n",CkSFtp_lastErrorText(sftp));
CkSFtp_Dispose(sftp);
return;
}
// Get the list of files (and, for downloads, directories) that were actually transferred.
// Directory paths end with "/" so they can be told apart from file paths.
synced = CkStringTable_Create();
CkSFtp_GetSyncedFiles(sftp,synced);
n = CkStringTable_getCount(synced);
printf("Synced %d item(s):\n",n);
for (i = 0; i <= n - 1; i++) {
relPath = CkStringTable_stringAt(synced,i);
if (CkStringTable_getLastMethodSuccess(synced) == FALSE) {
printf("%s\n",CkStringTable_lastErrorText(synced));
CkSFtp_Dispose(sftp);
CkStringTable_Dispose(synced);
return;
}
printf("%s\n",relPath);
}
CkSFtp_Disconnect(sftp);
CkSFtp_Dispose(sftp);
CkStringTable_Dispose(synced);
}