1 module libssh.c_bindings.sftp;
2 
3 import libssh.c_bindings.libssh;
4 import libssh.c_bindings.ctypes;
5 
6 enum auto LIBSFTP_VERSION = 3;
7 
8 struct sftp_ext_struct { }
9 
10 alias sftp_attributes = sftp_attributes_struct*;
11 alias sftp_client_message = sftp_client_message_struct*;
12 alias sftp_dir = sftp_dir_struct*;
13 alias sftp_ext = sftp_ext_struct*;
14 alias sftp_file = sftp_file_struct*;
15 alias sftp_message = sftp_message_struct*;
16 alias sftp_packet = sftp_packet_struct*;
17 alias sftp_request_queue = sftp_request_queue_struct*;
18 alias sftp_session = sftp_session_struct*;
19 alias sftp_status_message = sftp_status_message_struct*;
20 alias sftp_statvfs_t = sftp_statvfs_struct*;
21 
22 struct sftp_session_struct {
23     ssh_session session;
24     ssh_channel channel;
25     int server_version;
26     int client_version;
27     int version_;
28     sftp_request_queue queue;
29     uint id_counter;
30     int errnum;
31     void **handles;
32     sftp_ext ext;
33 };
34 
35 struct sftp_packet_struct {
36     sftp_session sftp;
37     ubyte type;
38     ssh_buffer payload;
39 };
40 
41 /* file handler */
42 struct sftp_file_struct {
43     sftp_session sftp;
44     char *name;
45     ulong offset;
46     ssh_string handle;
47     int eof;
48     int nonblocking;
49 };
50 
51 struct sftp_dir_struct {
52     sftp_session sftp;
53     char *name;
54     ssh_string handle; /* handle to directory */
55     ssh_buffer buffer; /* contains raw attributes from server which haven't been parsed */
56     uint count; /* counts the number of following attributes structures into buffer */
57     int eof; /* end of directory listing */
58 };
59 
60 struct sftp_message_struct {
61     sftp_session sftp;
62     ubyte packet_type;
63     ssh_buffer payload;
64     uint id;
65 };
66 
67 /* this is a bunch of all data that could be into a message */
68 struct sftp_client_message_struct {
69     sftp_session sftp;
70     ubyte type;
71     uint id;
72     char *filename; /* can be "path" */
73     uint flags;
74     sftp_attributes attr;
75     ssh_string handle;
76     ulong offset;
77     uint len;
78     int attr_num;
79     ssh_buffer attrbuf; /* used by sftp_reply_attrs */
80     ssh_string data; /* can be newpath of rename() */
81     ssh_buffer complete_message; /* complete message in case of retransmission*/
82     char *str_data; /* cstring version of data */
83 };
84 
85 struct sftp_request_queue_struct {
86     sftp_request_queue next;
87     sftp_message message;
88 };
89 
90 /* SSH_FXP_MESSAGE described into .7 page 26 */
91 struct sftp_status_message_struct {
92     uint id;
93     uint status;
94     ssh_string error_unused; /* not used anymore */
95     ssh_string lang_unused;  /* not used anymore */
96     char *errormsg;
97     char *langmsg;
98 };
99 
100 struct sftp_attributes_struct {
101     char *name;
102     char *longname; /* ls -l output on openssh, not reliable else */
103     uint flags;
104     ubyte type;
105     ulong size;
106     uint uid;
107     uint gid;
108     char *owner; /* set if openssh and version 4 */
109     char *group; /* set if openssh and version 4 */
110     uint permissions;
111     ulong atime64;
112     uint atime;
113     uint atime_nseconds;
114     ulong createtime;
115     uint createtime_nseconds;
116     ulong mtime64;
117     uint mtime;
118     uint mtime_nseconds;
119     ssh_string acl;
120     uint extended_count;
121     ssh_string extended_type;
122     ssh_string extended_data;
123 };
124 
125 /**
126  * @brief SFTP statvfs structure.
127  */
128 struct sftp_statvfs_struct {
129     ulong f_bsize;   /** file system block size */
130     ulong f_frsize;  /** fundamental fs block size */
131     ulong f_blocks;  /** number of blocks (unit f_frsize) */
132     ulong f_bfree;   /** free blocks in file system */
133     ulong f_bavail;  /** free blocks for non-root */
134     ulong f_files;   /** total file inodes */
135     ulong f_ffree;   /** free file inodes */
136     ulong f_favail;  /** free file inodes for to non-root */
137     ulong f_fsid;    /** file system id */
138     ulong f_flag;    /** bit mask of f_flag values */
139     ulong f_namemax; /** maximum filename length */
140 };
141 
142 extern(C) {
143     /**
144     * @brief Start a new sftp session.
145     *
146     * @param session       The ssh session to use.
147     *
148     * @return              A new sftp session or NULL on error.
149     *
150     * @see sftp_free()
151     */
152     sftp_session sftp_new(ssh_session session);
153 
154     /**
155     * @brief Start a new sftp session with an existing channel.
156     *
157     * @param session       The ssh session to use.
158     * @param channel       An open session channel with subsystem already allocated
159     *
160     * @return              A new sftp session or NULL on error.
161     *
162     * @see sftp_free()
163     */
164     sftp_session sftp_new_channel(ssh_session session, ssh_channel channel);
165 
166 
167     /**
168     * @brief Close and deallocate a sftp session.
169     *
170     * @param sftp          The sftp session handle to free.
171     */
172     void sftp_free(sftp_session sftp);
173 
174     /**
175     * @brief Initialize the sftp session with the server.
176     *
177     * @param sftp          The sftp session to initialize.
178     *
179     * @return              0 on success, < 0 on error with ssh error set.
180     *
181     * @see sftp_new()
182     */
183     int sftp_init(sftp_session sftp);
184 
185     /**
186     * @brief Get the last sftp error.
187     *
188     * Use this function to get the latest error set by a posix like sftp function.
189     *
190     * @param sftp          The sftp session where the error is saved.
191     *
192     * @return              The saved error (see server responses), < 0 if an error
193     *                      in the function occured.
194     *
195     * @see Server responses
196     */
197     int sftp_get_error(sftp_session sftp);
198 
199     /**
200     * @brief Get the count of extensions provided by the server.
201     *
202     * @param  sftp         The sftp session to use.
203     *
204     * @return The count of extensions provided by the server, 0 on error or
205     *         not available.
206     */
207     uint sftp_extensions_get_count(sftp_session sftp);
208 
209     /**
210     * @brief Get the name of the extension provided by the server.
211     *
212     * @param  sftp         The sftp session to use.
213     *
214     * @param  indexn        The index number of the extension name you want.
215     *
216     * @return              The name of the extension.
217     */
218     const(char) *sftp_extensions_get_name(sftp_session sftp, uint indexn);
219 
220     /**
221     * @brief Get the data of the extension provided by the server.
222     *
223     * This is normally the version number of the extension.
224     *
225     * @param  sftp         The sftp session to use.
226     *
227     * @param  indexn        The index number of the extension data you want.
228     *
229     * @return              The data of the extension.
230     */
231     const(char) *sftp_extensions_get_data(sftp_session sftp, uint indexn);
232 
233     /**
234     * @brief Check if the given extension is supported.
235     *
236     * @param  sftp         The sftp session to use.
237     *
238     * @param  name         The name of the extension.
239     *
240     * @param  data         The data of the extension.
241     *
242     * @return 1 if supported, 0 if not.
243     *
244     * Example:
245     *
246     * @code
247     * sftp_extension_supported(sftp, "statvfs@openssh.com", "2");
248     * @endcode
249     */
250     int sftp_extension_supported(sftp_session sftp, const char *name,
251         const char *data);
252 
253     /**
254     * @brief Open a directory used to obtain directory entries.
255     *
256     * @param session       The sftp session handle to open the directory.
257     * @param path          The path of the directory to open.
258     *
259     * @return              A sftp directory handle or NULL on error with ssh and
260     *                      sftp error set.
261     *
262     * @see                 sftp_readdir
263     * @see                 sftp_closedir
264     */
265     sftp_dir sftp_opendir(sftp_session session, const char *path);
266 
267     /**
268     * @brief Get a single file attributes structure of a directory.
269     *
270     * @param session      The sftp session handle to read the directory entry.
271     * @param dir          The opened sftp directory handle to read from.
272     *
273     * @return             A file attribute structure or NULL at the end of the
274     *                     directory.
275     *
276     * @see                sftp_opendir()
277     * @see                sftp_attribute_free()
278     * @see                sftp_closedir()
279     */
280     sftp_attributes sftp_readdir(sftp_session session, sftp_dir dir);
281 
282     /**
283     * @brief Tell if the directory has reached EOF (End Of File).
284     *
285     * @param dir           The sftp directory handle.
286     *
287     * @return              1 if the directory is EOF, 0 if not.
288     *
289     * @see                 sftp_readdir()
290     */
291     int sftp_dir_eof(sftp_dir dir);
292 
293     /**
294     * @brief Get information about a file or directory.
295     *
296     * @param session       The sftp session handle.
297     * @param path          The path to the file or directory to obtain the
298     *                      information.
299     *
300     * @return              The sftp attributes structure of the file or directory,
301     *                      NULL on error with ssh and sftp error set.
302     *
303     * @see sftp_get_error()
304     */
305     sftp_attributes sftp_stat(sftp_session session, const char *path);
306 
307     /**
308     * @brief Get information about a file or directory.
309     *
310     * Identical to sftp_stat, but if the file or directory is a symbolic link,
311     * then the link itself is stated, not the file that it refers to.
312     *
313     * @param session       The sftp session handle.
314     * @param path          The path to the file or directory to obtain the
315     *                      information.
316     *
317     * @return              The sftp attributes structure of the file or directory,
318     *                      NULL on error with ssh and sftp error set.
319     *
320     * @see sftp_get_error()
321     */
322     sftp_attributes sftp_lstat(sftp_session session, const char *path);
323 
324     /**
325     * @brief Get information about a file or directory from a file handle.
326     *
327     * @param file          The sftp file handle to get the stat information.
328     *
329     * @return              The sftp attributes structure of the file or directory,
330     *                      NULL on error with ssh and sftp error set.
331     *
332     * @see sftp_get_error()
333     */
334     sftp_attributes sftp_fstat(sftp_file file);
335 
336     /**
337     * @brief Free a sftp attribute structure.
338     *
339     * @param file          The sftp attribute structure to free.
340     */
341     void sftp_attributes_free(sftp_attributes file);
342 
343     /**
344     * @brief Close a directory handle opened by sftp_opendir().
345     *
346     * @param dir           The sftp directory handle to close.
347     *
348     * @return              Returns SSH_NO_ERROR or SSH_ERROR if an error occured.
349     */
350     int sftp_closedir(sftp_dir dir);
351 
352     /**
353     * @brief Close an open file handle.
354     *
355     * @param file          The open sftp file handle to close.
356     *
357     * @return              Returns SSH_NO_ERROR or SSH_ERROR if an error occured.
358     *
359     * @see                 sftp_open()
360     */
361     int sftp_close(sftp_file file);
362 
363     /**
364     * @brief Open a file on the server.
365     *
366     * @param session       The sftp session handle.
367     *
368     * @param file          The file to be opened.
369     *
370     * @param accesstype    Is one of O_RDONLY, O_WRONLY or O_RDWR which request
371     *                      opening  the  file  read-only,write-only or read/write.
372     *                      Acesss may also be bitwise-or'd with one or  more of
373     *                      the following:
374     *                      O_CREAT - If the file does not exist it will be
375     *                      created.
376     *                      O_EXCL - When  used with O_CREAT, if the file already
377     *                      exists it is an error and the open will fail.
378     *                      O_TRUNC - If the file already exists it will be
379     *                      truncated.
380     *
381     * @param mode          Mode specifies the permissions to use if a new file is
382     *                      created.  It  is  modified  by  the process's umask in
383     *                      the usual way: The permissions of the created file are
384     *                      (mode & ~umask)
385     *
386     * @return              A sftp file handle, NULL on error with ssh and sftp
387     *                      error set.
388     *
389     * @see sftp_get_error()
390     */
391     sftp_file sftp_open(sftp_session session, const char *file, int accesstype,
392         mode_t mode);
393 
394     /**
395     * @brief Make the sftp communication for this file handle non blocking.
396     *
397     * @param[in]  handle   The file handle to set non blocking.
398     */
399     void sftp_file_set_nonblocking(sftp_file handle);
400 
401     /**
402     * @brief Make the sftp communication for this file handle blocking.
403     *
404     * @param[in]  handle   The file handle to set blocking.
405     */
406     void sftp_file_set_blocking(sftp_file handle);
407 
408     /**
409     * @brief Read from a file using an opened sftp file handle.
410     *
411     * @param file          The opened sftp file handle to be read from.
412     *
413     * @param buf           Pointer to buffer to recieve read data.
414     *
415     * @param count         Size of the buffer in bytes.
416     *
417     * @return              Number of bytes written, < 0 on error with ssh and sftp
418     *                      error set.
419     *
420     * @see sftp_get_error()
421     */
422     ptrdiff_t sftp_read(sftp_file file, void *buf, size_t count);
423 
424     /**
425     * @brief Start an asynchronous read from a file using an opened sftp file handle.
426     *
427     * Its goal is to avoid the slowdowns related to the request/response pattern
428     * of a synchronous read. To do so, you must call 2 functions:
429     *
430     * sftp_async_read_begin() and sftp_async_read().
431     *
432     * The first step is to call sftp_async_read_begin(). This function returns a
433     * request identifier. The second step is to call sftp_async_read() using the
434     * returned identifier.
435     *
436     * @param file          The opened sftp file handle to be read from.
437     *
438     * @param len           Size to read in bytes.
439     *
440     * @return              An identifier corresponding to the sent request, < 0 on
441     *                      error.
442     *
443     * @warning             When calling this function, the internal offset is
444     *                      updated corresponding to the len parameter.
445     *
446     * @warning             A call to sftp_async_read_begin() sends a request to
447     *                      the server. When the server answers, libssh allocates
448     *                      memory to store it until sftp_async_read() is called.
449     *                      Not calling sftp_async_read() will lead to memory
450     *                      leaks.
451     *
452     * @see                 sftp_async_read()
453     * @see                 sftp_open()
454     */
455     int sftp_async_read_begin(sftp_file file, uint len);
456 
457     /**
458     * @brief Wait for an asynchronous read to complete and save the data.
459     *
460     * @param file          The opened sftp file handle to be read from.
461     *
462     * @param data          Pointer to buffer to recieve read data.
463     *
464     * @param len           Size of the buffer in bytes. It should be bigger or
465     *                      equal to the length parameter of the
466     *                      sftp_async_read_begin() call.
467     *
468     * @param id            The identifier returned by the sftp_async_read_begin()
469     *                      function.
470     *
471     * @return              Number of bytes read, 0 on EOF, SSH_ERROR if an error
472     *                      occured, SSH_AGAIN if the file is opened in nonblocking
473     *                      mode and the request hasn't been executed yet.
474     *
475     * @warning             A call to this function with an invalid identifier
476     *                      will never return.
477     *
478     * @see sftp_async_read_begin()
479     */
480     int sftp_async_read(sftp_file file, void *data, uint len, uint id);
481 
482     /**
483     * @brief Write to a file using an opened sftp file handle.
484     *
485     * @param file          Open sftp file handle to write to.
486     *
487     * @param buf           Pointer to buffer to write data.
488     *
489     * @param count         Size of buffer in bytes.
490     *
491     * @return              Number of bytes written, < 0 on error with ssh and sftp
492     *                      error set.
493     *
494     * @see                 sftp_open()
495     * @see                 sftp_read()
496     * @see                 sftp_close()
497     */
498     ptrdiff_t sftp_write(sftp_file file, const void *buf, size_t count);
499 
500     /**
501     * @brief Seek to a specific location in a file.
502     *
503     * @param file         Open sftp file handle to seek in.
504     *
505     * @param new_offset   Offset in bytes to seek.
506     *
507     * @return             0 on success, < 0 on error.
508     */
509     int sftp_seek(sftp_file file, uint new_offset);
510 
511     /**
512     * @brief Seek to a specific location in a file. This is the
513     * 64bit version.
514     *
515     * @param file         Open sftp file handle to seek in.
516     *
517     * @param new_offset   Offset in bytes to seek.
518     *
519     * @return             0 on success, < 0 on error.
520     */
521     int sftp_seek64(sftp_file file, ulong new_offset);
522 
523     /**
524     * @brief Report current byte position in file.
525     *
526     * @param file          Open sftp file handle.
527     *
528     * @return              The offset of the current byte relative to the beginning
529     *                      of the file associated with the file descriptor. < 0 on
530     *                      error.
531     */
532     uint sftp_tell(sftp_file file);
533 
534     /**
535     * @brief Report current byte position in file.
536     *
537     * @param file          Open sftp file handle.
538     *
539     * @return              The offset of the current byte relative to the beginning
540     *                      of the file associated with the file descriptor. < 0 on
541     *                      error.
542     */
543     ulong sftp_tell64(sftp_file file);
544 
545     /**
546     * @brief Rewinds the position of the file pointer to the beginning of the
547     * file.
548     *
549     * @param file          Open sftp file handle.
550     */
551     void sftp_rewind(sftp_file file);
552 
553     /**
554     * @brief Unlink (delete) a file.
555     *
556     * @param sftp          The sftp session handle.
557     *
558     * @param file          The file to unlink/delete.
559     *
560     * @return              0 on success, < 0 on error with ssh and sftp error set.
561     *
562     * @see sftp_get_error()
563     */
564     int sftp_unlink(sftp_session sftp, const char *file);
565 
566     /**
567     * @brief Remove a directoy.
568     *
569     * @param sftp          The sftp session handle.
570     *
571     * @param directory     The directory to remove.
572     *
573     * @return              0 on success, < 0 on error with ssh and sftp error set.
574     *
575     * @see sftp_get_error()
576     */
577     int sftp_rmdir(sftp_session sftp, const char *directory);
578 
579     /**
580     * @brief Create a directory.
581     *
582     * @param sftp          The sftp session handle.
583     *
584     * @param directory     The directory to create.
585     *
586     * @param mode          Specifies the permissions to use. It is modified by the
587     *                      process's umask in the usual way:
588     *                      The permissions of the created file are (mode & ~umask)
589     *
590     * @return              0 on success, < 0 on error with ssh and sftp error set.
591     *
592     * @see sftp_get_error()
593     */
594     int sftp_mkdir(sftp_session sftp, const char *directory, mode_t mode);
595 
596     /**
597     * @brief Rename or move a file or directory.
598     *
599     * @param sftp          The sftp session handle.
600     *
601     * @param original      The original url (source url) of file or directory to
602     *                      be moved.
603     *
604     * @param newname       The new url (destination url) of the file or directory
605     *                      after the move.
606     *
607     * @return              0 on success, < 0 on error with ssh and sftp error set.
608     *
609     * @see sftp_get_error()
610     */
611     int sftp_rename(sftp_session sftp, const char *original, const  char *newname);
612 
613     /**
614     * @brief Set file attributes on a file, directory or symbolic link.
615     *
616     * @param sftp          The sftp session handle.
617     *
618     * @param file          The file which attributes should be changed.
619     *
620     * @param attr          The file attributes structure with the attributes set
621     *                      which should be changed.
622     *
623     * @return              0 on success, < 0 on error with ssh and sftp error set.
624     *
625     * @see sftp_get_error()
626     */
627     int sftp_setstat(sftp_session sftp, const char *file, sftp_attributes attr);
628 
629     /**
630     * @brief Change the file owner and group
631     *
632     * @param sftp          The sftp session handle.
633     *
634     * @param file          The file which owner and group should be changed.
635     *
636     * @param owner         The new owner which should be set.
637     *
638     * @param group         The new group which should be set.
639     *
640     * @return              0 on success, < 0 on error with ssh and sftp error set.
641     *
642     * @see sftp_get_error()
643     */
644     int sftp_chown(sftp_session sftp, const char *file, uid_t owner, gid_t group);
645 
646     /**
647     * @brief Change permissions of a file
648     *
649     * @param sftp          The sftp session handle.
650     *
651     * @param file          The file which owner and group should be changed.
652     *
653     * @param mode          Specifies the permissions to use. It is modified by the
654     *                      process's umask in the usual way:
655     *                      The permissions of the created file are (mode & ~umask)
656     *
657     * @return              0 on success, < 0 on error with ssh and sftp error set.
658     *
659     * @see sftp_get_error()
660     */
661     int sftp_chmod(sftp_session sftp, const char *file, mode_t mode);
662 
663     /**
664     * @brief Change the last modification and access time of a file.
665     *
666     * @param sftp          The sftp session handle.
667     *
668     * @param file          The file which owner and group should be changed.
669     *
670     * @param times         A timeval structure which contains the desired access
671     *                      and modification time.
672     *
673     * @return              0 on success, < 0 on error with ssh and sftp error set.
674     *
675     * @see sftp_get_error()
676     */
677     int sftp_utimes(sftp_session sftp, const char *file, const timeval *times);
678 
679     /**
680     * @brief Create a symbolic link.
681     *
682     * @param  sftp         The sftp session handle.
683     *
684     * @param  target       Specifies the target of the symlink.
685     *
686     * @param  dest         Specifies the path name of the symlink to be created.
687     *
688     * @return              0 on success, < 0 on error with ssh and sftp error set.
689     *
690     * @see sftp_get_error()
691     */
692     int sftp_symlink(sftp_session sftp, const char *target, const char *dest);
693 
694     /**
695     * @brief Read the value of a symbolic link.
696     *
697     * @param  sftp         The sftp session handle.
698     *
699     * @param  path         Specifies the path name of the symlink to be read.
700     *
701     * @return              The target of the link, NULL on error.
702     *
703     * @see sftp_get_error()
704     */
705     char *sftp_readlink(sftp_session sftp, const char *path);
706 
707     /**
708     * @brief Get information about a mounted file system.
709     *
710     * @param  sftp         The sftp session handle.
711     *
712     * @param  path         The pathname of any file within the mounted file system.
713     *
714     * @return A statvfs structure or NULL on error.
715     *
716     * @see sftp_get_error()
717     */
718     sftp_statvfs_t sftp_statvfs(sftp_session sftp, const char *path);
719 
720     /**
721     * @brief Get information about a mounted file system.
722     *
723     * @param  file         An opened file.
724     *
725     * @return A statvfs structure or NULL on error.
726     *
727     * @see sftp_get_error()
728     */
729     sftp_statvfs_t sftp_fstatvfs(sftp_file file);
730 
731     /**
732     * @brief Free the memory of an allocated statvfs.
733     *
734     * @param  statvfs_o      The statvfs to free.
735     */
736     void sftp_statvfs_free(sftp_statvfs_t statvfs_o);
737 
738     /**
739     * @brief Canonicalize a sftp path.
740     *
741     * @param sftp          The sftp session handle.
742     *
743     * @param path          The path to be canonicalized.
744     *
745     * @return              The canonicalize path, NULL on error.
746     */
747     char *sftp_canonicalize_path(sftp_session sftp, const char *path);
748 
749     /**
750     * @brief Get the version of the SFTP protocol supported by the server
751     *
752     * @param sftp          The sftp session handle.
753     *
754     * @return              The server version.
755     */
756     int sftp_server_version(sftp_session sftp);
757 
758     version (LIBSSH_WITH_SERVER) {
759         /**
760         * @brief Create a new sftp server session.
761         *
762         * @param session       The ssh session to use.
763         *
764         * @param chan          The ssh channel to use.
765         *
766         * @return              A new sftp server session.
767         */
768         sftp_session sftp_server_new(ssh_session session, ssh_channel chan);
769 
770         /**
771         * @brief Intialize the sftp server.
772         *
773         * @param sftp         The sftp session to init.
774         *
775         * @return             0 on success, < 0 on error.
776         */
777         int sftp_server_init(sftp_session sftp);
778     }
779 
780 //    /* this is not a public interface */
781 //    #define SFTP_HANDLES 256
782 //    sftp_packet sftp_packet_read(sftp_session sftp);
783 //    int sftp_packet_write(sftp_session sftp,uint8_t type, ssh_buffer payload);
784 //    void sftp_packet_free(sftp_packet packet);
785 //    int buffer_add_attributes(ssh_buffer buffer, sftp_attributes attr);
786 //    sftp_attributes sftp_parse_attr(sftp_session session, ssh_buffer buf,int expectname);
787 //    /* sftpserver.c */
788 //    
789 //    LIBSSH_API sftp_client_message sftp_get_client_message(sftp_session sftp);
790 //    LIBSSH_API void sftp_client_message_free(sftp_client_message msg);
791 //    LIBSSH_API uint8_t sftp_client_message_get_type(sftp_client_message msg);
792 //    LIBSSH_API const char *sftp_client_message_get_filename(sftp_client_message msg);
793 //    LIBSSH_API void sftp_client_message_set_filename(sftp_client_message msg, const char *newname);
794 //    LIBSSH_API const char *sftp_client_message_get_data(sftp_client_message msg);
795 //    LIBSSH_API uint32_t sftp_client_message_get_flags(sftp_client_message msg);
796 //    LIBSSH_API int sftp_send_client_message(sftp_session sftp, sftp_client_message msg);
797 //    int sftp_reply_name(sftp_client_message msg, const char *name,
798 //        sftp_attributes attr);
799 //    int sftp_reply_handle(sftp_client_message msg, ssh_string handle);
800 //    ssh_string sftp_handle_alloc(sftp_session sftp, void *info);
801 //    int sftp_reply_attr(sftp_client_message msg, sftp_attributes attr);
802 //    void *sftp_handle(sftp_session sftp, ssh_string handle);
803 //    int sftp_reply_status(sftp_client_message msg, uint32_t status, const char *message);
804 //    int sftp_reply_names_add(sftp_client_message msg, const char *file,
805 //        const char *longname, sftp_attributes attr);
806 //    int sftp_reply_names(sftp_client_message msg);
807 //    int sftp_reply_data(sftp_client_message msg, const void *data, int len);
808 //    void sftp_handle_remove(sftp_session sftp, void *handle);
809 }
810 
811 /* SFTP commands and constants */
812 enum auto SSH_FXP_INIT = 1;
813 enum auto SSH_FXP_VERSION = 2;
814 enum auto SSH_FXP_OPEN = 3;
815 enum auto SSH_FXP_CLOSE = 4;
816 enum auto SSH_FXP_READ = 5;
817 enum auto SSH_FXP_WRITE = 6;
818 enum auto SSH_FXP_LSTAT = 7;
819 enum auto SSH_FXP_FSTAT = 8;
820 enum auto SSH_FXP_SETSTAT = 9;
821 enum auto SSH_FXP_FSETSTAT = 10;
822 enum auto SSH_FXP_OPENDIR = 11;
823 enum auto SSH_FXP_READDIR = 12;
824 enum auto SSH_FXP_REMOVE = 13;
825 enum auto SSH_FXP_MKDIR = 14;
826 enum auto SSH_FXP_RMDIR = 15;
827 enum auto SSH_FXP_REALPATH = 16;
828 enum auto SSH_FXP_STAT = 17;
829 enum auto SSH_FXP_RENAME = 18;
830 enum auto SSH_FXP_READLINK = 19;
831 enum auto SSH_FXP_SYMLINK = 20;
832 
833 enum auto SSH_FXP_STATUS = 101;
834 enum auto SSH_FXP_HANDLE = 102;
835 enum auto SSH_FXP_DATA = 103;
836 enum auto SSH_FXP_NAME = 104;
837 enum auto SSH_FXP_ATTRS = 105;
838 
839 enum auto SSH_FXP_EXTENDED = 200;
840 enum auto SSH_FXP_EXTENDED_REPLY = 201;
841 
842 /* attributes */
843 /* sftp draft is completely braindead : version 3 and 4 have different flags for same constants */
844 /* and even worst, version 4 has same flag for 2 different constants */
845 /* follow up : i won't develop any sftp4 compliant library before having a clarification */
846 
847 enum auto SSH_FILEXFER_ATTR_SIZE = 0x00000001;
848 enum auto SSH_FILEXFER_ATTR_PERMISSIONS = 0x00000004;
849 enum auto SSH_FILEXFER_ATTR_ACCESSTIME = 0x00000008;
850 enum auto SSH_FILEXFER_ATTR_ACMODTIME =  0x00000008;
851 enum auto SSH_FILEXFER_ATTR_CREATETIME = 0x00000010;
852 enum auto SSH_FILEXFER_ATTR_MODIFYTIME = 0x00000020;
853 enum auto SSH_FILEXFER_ATTR_ACL = 0x00000040;
854 enum auto SSH_FILEXFER_ATTR_OWNERGROUP = 0x00000080;
855 enum auto SSH_FILEXFER_ATTR_SUBSECOND_TIMES = 0x00000100;
856 enum auto SSH_FILEXFER_ATTR_EXTENDED = 0x80000000;
857 enum auto SSH_FILEXFER_ATTR_UIDGID = 0x00000002;
858 
859 /* types */
860 enum auto SSH_FILEXFER_TYPE_REGULAR = 1;
861 enum auto SSH_FILEXFER_TYPE_DIRECTORY = 2;
862 enum auto SSH_FILEXFER_TYPE_SYMLINK = 3;
863 enum auto SSH_FILEXFER_TYPE_SPECIAL = 4;
864 enum auto SSH_FILEXFER_TYPE_UNKNOWN = 5;
865 
866 /**
867  * @name Server responses
868  *
869  * @brief Responses returned by the sftp server.
870  * @{
871  */
872 
873 /** No error */
874 enum auto SSH_FX_OK = 0;
875 /** End-of-file encountered */
876 enum auto SSH_FX_EOF = 1;
877 /** File doesn't exist */
878 enum auto SSH_FX_NO_SUCH_FILE = 2;
879 /** Permission denied */
880 enum auto SSH_FX_PERMISSION_DENIED = 3;
881 /** Generic failure */
882 enum auto SSH_FX_FAILURE = 4;
883 /** Garbage received from server */
884 enum auto SSH_FX_BAD_MESSAGE = 5;
885 /** No connection has been set up */
886 enum auto SSH_FX_NO_CONNECTION = 6;
887 /** There was a connection, but we lost it */
888 enum auto SSH_FX_CONNECTION_LOST = 7;
889 /** Operation not supported by the server */
890 enum auto SSH_FX_OP_UNSUPPORTED = 8;
891 /** Invalid file handle */
892 enum auto SSH_FX_INVALID_HANDLE = 9;
893 /** No such file or directory path exists */
894 enum auto SSH_FX_NO_SUCH_PATH = 10;
895 /** An attempt to create an already existing file or directory has been made */
896 enum auto SSH_FX_FILE_ALREADY_EXISTS = 11;
897 /** We are trying to write on a write-protected filesystem */
898 enum auto SSH_FX_WRITE_PROTECT = 12;
899 /** No media in remote drive */
900 enum auto SSH_FX_NO_MEDIA = 13;
901 
902 /** @} */
903 
904 /* file flags */
905 enum auto SSH_FXF_READ = 0x01;
906 enum auto SSH_FXF_WRITE = 0x02;
907 enum auto SSH_FXF_APPEND = 0x04;
908 enum auto SSH_FXF_CREAT = 0x08;
909 enum auto SSH_FXF_TRUNC = 0x10;
910 enum auto SSH_FXF_EXCL = 0x20;
911 enum auto SSH_FXF_TEXT = 0x40;
912 
913 /* rename flags */
914 enum auto SSH_FXF_RENAME_OVERWRITE =  0x00000001;
915 enum auto SSH_FXF_RENAME_ATOMIC =     0x00000002;
916 enum auto SSH_FXF_RENAME_NATIVE =     0x00000004;
917 
918 enum auto SFTP_OPEN = SSH_FXP_OPEN;
919 enum auto SFTP_CLOSE = SSH_FXP_CLOSE;
920 enum auto SFTP_READ = SSH_FXP_READ;
921 enum auto SFTP_WRITE = SSH_FXP_WRITE;
922 enum auto SFTP_LSTAT = SSH_FXP_LSTAT;
923 enum auto SFTP_FSTAT = SSH_FXP_FSTAT;
924 enum auto SFTP_SETSTAT = SSH_FXP_SETSTAT;
925 enum auto SFTP_FSETSTAT = SSH_FXP_FSETSTAT;
926 enum auto SFTP_OPENDIR = SSH_FXP_OPENDIR;
927 enum auto SFTP_READDIR = SSH_FXP_READDIR;
928 enum auto SFTP_REMOVE = SSH_FXP_REMOVE;
929 enum auto SFTP_MKDIR = SSH_FXP_MKDIR;
930 enum auto SFTP_RMDIR = SSH_FXP_RMDIR;
931 enum auto SFTP_REALPATH = SSH_FXP_REALPATH;
932 enum auto SFTP_STAT = SSH_FXP_STAT;
933 enum auto SFTP_RENAME = SSH_FXP_RENAME;
934 enum auto SFTP_READLINK = SSH_FXP_READLINK;
935 enum auto SFTP_SYMLINK = SSH_FXP_SYMLINK;
936 
937 /* openssh flags */
938 enum auto SSH_FXE_STATVFS_ST_RDONLY = 0x1; /* read-only */
939 enum auto SSH_FXE_STATVFS_ST_NOSUID = 0x2; /* no setuid */