React Native
React Native
Flush an SFTP File to Stable Storage (Fsync)
See more SFTP Examples
Demonstrates the Chilkat SFtp.Fsync method, which requests that the server flush pending data for an open file to stable storage. The only argument is the handle. It uses the OpenSSH fsync@openssh.com extension and succeeds only when the server supports it.
Background: A successful write does not guarantee the data has reached disk — it may still sit in the server's write cache, where a crash or power loss would lose it.
Fsync forces it to durable storage, which matters for transactional or journal-style files where a committed record must survive a failure. Because it relies on a non-standard OpenSSH extension, expect it to fail against servers that do not implement it, and treat that as a capability check rather than a hard error where appropriate.Chilkat React Native Downloads
import { SFtp } from '@chilkat/react-native'
async function chilkatExample() {
// Demonstrates the SFtp.Fsync method, which requests that the server flush pending data for an
// open file to stable storage. The only argument is the handle.
const sftp = new SFtp();
// Connect, authenticate, and initialize the SFTP subsystem.
const port = 22;
try {
await sftp.connectAsync('sftp.example.com', port);
} catch {
console.log(sftp.lastErrorText);
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.
const password = 'mySshPassword';
try {
await sftp.authenticatePwAsync('mySshLogin', password);
} catch {
console.log(sftp.lastErrorText);
return;
}
try {
await sftp.initializeSftpAsync();
} catch {
console.log(sftp.lastErrorText);
return;
}
let handle: string;
try {
handle = await sftp.openFileAsync('subdir/journal.log', 'writeOnly', 'openOrCreate,appendData');
} catch {
console.log(sftp.lastErrorText);
return;
}
// Write a critical record.
try {
await sftp.writeFileTextAsync(handle, 'utf-8', '2026-07-23 transaction committed\n');
} catch {
console.log(sftp.lastErrorText);
return;
}
// Ask the server to flush the data to durable storage before continuing. Fsync uses the
// OpenSSH fsync@openssh.com extension and succeeds only when the server supports it.
try {
await sftp.fsyncAsync(handle);
} catch {
console.log(sftp.lastErrorText);
return;
}
try {
await sftp.closeHandleAsync(handle);
} catch {
console.log(sftp.lastErrorText);
return;
}
console.log('Data flushed to stable storage.');
sftp.disconnect();
}