libssh2_sftp_readdir_ex(3)
π libssh2_sftp_readdir_ex(3) π
okay bestie, welcome to the libssh2 manual, and specifically the page for libssh2_sftp_readdir_ex β the girl whose whole job is reading directory entries off an SFTP handle. she's giving β¨filesystem tour guideβ¨ energy and honestly? we're obsessed.
πΈ NAME πΈ
libssh2_sftp_readdir_ex β read directory data from an SFTP handle
π¦ SYNOPSIS π¦
#include <libssh2.h>
#include <libssh2_sftp.h>
int
libssh2_sftp_readdir_ex(LIBSSH2_SFTP_HANDLE *handle,
char *buffer, size_t buffer_maxlen,
char *longentry, size_t longentry_maxlen,
LIBSSH2_SFTP_ATTRIBUTES *attrs);
π DESCRIPTION π
So like, what does she actually do? She reads a block of data from a LIBSSH2_SFTP_HANDLE and hands you back the file entry info for the next entry, if there even is one. That's it, that's the gig. π
Now let's meet the cast, because there are a lot of them:
handleβ the SFTP File Handle, as returned bylibssh2_sftp_open_ex(3). it's your ticket, don't lose it.bufferβ a pointer to a pre-allocated buffer of at leastbuffer_maxlenbytes that we read data into.buffer_maxlenβ the length ofbuffer, in bytes. and bestie, listen: if the filename is longer than the space you gave it inbuffer_maxlen, it's gonna get truncated to fit. no cap. it just gets chopped.longentryβ a pointer to a pre-allocated buffer of at leastlongentry_maxlenbytes to read data into. and here's the thing: the format of thelongnamefield is unspecified by the SFTP protocol. it MUST be suitable for use in the output of a directory listing command (fr fr, the recommended move for a directory listing command is literally to just display this data as-is).longentry_maxlenβ the length oflongentryin bytes. same deal as before: if the full directory entry is longer than the space inlongentry_maxlen, it gets truncated to fit. πattrsβ a pointer toLIBSSH2_SFTP_ATTRIBUTESstorage, where she'll populatestatbuf-style data for you.
β¨ RETURN VALUE β¨
She returns the number of bytes actually populated into buffer β and no, that does not count the terminating zero, she's precise like that. π
Or, you know, negative on failure. And if she would otherwise block, she returns LIBSSH2_ERROR_EAGAIN. now here's the plot twist: even though LIBSSH2_ERROR_EAGAIN is a negative number, it is not really a failure per se. it's giving "girl, try again later" not "girl, we're done." π
π BUG π
okay so this one's important, bestie, sit down. if you pass in a too small buffer forbufferorlongentrywhen receiving data, then in libssh2 1.2.7 or earlier the only thing that happens is that she doesn't copy the entire data amount β and it is not possible for the application to tell when it happens! π like, no error, no warning, nothing. you just silently get less than you asked for. terrifying. iconic in the worst way.
π ERRORS π
LIBSSH2_ERROR_ALLOCβ an internal memory allocation call failed.LIBSSH2_ERROR_SOCKET_SENDβ unable to send data on socket.LIBSSH2_ERROR_SOCKET_TIMEOUTβ (that's it, that's the whole description in the original, she just left it blank bestie, we don't invent lore)LIBSSH2_ERROR_SFTP_PROTOCOLβ an invalid SFTP protocol response was received on the socket, or an SFTP operation caused an errorcode to be returned by the server.
And from 1.2.8 onward? π«Ά LIBSSH2_ERROR_BUFFER_TOO_SMALL is returned if any of the given buffer or longentry buffers are too small to fit the requested object name. so yes, they fixed the silent-truncation nightmare above. slay.
π SEE ALSO π
and that's the whole page, bestie!! π this was libssh2 0.15, dated 1 Jun 2007, and no I will not be explaining why a 2007 manpage has this much drama in it. go read some directories, stay hydrated, and please pass buffers that are big enough. mwah. ππ¦π
π¦ where this page comes from
- alpine 3.22: libssh2-doc 1.11.1-r3 Β· BSD-3-Clause
- alpine 3.23: libssh2-doc 1.11.1-r4 Β· BSD-3-Clause
- alpine 3.24: libssh2-doc 1.11.1-r4 Β· BSD-3-Clause
- arch rolling: libssh2 1.11.1-7 Β· BSD-3-Clause
- debian trixie: libssh2-1-dev 1.11.1-1+deb13u1 Β· BSD3
- debian trixie: libssh2-1-dev 1.11.1-1+deb13u2 Β· BSD3
- epel 10: libssh2-docs 1.11.1-9.el10_3 Β· BSD-3-Clause
- epel 9: libssh2-docs 1.11.1-9.el9 Β· BSD-3-Clause
- fedora 43: libssh2-docs 1.11.1-4.fc43 Β· BSD-3-Clause
- fedora 43: libssh2-docs 1.11.1-9.fc43 Β· BSD-3-Clause
- fedora 44: libssh2-docs 1.11.1-5.fc44 Β· BSD-3-Clause
- fedora 44: libssh2-docs 1.11.1-9.fc44 Β· BSD-3-Clause
- opensuse-leap 16.0: libssh2-devel 1.11.1-160000.5.1 Β· BSD-3-Clause
- opensuse-leap 16.0: libssh2-devel 1.11.1-160000.5.1 Β· BSD-3-Clause
- opensuse-leap 16.0: libssh2-devel 1.11.1-160000.5.1 Β· BSD-3-Clause
- opensuse-leap 16.0: libssh2-devel 1.11.1-160000.5.1 Β· BSD-3-Clause
- ubuntu questing: libssh2-1-dev 1.11.1-1build1
- ubuntu questing: libssh2-1-dev 1.11.1-1ubuntu0.25.10.2
- ubuntu resolute: libssh2-1-dev 1.11.1-1build2 Β· BSD3
- ubuntu resolute: libssh2-1-dev 1.11.1-1ubuntu0.26.04.4 Β· BSD3
- ubuntu stonking: libssh2-1-dev 1.11.1-6 Β· BSD3
- π the page's license: BSD-3-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms