db_files.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * db_files.c
  15. *
  16. * This file contains operands to handle streams with URIs that start with
  17. * "file:" or "dir:".
  18. */
  19. #include <linux/types.h>
  20. #include "api.h"
  21. #include "assert.h"
  22. #include "pal.h"
  23. #include "pal_debug.h"
  24. #include "pal_defs.h"
  25. #include "pal_error.h"
  26. #include "pal_internal.h"
  27. #include "pal_linux.h"
  28. #include "pal_linux_defs.h"
  29. #include "pal_linux_error.h"
  30. typedef __kernel_pid_t pid_t;
  31. #undef __GLIBC__
  32. #include <asm/fcntl.h>
  33. #include <asm/stat.h>
  34. #include <linux/fs.h>
  35. #include <linux/stat.h>
  36. #include "enclave_pages.h"
  37. /* 'open' operation for file streams */
  38. static int file_open(PAL_HANDLE* handle, const char* type, const char* uri, int access, int share,
  39. int create, int options) {
  40. if (strcmp_static(type, "file"))
  41. return -PAL_ERROR_INVAL;
  42. /* try to do the real open */
  43. int fd = ocall_open(uri, access | create | options, share);
  44. if (IS_ERR(fd))
  45. return unix_to_pal_error(ERRNO(fd));
  46. /* if try_create_path succeeded, prepare for the file handle */
  47. size_t len = strlen(uri) + 1;
  48. PAL_HANDLE hdl = malloc(HANDLE_SIZE(file) + len);
  49. SET_HANDLE_TYPE(hdl, file);
  50. HANDLE_HDR(hdl)->flags |= RFD(0) | WFD(0) | WRITABLE(0);
  51. hdl->file.fd = fd;
  52. char* path = (void*)hdl + HANDLE_SIZE(file);
  53. int ret;
  54. if ((ret = get_norm_path(uri, path, &len)) < 0) {
  55. SGX_DBG(DBG_E, "Could not normalize path (%s): %s\n", uri, pal_strerror(ret));
  56. free(hdl);
  57. return ret;
  58. }
  59. hdl->file.realpath = (PAL_STR)path;
  60. sgx_stub_t* stubs;
  61. uint64_t total;
  62. ret = load_trusted_file(hdl, &stubs, &total, create);
  63. if (ret < 0) {
  64. SGX_DBG(DBG_E,
  65. "Accessing file:%s is denied. (%s) "
  66. "This file is not trusted or allowed.\n",
  67. hdl->file.realpath, pal_strerror(ret));
  68. free(hdl);
  69. return ret;
  70. }
  71. hdl->file.stubs = (PAL_PTR)stubs;
  72. hdl->file.total = total;
  73. hdl->file.offset = 0;
  74. if (hdl->file.stubs) {
  75. /* case of trusted file: mmap the whole file in untrusted memory for future reads/writes */
  76. ret = ocall_map_untrusted(hdl->file.fd, 0, hdl->file.total, PROT_READ, &hdl->file.umem);
  77. if (IS_ERR(ret)) {
  78. /* note that we don't free stubs because they are re-used in same trusted file */
  79. free(hdl);
  80. return unix_to_pal_error(ERRNO(ret));
  81. }
  82. }
  83. *handle = hdl;
  84. return 0;
  85. }
  86. /* 'read' operation for file streams. */
  87. static int64_t file_read(PAL_HANDLE handle, uint64_t offset, uint64_t count, void* buffer) {
  88. int64_t ret;
  89. sgx_stub_t* stubs = (sgx_stub_t*)handle->file.stubs;
  90. if (!stubs) {
  91. /* case of allowed file: emulate via lseek + read */
  92. if (handle->file.offset != offset) {
  93. ret = ocall_lseek(handle->file.fd, offset, SEEK_SET);
  94. if (IS_ERR(ret))
  95. return -PAL_ERROR_DENIED;
  96. handle->file.offset = offset;
  97. }
  98. ret = ocall_read(handle->file.fd, buffer, count);
  99. if (IS_ERR(ret))
  100. return unix_to_pal_error(ERRNO(ret));
  101. handle->file.offset = offset + ret;
  102. return ret;
  103. }
  104. /* case of trusted file: already mmaped in umem, copy from there and verify hash */
  105. uint64_t total = handle->file.total;
  106. if (offset >= total)
  107. return 0;
  108. static_assert((TRUSTED_STUB_SIZE & (TRUSTED_STUB_SIZE - 1)) == 0,
  109. "TRUSTED_STUB_SIZE must be a power of two");
  110. uint64_t end = (offset + count > total) ? total : offset + count;
  111. uint64_t map_start = offset & ~(TRUSTED_STUB_SIZE - 1);
  112. uint64_t map_end = (end + TRUSTED_STUB_SIZE - 1) & ~(TRUSTED_STUB_SIZE - 1);
  113. if (map_end > total)
  114. map_end = ALLOC_ALIGNUP(total);
  115. ret = copy_and_verify_trusted_file(handle->file.realpath, handle->file.umem + map_start,
  116. map_start, map_end, buffer, offset, end - offset, stubs, total);
  117. if (ret < 0)
  118. return ret;
  119. return end - offset;
  120. }
  121. /* 'write' operation for file streams. */
  122. static int64_t file_write(PAL_HANDLE handle, uint64_t offset, uint64_t count, const void* buffer) {
  123. int64_t ret;
  124. sgx_stub_t* stubs = (sgx_stub_t*)handle->file.stubs;
  125. if (!stubs) {
  126. /* case of allowed file: emulate via lseek + write */
  127. if (handle->file.offset != offset) {
  128. ret = ocall_lseek(handle->file.fd, offset, SEEK_SET);
  129. if (IS_ERR(ret))
  130. return -PAL_ERROR_DENIED;
  131. handle->file.offset = offset;
  132. }
  133. ret = ocall_write(handle->file.fd, buffer, count);
  134. if (IS_ERR(ret))
  135. return unix_to_pal_error(ERRNO(ret));
  136. handle->file.offset = offset + ret;
  137. return ret;
  138. }
  139. /* case of trusted file: disallow writing completely */
  140. SGX_DBG(DBG_E, "Writing to a trusted file (%s) is disallowed!\n", handle->file.realpath);
  141. return -PAL_ERROR_DENIED;
  142. }
  143. /* 'close' operation for file streams. In this case, it will only
  144. close the file without deleting it. */
  145. static int file_close(PAL_HANDLE handle) {
  146. int fd = handle->file.fd;
  147. if (handle->file.stubs) {
  148. /* case of trusted file: the whole file was mmapped in untrusted memory */
  149. ocall_unmap_untrusted(handle->file.umem, handle->file.total);
  150. }
  151. ocall_close(fd);
  152. /* initial realpath is part of handle object and will be freed with it */
  153. if (handle->file.realpath && handle->file.realpath != (void*)handle + HANDLE_SIZE(file))
  154. free((void*)handle->file.realpath);
  155. return 0;
  156. }
  157. /* 'delete' operation for file streams. It will actually delete
  158. the file if we can successfully close it. */
  159. static int file_delete(PAL_HANDLE handle, int access) {
  160. if (access)
  161. return -PAL_ERROR_INVAL;
  162. int ret = ocall_delete(handle->file.realpath);
  163. return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : ret;
  164. }
  165. /* 'map' operation for file stream. */
  166. static int file_map(PAL_HANDLE handle, void** addr, int prot, uint64_t offset, uint64_t size) {
  167. sgx_stub_t* stubs = (sgx_stub_t*)handle->file.stubs;
  168. uint64_t total = handle->file.total;
  169. void* mem = *addr;
  170. void* umem;
  171. int ret;
  172. /*
  173. * If the file is listed in the manifest as an "allowed" file,
  174. * we allow mapping the file outside the enclave, if the library OS
  175. * does not request a specific address.
  176. */
  177. if (!mem && !stubs && !(prot & PAL_PROT_WRITECOPY)) {
  178. ret = ocall_map_untrusted(handle->file.fd, offset, size, HOST_PROT(prot), &mem);
  179. if (!IS_ERR(ret))
  180. *addr = mem;
  181. return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : ret;
  182. }
  183. if (!(prot & PAL_PROT_WRITECOPY) && (prot & PAL_PROT_WRITE)) {
  184. SGX_DBG(DBG_E,
  185. "file_map does not currently support writable pass-through mappings on SGX. You "
  186. "may add the PAL_PROT_WRITECOPY (MAP_PRIVATE) flag to your file mapping to keep "
  187. "the writes inside the enclave but they won't be reflected outside of the "
  188. "enclave.\n");
  189. return -PAL_ERROR_DENIED;
  190. }
  191. mem = get_reserved_pages(mem, size);
  192. if (!mem)
  193. return -PAL_ERROR_NOMEM;
  194. uint64_t end = (offset + size > total) ? total : offset + size;
  195. uint64_t map_start, map_end;
  196. if (stubs) {
  197. static_assert((TRUSTED_STUB_SIZE & (TRUSTED_STUB_SIZE - 1)) == 0,
  198. "TRUSTED_STUB_SIZE must be a power of two");
  199. map_start = offset & ~(TRUSTED_STUB_SIZE - 1);
  200. map_end = (end + TRUSTED_STUB_SIZE - 1) & ~(TRUSTED_STUB_SIZE - 1);
  201. } else {
  202. map_start = ALLOC_ALIGNDOWN(offset);
  203. map_end = ALLOC_ALIGNUP(end);
  204. }
  205. ret = ocall_map_untrusted(handle->file.fd, map_start, map_end - map_start, PROT_READ, &umem);
  206. if (IS_ERR(ret)) {
  207. SGX_DBG(DBG_E, "file_map - ocall returned %d\n", ret);
  208. return unix_to_pal_error(ERRNO(ret));
  209. }
  210. if (stubs) {
  211. ret = copy_and_verify_trusted_file(handle->file.realpath, umem, map_start, map_end, mem,
  212. offset, end - offset, stubs, total);
  213. if (ret < 0) {
  214. SGX_DBG(DBG_E, "file_map - verify trusted returned %d\n", ret);
  215. ocall_unmap_untrusted(umem, map_end - map_start);
  216. return ret;
  217. }
  218. } else {
  219. memcpy(mem, umem + (offset - map_start), end - offset);
  220. }
  221. ocall_unmap_untrusted(umem, map_end - map_start);
  222. *addr = mem;
  223. return 0;
  224. }
  225. /* 'setlength' operation for file stream. */
  226. static int64_t file_setlength(PAL_HANDLE handle, uint64_t length) {
  227. int ret = ocall_ftruncate(handle->file.fd, length);
  228. if (IS_ERR(ret))
  229. return unix_to_pal_error(ERRNO(ret));
  230. handle->file.total = length;
  231. return (int64_t)length;
  232. }
  233. /* 'flush' operation for file stream. */
  234. static int file_flush(PAL_HANDLE handle) {
  235. ocall_fsync(handle->file.fd);
  236. return 0;
  237. }
  238. static inline int file_stat_type(struct stat* stat) {
  239. if (S_ISREG(stat->st_mode))
  240. return pal_type_file;
  241. if (S_ISDIR(stat->st_mode))
  242. return pal_type_dir;
  243. if (S_ISCHR(stat->st_mode))
  244. return pal_type_dev;
  245. if (S_ISFIFO(stat->st_mode))
  246. return pal_type_pipe;
  247. if (S_ISSOCK(stat->st_mode))
  248. return pal_type_dev;
  249. return 0;
  250. }
  251. /* copy attr content from POSIX stat struct to PAL_STREAM_ATTR */
  252. static inline void file_attrcopy(PAL_STREAM_ATTR* attr, struct stat* stat) {
  253. attr->handle_type = file_stat_type(stat);
  254. attr->disconnected = PAL_FALSE;
  255. attr->nonblocking = PAL_FALSE;
  256. attr->readable = stataccess(stat, ACCESS_R);
  257. attr->writable = stataccess(stat, ACCESS_W);
  258. attr->runnable = stataccess(stat, ACCESS_X);
  259. attr->share_flags = stat->st_mode;
  260. attr->pending_size = stat->st_size;
  261. }
  262. /* 'attrquery' operation for file streams */
  263. static int file_attrquery(const char* type, const char* uri, PAL_STREAM_ATTR* attr) {
  264. if (strcmp_static(type, "file") && strcmp_static(type, "dir"))
  265. return -PAL_ERROR_INVAL;
  266. /* try to do the real open */
  267. int fd = ocall_open(uri, 0, 0);
  268. if (IS_ERR(fd))
  269. return unix_to_pal_error(ERRNO(fd));
  270. struct stat stat_buf;
  271. int ret = ocall_fstat(fd, &stat_buf);
  272. ocall_close(fd);
  273. /* if it failed, return the right error code */
  274. if (IS_ERR(ret))
  275. return unix_to_pal_error(ERRNO(ret));
  276. file_attrcopy(attr, &stat_buf);
  277. return 0;
  278. }
  279. /* 'attrquerybyhdl' operation for file streams */
  280. static int file_attrquerybyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) {
  281. int fd = handle->file.fd;
  282. struct stat stat_buf;
  283. int ret = ocall_fstat(fd, &stat_buf);
  284. if (IS_ERR(ret))
  285. return unix_to_pal_error(ERRNO(ret));
  286. file_attrcopy(attr, &stat_buf);
  287. return 0;
  288. }
  289. static int file_attrsetbyhdl(PAL_HANDLE handle, PAL_STREAM_ATTR* attr) {
  290. int fd = handle->file.fd;
  291. int ret = ocall_fchmod(fd, attr->share_flags | 0600);
  292. if (IS_ERR(ret))
  293. return unix_to_pal_error(ERRNO(ret));
  294. return 0;
  295. }
  296. static int file_rename(PAL_HANDLE handle, const char* type, const char* uri) {
  297. if (strcmp_static(type, "file"))
  298. return -PAL_ERROR_INVAL;
  299. char* tmp = strdup(uri);
  300. if (!tmp)
  301. return -PAL_ERROR_NOMEM;
  302. int ret = ocall_rename(handle->file.realpath, uri);
  303. if (IS_ERR(ret)) {
  304. free(tmp);
  305. return unix_to_pal_error(ERRNO(ret));
  306. }
  307. /* initial realpath is part of handle object and will be freed with it */
  308. if (handle->file.realpath && handle->file.realpath != (void*)handle + HANDLE_SIZE(file)) {
  309. free((void*)handle->file.realpath);
  310. }
  311. handle->file.realpath = tmp;
  312. return 0;
  313. }
  314. static int file_getname(PAL_HANDLE handle, char* buffer, size_t count) {
  315. if (!handle->file.realpath)
  316. return 0;
  317. int len = strlen(handle->file.realpath);
  318. char* tmp = strcpy_static(buffer, "file:", count);
  319. if (!tmp || buffer + count < tmp + len + 1)
  320. return -PAL_ERROR_TOOLONG;
  321. memcpy(tmp, handle->file.realpath, len + 1);
  322. return tmp + len - buffer;
  323. }
  324. const char* file_getrealpath(PAL_HANDLE handle) {
  325. return handle->file.realpath;
  326. }
  327. struct handle_ops file_ops = {
  328. .getname = &file_getname,
  329. .getrealpath = &file_getrealpath,
  330. .open = &file_open,
  331. .read = &file_read,
  332. .write = &file_write,
  333. .close = &file_close,
  334. .delete = &file_delete,
  335. .map = &file_map,
  336. .setlength = &file_setlength,
  337. .flush = &file_flush,
  338. .attrquery = &file_attrquery,
  339. .attrquerybyhdl = &file_attrquerybyhdl,
  340. .attrsetbyhdl = &file_attrsetbyhdl,
  341. .rename = &file_rename,
  342. };
  343. /* 'open' operation for directory stream. Directory stream does not have a
  344. specific type prefix, its URI looks the same file streams, plus it
  345. ended with slashes. dir_open will be called by file_open. */
  346. static int dir_open(PAL_HANDLE* handle, const char* type, const char* uri, int access, int share,
  347. int create, int options) {
  348. if (strcmp_static(type, "dir"))
  349. return -PAL_ERROR_INVAL;
  350. if (!WITHIN_MASK(access, PAL_ACCESS_MASK))
  351. return -PAL_ERROR_INVAL;
  352. int ret;
  353. if (create & PAL_CREATE_TRY) {
  354. ret = ocall_mkdir(uri, share);
  355. if (IS_ERR(ret) && ERRNO(ret) == EEXIST && create & PAL_CREATE_ALWAYS)
  356. return -PAL_ERROR_STREAMEXIST;
  357. }
  358. ret = ocall_open(uri, O_DIRECTORY | options, 0);
  359. if (IS_ERR(ret))
  360. return unix_to_pal_error(ERRNO(ret));
  361. int len = strlen(uri);
  362. PAL_HANDLE hdl = malloc(HANDLE_SIZE(dir) + len + 1);
  363. SET_HANDLE_TYPE(hdl, dir);
  364. HANDLE_HDR(hdl)->flags |= RFD(0);
  365. hdl->dir.fd = ret;
  366. char* path = (void*)hdl + HANDLE_SIZE(dir);
  367. memcpy(path, uri, len + 1);
  368. hdl->dir.realpath = (PAL_STR)path;
  369. hdl->dir.buf = (PAL_PTR)NULL;
  370. hdl->dir.ptr = (PAL_PTR)NULL;
  371. hdl->dir.end = (PAL_PTR)NULL;
  372. hdl->dir.endofstream = PAL_FALSE;
  373. *handle = hdl;
  374. return 0;
  375. }
  376. #define DIRBUF_SIZE 1024
  377. static inline bool is_dot_or_dotdot(const char* name) {
  378. return (name[0] == '.' && !name[1]) || (name[0] == '.' && name[1] == '.' && !name[2]);
  379. }
  380. /* 'read' operation for directory stream. Directory stream will not
  381. need a 'write' operation. */
  382. static int64_t dir_read(PAL_HANDLE handle, uint64_t offset, size_t count, void* _buf) {
  383. size_t bytes_written = 0;
  384. char* buf = (char*)_buf;
  385. if (offset) {
  386. return -PAL_ERROR_INVAL;
  387. }
  388. if (handle->dir.endofstream == PAL_TRUE) {
  389. return -PAL_ERROR_ENDOFSTREAM;
  390. }
  391. while (1) {
  392. while ((char*)handle->dir.ptr < (char*)handle->dir.end) {
  393. struct linux_dirent64* dirent = (struct linux_dirent64*)handle->dir.ptr;
  394. if (is_dot_or_dotdot(dirent->d_name)) {
  395. goto skip;
  396. }
  397. bool is_dir = dirent->d_type == DT_DIR;
  398. size_t len = strlen(dirent->d_name);
  399. if (len + 1 + (is_dir ? 1 : 0) > count) {
  400. goto out;
  401. }
  402. memcpy(buf, dirent->d_name, len);
  403. if (is_dir) {
  404. buf[len++] = '/';
  405. }
  406. buf[len++] = '\0';
  407. buf += len;
  408. bytes_written += len;
  409. count -= len;
  410. skip:
  411. handle->dir.ptr = (char*)handle->dir.ptr + dirent->d_reclen;
  412. }
  413. if (!count) {
  414. /* No space left, returning */
  415. goto out;
  416. }
  417. if (!handle->dir.buf) {
  418. handle->dir.buf = (PAL_PTR)malloc(DIRBUF_SIZE);
  419. if (!handle->dir.buf) {
  420. return -PAL_ERROR_NOMEM;
  421. }
  422. }
  423. int size = ocall_getdents(handle->dir.fd, handle->dir.buf, DIRBUF_SIZE);
  424. if (IS_ERR(size)) {
  425. /*
  426. * If something was written just return that and pretend no error
  427. * was seen - it will be caught next time.
  428. */
  429. if (bytes_written) {
  430. return bytes_written;
  431. }
  432. return unix_to_pal_error(ERRNO(size));
  433. }
  434. if (!size) {
  435. handle->dir.endofstream = PAL_TRUE;
  436. goto out;
  437. }
  438. handle->dir.ptr = handle->dir.buf;
  439. handle->dir.end = (char*)handle->dir.buf + size;
  440. }
  441. out:
  442. return (int64_t)bytes_written ?: -PAL_ERROR_ENDOFSTREAM;
  443. }
  444. /* 'close' operation of directory streams */
  445. static int dir_close(PAL_HANDLE handle) {
  446. int fd = handle->dir.fd;
  447. ocall_close(fd);
  448. if (handle->dir.buf) {
  449. free((void*)handle->dir.buf);
  450. handle->dir.buf = handle->dir.ptr = handle->dir.end = (PAL_PTR)NULL;
  451. }
  452. /* initial realpath is part of handle object and will be freed with it */
  453. if (handle->dir.realpath && handle->dir.realpath != (void*)handle + HANDLE_SIZE(dir))
  454. free((void*)handle->dir.realpath);
  455. return 0;
  456. }
  457. /* 'delete' operation of directoy streams */
  458. static int dir_delete(PAL_HANDLE handle, int access) {
  459. if (access)
  460. return -PAL_ERROR_INVAL;
  461. int ret = dir_close(handle);
  462. if (ret < 0)
  463. return ret;
  464. ret = ocall_delete(handle->dir.realpath);
  465. return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : ret;
  466. }
  467. static int dir_rename(PAL_HANDLE handle, const char* type, const char* uri) {
  468. if (strcmp_static(type, "dir"))
  469. return -PAL_ERROR_INVAL;
  470. char* tmp = strdup(uri);
  471. if (!tmp)
  472. return -PAL_ERROR_NOMEM;
  473. int ret = ocall_rename(handle->dir.realpath, uri);
  474. if (IS_ERR(ret)) {
  475. free(tmp);
  476. return unix_to_pal_error(ERRNO(ret));
  477. }
  478. /* initial realpath is part of handle object and will be freed with it */
  479. if (handle->dir.realpath && handle->dir.realpath != (void*)handle + HANDLE_SIZE(dir)) {
  480. free((void*)handle->dir.realpath);
  481. }
  482. handle->dir.realpath = tmp;
  483. return 0;
  484. }
  485. static int dir_getname(PAL_HANDLE handle, char* buffer, size_t count) {
  486. if (!handle->dir.realpath)
  487. return 0;
  488. size_t len = strlen(handle->dir.realpath);
  489. char* tmp = strcpy_static(buffer, "dir:", count);
  490. if (!tmp || buffer + count < tmp + len + 1)
  491. return -PAL_ERROR_TOOLONG;
  492. memcpy(tmp, handle->dir.realpath, len + 1);
  493. return tmp + len - buffer;
  494. if (len + 6 >= count)
  495. return -PAL_ERROR_TOOLONG;
  496. }
  497. static const char* dir_getrealpath(PAL_HANDLE handle) {
  498. return handle->dir.realpath;
  499. }
  500. struct handle_ops dir_ops = {
  501. .getname = &dir_getname,
  502. .getrealpath = &dir_getrealpath,
  503. .open = &dir_open,
  504. .read = &dir_read,
  505. .close = &dir_close,
  506. .delete = &dir_delete,
  507. .attrquery = &file_attrquery,
  508. .attrquerybyhdl = &file_attrquerybyhdl,
  509. .attrsetbyhdl = &file_attrsetbyhdl,
  510. .rename = &dir_rename,
  511. };