Zig
Zig
Close a Remote SFTP File or Directory Handle
See more SFTP Examples
Demonstrates the Chilkat SFtp.CloseHandle method, which closes a remote file or directory handle previously returned by OpenFile or OpenDir. The only argument is the handle.
Background: Every opened handle corresponds to state the server holds, and servers cap how many a session may keep open at once, so a long-running program that forgets to close handles will eventually start failing to open new ones. Closing also ensures buffered writes are committed. Once closed, a handle is invalid and must not be reused.
Chilkat Zig Downloads
const std = @import("std");
const chilkat = @import("chilkat");
pub fn main(init: std.process.Init) !void {
const alloc = init.arena.allocator();
// Demonstrates the SFtp.CloseHandle method, which closes a remote file or directory handle. The
// only argument is the handle returned by OpenFile or OpenDir.
const sftp = try chilkat.SFtp.init();
defer sftp.deinit();
// Connect, authenticate, and initialize the SFTP subsystem.
const port = 22;
sftp.connect("sftp.example.com", port) catch {
std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
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";
sftp.authenticatePw("mySshLogin", password) catch {
std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
return;
};
sftp.initializeSftp() catch {
std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
return;
};
// Open a remote file, obtaining a handle.
const handle = sftp.openFile(alloc, "subdir/data.txt", "readOnly", "openExisting") catch {
std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
return;
};
// ... use the handle ...
// Close the handle to release the server-side resource. A handle should not be used after it
// is closed.
sftp.closeHandle(handle) catch {
std.debug.print("{s}\n", .{try sftp.getLastErrorText(alloc)});
return;
};
std.debug.print("Handle closed.\n", .{});
sftp.disconnect();
}