Sample code for 30+ languages & platforms
Zig

Get FTP File Permissions

See more FTP Examples

_LANGUAGE_ example showing how to retrieve file permissions (via FTP) for each file in a remote directory.

Chilkat Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    const ftp = try chilkat.Ftp2.init();
    defer ftp.deinit();

    // Set the various properties to establish the connection.
    ftp.setHostname("ftp.example.com");
    ftp.setUsername("login");
    ftp.setPassword("password");

    // If SSL/TLS is required..
    // See http://www.cknotes.com/determining-ftp2-connection-settings/
    // for more information.
    ftp.setAuthTls(true);
    ftp.setPassiveUseHostAddr(true);

    // Connect to the FTP server:
    ftp.connectOnly() catch {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    };

    // Authenticate with the FTP server.
    ftp.loginAfterConnectOnly() catch {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    };

    // To get file permissions in UNIX format, disallow MSLD:
    ftp.setAllowMlsd(false);

    // To get file and sub-directory information, simply
    // loop from 0 to ftp.GetDirCount() - 1
    var i: i32 = 0;
    var n: i32 = ftp.getDirCount();
    if (n < 0) {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    }

    // Show the type of permissions information that is available.
    // The two most common types are "mlsd" and "unix".  See the
    // online reference documentation for more information.
    if (n > 0) {
        std.debug.print("The permissions format is: {s}\n", .{try ftp.getPermType(alloc, 0)});
    }

    while (i < n) {
        // Display the permissions and filename
        std.debug.print("{s} {s}\n", .{ try ftp.getPermissions(alloc, i), try ftp.getFilename(alloc, i) });

        i = i + 1;
    }

    // Assuming MLSD was possible, let's see the file permissions in MLSD format:
    ftp.setAllowMlsd(true);

    // Clear the directory cache so we're forced to re-fetch the directory listing:
    ftp.clearDirCache();

    n = ftp.getDirCount();
    if (n < 0) {
        std.debug.print("{s}\n", .{try ftp.getLastErrorText(alloc)});
        return;
    }

    // Show the type of permissions information that is available.
    // The two most common types are "mlsd" and "unix".  See the
    // online reference documentation for more information.
    if (n > 0) {
        std.debug.print("----\n", .{});
        std.debug.print("The permissions format is: {s}\n", .{try ftp.getPermType(alloc, 0)});
    }

    i = 0;
    while (i < n) {
        // Display the permissions and filename
        std.debug.print("{s} {s}\n", .{ try ftp.getPermissions(alloc, i), try ftp.getFilename(alloc, i) });

        i = i + 1;
    }

    try ftp.disconnect();
}