libssh2_sftp.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /* Copyright (C) Sara Golemon <sarag@libssh2.org>
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms,
  5. * with or without modification, are permitted provided
  6. * that the following conditions are met:
  7. *
  8. * Redistributions of source code must retain the above
  9. * copyright notice, this list of conditions and the
  10. * following disclaimer.
  11. *
  12. * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following
  14. * disclaimer in the documentation and/or other materials
  15. * provided with the distribution.
  16. *
  17. * Neither the name of the copyright holder nor the names
  18. * of any other contributors may be used to endorse or
  19. * promote products derived from this software without
  20. * specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  23. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  24. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  25. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  27. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  32. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  34. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  35. * OF SUCH DAMAGE.
  36. *
  37. * SPDX-License-Identifier: BSD-3-Clause
  38. */
  39. #ifndef LIBSSH2_SFTP_H
  40. #define LIBSSH2_SFTP_H 1
  41. #include "libssh2.h"
  42. #ifndef _WIN32
  43. #include <unistd.h>
  44. #endif
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /* Note: Version 6 was documented at the time of writing
  49. * However it was marked as "DO NOT IMPLEMENT" due to pending changes
  50. *
  51. * Let's start with Version 3 (The version found in OpenSSH) and go from there
  52. */
  53. #define LIBSSH2_SFTP_VERSION 3
  54. typedef struct _LIBSSH2_SFTP LIBSSH2_SFTP;
  55. typedef struct _LIBSSH2_SFTP_HANDLE LIBSSH2_SFTP_HANDLE;
  56. typedef struct _LIBSSH2_SFTP_ATTRIBUTES LIBSSH2_SFTP_ATTRIBUTES;
  57. typedef struct _LIBSSH2_SFTP_STATVFS LIBSSH2_SFTP_STATVFS;
  58. /* Flags for open_ex() */
  59. #define LIBSSH2_SFTP_OPENFILE 0
  60. #define LIBSSH2_SFTP_OPENDIR 1
  61. /* Flags for rename_ex() */
  62. #define LIBSSH2_SFTP_RENAME_OVERWRITE 0x00000001
  63. #define LIBSSH2_SFTP_RENAME_ATOMIC 0x00000002
  64. #define LIBSSH2_SFTP_RENAME_NATIVE 0x00000004
  65. /* Flags for stat_ex() */
  66. #define LIBSSH2_SFTP_STAT 0
  67. #define LIBSSH2_SFTP_LSTAT 1
  68. #define LIBSSH2_SFTP_SETSTAT 2
  69. /* Flags for symlink_ex() */
  70. #define LIBSSH2_SFTP_SYMLINK 0
  71. #define LIBSSH2_SFTP_READLINK 1
  72. #define LIBSSH2_SFTP_REALPATH 2
  73. /* Flags for sftp_mkdir() */
  74. #define LIBSSH2_SFTP_DEFAULT_MODE -1
  75. /* SFTP attribute flag bits */
  76. #define LIBSSH2_SFTP_ATTR_SIZE 0x00000001
  77. #define LIBSSH2_SFTP_ATTR_UIDGID 0x00000002
  78. #define LIBSSH2_SFTP_ATTR_PERMISSIONS 0x00000004
  79. #define LIBSSH2_SFTP_ATTR_ACMODTIME 0x00000008
  80. #define LIBSSH2_SFTP_ATTR_EXTENDED 0x80000000
  81. /* SFTP statvfs flag bits */
  82. #define LIBSSH2_SFTP_ST_RDONLY 0x00000001
  83. #define LIBSSH2_SFTP_ST_NOSUID 0x00000002
  84. struct _LIBSSH2_SFTP_ATTRIBUTES {
  85. /* If flags & ATTR_* bit is set, then the value in this struct will be
  86. * meaningful Otherwise it should be ignored
  87. */
  88. unsigned long flags;
  89. libssh2_uint64_t filesize;
  90. unsigned long uid, gid;
  91. unsigned long permissions;
  92. unsigned long atime, mtime;
  93. };
  94. struct _LIBSSH2_SFTP_STATVFS {
  95. libssh2_uint64_t f_bsize; /* file system block size */
  96. libssh2_uint64_t f_frsize; /* fragment size */
  97. libssh2_uint64_t f_blocks; /* size of fs in f_frsize units */
  98. libssh2_uint64_t f_bfree; /* # free blocks */
  99. libssh2_uint64_t f_bavail; /* # free blocks for non-root */
  100. libssh2_uint64_t f_files; /* # inodes */
  101. libssh2_uint64_t f_ffree; /* # free inodes */
  102. libssh2_uint64_t f_favail; /* # free inodes for non-root */
  103. libssh2_uint64_t f_fsid; /* file system ID */
  104. libssh2_uint64_t f_flag; /* mount flags */
  105. libssh2_uint64_t f_namemax; /* maximum filename length */
  106. };
  107. /* SFTP filetypes */
  108. #define LIBSSH2_SFTP_TYPE_REGULAR 1
  109. #define LIBSSH2_SFTP_TYPE_DIRECTORY 2
  110. #define LIBSSH2_SFTP_TYPE_SYMLINK 3
  111. #define LIBSSH2_SFTP_TYPE_SPECIAL 4
  112. #define LIBSSH2_SFTP_TYPE_UNKNOWN 5
  113. #define LIBSSH2_SFTP_TYPE_SOCKET 6
  114. #define LIBSSH2_SFTP_TYPE_CHAR_DEVICE 7
  115. #define LIBSSH2_SFTP_TYPE_BLOCK_DEVICE 8
  116. #define LIBSSH2_SFTP_TYPE_FIFO 9
  117. /*
  118. * Reproduce the POSIX file modes here for systems that are not POSIX
  119. * compliant.
  120. *
  121. * These is used in "permissions" of "struct _LIBSSH2_SFTP_ATTRIBUTES"
  122. */
  123. /* File type */
  124. #define LIBSSH2_SFTP_S_IFMT 0170000 /* type of file mask */
  125. #define LIBSSH2_SFTP_S_IFIFO 0010000 /* named pipe (fifo) */
  126. #define LIBSSH2_SFTP_S_IFCHR 0020000 /* character special */
  127. #define LIBSSH2_SFTP_S_IFDIR 0040000 /* directory */
  128. #define LIBSSH2_SFTP_S_IFBLK 0060000 /* block special */
  129. #define LIBSSH2_SFTP_S_IFREG 0100000 /* regular */
  130. #define LIBSSH2_SFTP_S_IFLNK 0120000 /* symbolic link */
  131. #define LIBSSH2_SFTP_S_IFSOCK 0140000 /* socket */
  132. /* File mode */
  133. /* Read, write, execute/search by owner */
  134. #define LIBSSH2_SFTP_S_IRWXU 0000700 /* RWX mask for owner */
  135. #define LIBSSH2_SFTP_S_IRUSR 0000400 /* R for owner */
  136. #define LIBSSH2_SFTP_S_IWUSR 0000200 /* W for owner */
  137. #define LIBSSH2_SFTP_S_IXUSR 0000100 /* X for owner */
  138. /* Read, write, execute/search by group */
  139. #define LIBSSH2_SFTP_S_IRWXG 0000070 /* RWX mask for group */
  140. #define LIBSSH2_SFTP_S_IRGRP 0000040 /* R for group */
  141. #define LIBSSH2_SFTP_S_IWGRP 0000020 /* W for group */
  142. #define LIBSSH2_SFTP_S_IXGRP 0000010 /* X for group */
  143. /* Read, write, execute/search by others */
  144. #define LIBSSH2_SFTP_S_IRWXO 0000007 /* RWX mask for other */
  145. #define LIBSSH2_SFTP_S_IROTH 0000004 /* R for other */
  146. #define LIBSSH2_SFTP_S_IWOTH 0000002 /* W for other */
  147. #define LIBSSH2_SFTP_S_IXOTH 0000001 /* X for other */
  148. /* macros to check for specific file types, added in 1.2.5 */
  149. #define LIBSSH2_SFTP_S_ISLNK(m) \
  150. (((m) & LIBSSH2_SFTP_S_IFMT) == LIBSSH2_SFTP_S_IFLNK)
  151. #define LIBSSH2_SFTP_S_ISREG(m) \
  152. (((m) & LIBSSH2_SFTP_S_IFMT) == LIBSSH2_SFTP_S_IFREG)
  153. #define LIBSSH2_SFTP_S_ISDIR(m) \
  154. (((m) & LIBSSH2_SFTP_S_IFMT) == LIBSSH2_SFTP_S_IFDIR)
  155. #define LIBSSH2_SFTP_S_ISCHR(m) \
  156. (((m) & LIBSSH2_SFTP_S_IFMT) == LIBSSH2_SFTP_S_IFCHR)
  157. #define LIBSSH2_SFTP_S_ISBLK(m) \
  158. (((m) & LIBSSH2_SFTP_S_IFMT) == LIBSSH2_SFTP_S_IFBLK)
  159. #define LIBSSH2_SFTP_S_ISFIFO(m) \
  160. (((m) & LIBSSH2_SFTP_S_IFMT) == LIBSSH2_SFTP_S_IFIFO)
  161. #define LIBSSH2_SFTP_S_ISSOCK(m) \
  162. (((m) & LIBSSH2_SFTP_S_IFMT) == LIBSSH2_SFTP_S_IFSOCK)
  163. /* SFTP File Transfer Flags -- (e.g. flags parameter to sftp_open())
  164. * Danger will robinson... APPEND doesn't have any effect on OpenSSH servers */
  165. #define LIBSSH2_FXF_READ 0x00000001
  166. #define LIBSSH2_FXF_WRITE 0x00000002
  167. #define LIBSSH2_FXF_APPEND 0x00000004
  168. #define LIBSSH2_FXF_CREAT 0x00000008
  169. #define LIBSSH2_FXF_TRUNC 0x00000010
  170. #define LIBSSH2_FXF_EXCL 0x00000020
  171. /* SFTP Status Codes (returned by libssh2_sftp_last_error() ) */
  172. #define LIBSSH2_FX_OK 0UL
  173. #define LIBSSH2_FX_EOF 1UL
  174. #define LIBSSH2_FX_NO_SUCH_FILE 2UL
  175. #define LIBSSH2_FX_PERMISSION_DENIED 3UL
  176. #define LIBSSH2_FX_FAILURE 4UL
  177. #define LIBSSH2_FX_BAD_MESSAGE 5UL
  178. #define LIBSSH2_FX_NO_CONNECTION 6UL
  179. #define LIBSSH2_FX_CONNECTION_LOST 7UL
  180. #define LIBSSH2_FX_OP_UNSUPPORTED 8UL
  181. #define LIBSSH2_FX_INVALID_HANDLE 9UL
  182. #define LIBSSH2_FX_NO_SUCH_PATH 10UL
  183. #define LIBSSH2_FX_FILE_ALREADY_EXISTS 11UL
  184. #define LIBSSH2_FX_WRITE_PROTECT 12UL
  185. #define LIBSSH2_FX_NO_MEDIA 13UL
  186. #define LIBSSH2_FX_NO_SPACE_ON_FILESYSTEM 14UL
  187. #define LIBSSH2_FX_QUOTA_EXCEEDED 15UL
  188. #define LIBSSH2_FX_UNKNOWN_PRINCIPLE 16UL /* Initial mis-spelling */
  189. #define LIBSSH2_FX_UNKNOWN_PRINCIPAL 16UL
  190. #define LIBSSH2_FX_LOCK_CONFlICT 17UL /* Initial mis-spelling */
  191. #define LIBSSH2_FX_LOCK_CONFLICT 17UL
  192. #define LIBSSH2_FX_DIR_NOT_EMPTY 18UL
  193. #define LIBSSH2_FX_NOT_A_DIRECTORY 19UL
  194. #define LIBSSH2_FX_INVALID_FILENAME 20UL
  195. #define LIBSSH2_FX_LINK_LOOP 21UL
  196. /* Returned by any function that would block during a read/write operation */
  197. #define LIBSSH2SFTP_EAGAIN LIBSSH2_ERROR_EAGAIN
  198. /* SFTP API */
  199. LIBSSH2_API LIBSSH2_SFTP *libssh2_sftp_init(LIBSSH2_SESSION *session);
  200. LIBSSH2_API int libssh2_sftp_shutdown(LIBSSH2_SFTP *sftp);
  201. LIBSSH2_API unsigned long libssh2_sftp_last_error(LIBSSH2_SFTP *sftp);
  202. LIBSSH2_API LIBSSH2_CHANNEL *libssh2_sftp_get_channel(LIBSSH2_SFTP *sftp);
  203. /* File / Directory Ops */
  204. LIBSSH2_API LIBSSH2_SFTP_HANDLE *
  205. libssh2_sftp_open_ex(LIBSSH2_SFTP *sftp,
  206. const char *filename,
  207. unsigned int filename_len,
  208. unsigned long flags,
  209. long mode, int open_type);
  210. #define libssh2_sftp_open(sftp, filename, flags, mode) \
  211. libssh2_sftp_open_ex((sftp), \
  212. (filename), (unsigned int)strlen(filename), \
  213. (flags), (mode), LIBSSH2_SFTP_OPENFILE)
  214. #define libssh2_sftp_opendir(sftp, path) \
  215. libssh2_sftp_open_ex((sftp), \
  216. (path), (unsigned int)strlen(path), \
  217. 0, 0, LIBSSH2_SFTP_OPENDIR)
  218. LIBSSH2_API LIBSSH2_SFTP_HANDLE *
  219. libssh2_sftp_open_ex_r(LIBSSH2_SFTP *sftp,
  220. const char *filename,
  221. size_t filename_len,
  222. unsigned long flags,
  223. long mode, int open_type,
  224. LIBSSH2_SFTP_ATTRIBUTES *attrs);
  225. #define libssh2_sftp_open_r(sftp, filename, flags, mode, attrs) \
  226. libssh2_sftp_open_ex_r((sftp), (filename), strlen(filename), \
  227. (flags), (mode), LIBSSH2_SFTP_OPENFILE, \
  228. (attrs))
  229. LIBSSH2_API ssize_t libssh2_sftp_read(LIBSSH2_SFTP_HANDLE *handle,
  230. char *buffer, size_t buffer_maxlen);
  231. LIBSSH2_API int libssh2_sftp_readdir_ex(LIBSSH2_SFTP_HANDLE *handle, \
  232. char *buffer, size_t buffer_maxlen,
  233. char *longentry,
  234. size_t longentry_maxlen,
  235. LIBSSH2_SFTP_ATTRIBUTES *attrs);
  236. #define libssh2_sftp_readdir(handle, buffer, buffer_maxlen, attrs) \
  237. libssh2_sftp_readdir_ex((handle), (buffer), (buffer_maxlen), NULL, 0, \
  238. (attrs))
  239. LIBSSH2_API ssize_t libssh2_sftp_write(LIBSSH2_SFTP_HANDLE *handle,
  240. const char *buffer, size_t count);
  241. LIBSSH2_API int libssh2_sftp_fsync(LIBSSH2_SFTP_HANDLE *handle);
  242. LIBSSH2_API int libssh2_sftp_close_handle(LIBSSH2_SFTP_HANDLE *handle);
  243. #define libssh2_sftp_close(handle) libssh2_sftp_close_handle(handle)
  244. #define libssh2_sftp_closedir(handle) libssh2_sftp_close_handle(handle)
  245. LIBSSH2_API void libssh2_sftp_seek(LIBSSH2_SFTP_HANDLE *handle, size_t offset);
  246. LIBSSH2_API void libssh2_sftp_seek64(LIBSSH2_SFTP_HANDLE *handle,
  247. libssh2_uint64_t offset);
  248. #define libssh2_sftp_rewind(handle) libssh2_sftp_seek64((handle), 0)
  249. LIBSSH2_API size_t libssh2_sftp_tell(LIBSSH2_SFTP_HANDLE *handle);
  250. LIBSSH2_API libssh2_uint64_t libssh2_sftp_tell64(LIBSSH2_SFTP_HANDLE *handle);
  251. LIBSSH2_API int libssh2_sftp_fstat_ex(LIBSSH2_SFTP_HANDLE *handle,
  252. LIBSSH2_SFTP_ATTRIBUTES *attrs,
  253. int setstat);
  254. #define libssh2_sftp_fstat(handle, attrs) \
  255. libssh2_sftp_fstat_ex((handle), (attrs), 0)
  256. #define libssh2_sftp_fsetstat(handle, attrs) \
  257. libssh2_sftp_fstat_ex((handle), (attrs), 1)
  258. /* Miscellaneous Ops */
  259. LIBSSH2_API int libssh2_sftp_rename_ex(LIBSSH2_SFTP *sftp,
  260. const char *source_filename,
  261. unsigned int srouce_filename_len,
  262. const char *dest_filename,
  263. unsigned int dest_filename_len,
  264. long flags);
  265. #define libssh2_sftp_rename(sftp, sourcefile, destfile) \
  266. libssh2_sftp_rename_ex((sftp), \
  267. (sourcefile), (unsigned int)strlen(sourcefile), \
  268. (destfile), (unsigned int)strlen(destfile), \
  269. LIBSSH2_SFTP_RENAME_OVERWRITE | \
  270. LIBSSH2_SFTP_RENAME_ATOMIC | \
  271. LIBSSH2_SFTP_RENAME_NATIVE)
  272. LIBSSH2_API int libssh2_sftp_posix_rename_ex(LIBSSH2_SFTP *sftp,
  273. const char *source_filename,
  274. size_t srouce_filename_len,
  275. const char *dest_filename,
  276. size_t dest_filename_len);
  277. #define libssh2_sftp_posix_rename(sftp, sourcefile, destfile) \
  278. libssh2_sftp_posix_rename_ex((sftp), (sourcefile), strlen(sourcefile), \
  279. (destfile), strlen(destfile))
  280. LIBSSH2_API int libssh2_sftp_unlink_ex(LIBSSH2_SFTP *sftp,
  281. const char *filename,
  282. unsigned int filename_len);
  283. #define libssh2_sftp_unlink(sftp, filename) \
  284. libssh2_sftp_unlink_ex((sftp), (filename), (unsigned int)strlen(filename))
  285. LIBSSH2_API int libssh2_sftp_fstatvfs(LIBSSH2_SFTP_HANDLE *handle,
  286. LIBSSH2_SFTP_STATVFS *st);
  287. LIBSSH2_API int libssh2_sftp_statvfs(LIBSSH2_SFTP *sftp,
  288. const char *path,
  289. size_t path_len,
  290. LIBSSH2_SFTP_STATVFS *st);
  291. LIBSSH2_API int libssh2_sftp_mkdir_ex(LIBSSH2_SFTP *sftp,
  292. const char *path,
  293. unsigned int path_len, long mode);
  294. #define libssh2_sftp_mkdir(sftp, path, mode) \
  295. libssh2_sftp_mkdir_ex((sftp), (path), (unsigned int)strlen(path), (mode))
  296. LIBSSH2_API int libssh2_sftp_rmdir_ex(LIBSSH2_SFTP *sftp,
  297. const char *path,
  298. unsigned int path_len);
  299. #define libssh2_sftp_rmdir(sftp, path) \
  300. libssh2_sftp_rmdir_ex((sftp), (path), (unsigned int)strlen(path))
  301. LIBSSH2_API int libssh2_sftp_stat_ex(LIBSSH2_SFTP *sftp,
  302. const char *path,
  303. unsigned int path_len,
  304. int stat_type,
  305. LIBSSH2_SFTP_ATTRIBUTES *attrs);
  306. #define libssh2_sftp_stat(sftp, path, attrs) \
  307. libssh2_sftp_stat_ex((sftp), (path), (unsigned int)strlen(path), \
  308. LIBSSH2_SFTP_STAT, (attrs))
  309. #define libssh2_sftp_lstat(sftp, path, attrs) \
  310. libssh2_sftp_stat_ex((sftp), (path), (unsigned int)strlen(path), \
  311. LIBSSH2_SFTP_LSTAT, (attrs))
  312. #define libssh2_sftp_setstat(sftp, path, attrs) \
  313. libssh2_sftp_stat_ex((sftp), (path), (unsigned int)strlen(path), \
  314. LIBSSH2_SFTP_SETSTAT, (attrs))
  315. LIBSSH2_API int libssh2_sftp_symlink_ex(LIBSSH2_SFTP *sftp,
  316. const char *path,
  317. unsigned int path_len,
  318. char *target,
  319. unsigned int target_len,
  320. int link_type);
  321. #define libssh2_sftp_symlink(sftp, orig, linkpath) \
  322. libssh2_sftp_symlink_ex((sftp), \
  323. (orig), (unsigned int)strlen(orig), \
  324. (linkpath), (unsigned int)strlen(linkpath), \
  325. LIBSSH2_SFTP_SYMLINK)
  326. #define libssh2_sftp_readlink(sftp, path, target, maxlen) \
  327. libssh2_sftp_symlink_ex((sftp), \
  328. (path), (unsigned int)strlen(path), \
  329. (target), (maxlen), \
  330. LIBSSH2_SFTP_READLINK)
  331. #define libssh2_sftp_realpath(sftp, path, target, maxlen) \
  332. libssh2_sftp_symlink_ex((sftp), \
  333. (path), (unsigned int)strlen(path), \
  334. (target), (maxlen), \
  335. LIBSSH2_SFTP_REALPATH)
  336. #ifdef __cplusplus
  337. } /* extern "C" */
  338. #endif
  339. #endif /* LIBSSH2_SFTP_H */