db_files.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. #undef __GLIBC__
  20. #include "pal_defs.h"
  21. #include "pal_freebsd_defs.h"
  22. #include "pal.h"
  23. #include "pal_internal.h"
  24. #include "pal_freebsd.h"
  25. #include "pal_debug.h"
  26. #include "pal_error.h"
  27. #include "api.h"
  28. #include <sys/stat.h>
  29. #include <sys/fcntl.h>
  30. #include <errno.h>
  31. #include <sys/types.h>
  32. typedef __kernel_pid_t pid_t;
  33. /* 'open' operation for file streams */
  34. static int file_open (PAL_HANDLE * handle, const char * type, const char * uri,
  35. int access, int share, int create, int options)
  36. {
  37. int flags = HOST_FILE_OPEN(access, create, options);
  38. int mode = HOST_PERM(share);
  39. /* try to do the real open */
  40. int ret = INLINE_SYSCALL(open, 3, uri, flags|O_CLOEXEC, mode);
  41. if (IS_ERR(ret))
  42. return unix_to_pal_error(ERRNO(ret));
  43. /* if try_create_path succeeded, prepare for the file handle */
  44. int len = strlen(uri);
  45. PAL_HANDLE hdl = malloc(HANDLE_SIZE(file) + len + 1);
  46. SET_HANDLE_TYPE(hdl, file);
  47. hdl->hdr.flags |= RFD(0)|WFD(0)|WRITABLE(0);
  48. hdl->file.fd = ret;
  49. hdl->file.offset = 0;
  50. hdl->file.append = 0;
  51. hdl->file.pass = 0;
  52. char * path = (void *) hdl + HANDLE_SIZE(file);
  53. memcpy(path, uri, len + 1);
  54. hdl->file.realpath = path;
  55. *handle = hdl;
  56. return 0;
  57. }
  58. #ifndef SEEK_SET
  59. # define SEEK_SET 0
  60. #endif
  61. /* 'read' operation for file streams. */
  62. static int file_read (PAL_HANDLE handle, int offset, int count,
  63. void * buffer)
  64. {
  65. int fd = handle->file.fd;
  66. int ret;
  67. if (handle->file.offset != offset) {
  68. ret = INLINE_SYSCALL(lseek, 3, fd, offset, SEEK_SET);
  69. if (IS_ERR(ret))
  70. return -PAL_ERROR_DENIED;
  71. handle->file.offset = offset;
  72. }
  73. ret = INLINE_SYSCALL(read, 3, fd, buffer, count);
  74. if (IS_ERR(ret))
  75. return unix_to_pal_error(ERRNO(ret));
  76. handle->file.offset = offset + ret;
  77. return ret;
  78. }
  79. /* 'write' operation for file streams. */
  80. static int file_write (PAL_HANDLE handle, int offset, int count,
  81. const void * buffer)
  82. {
  83. int fd = handle->file.fd;
  84. int ret;
  85. if (handle->file.offset != offset) {
  86. ret = INLINE_SYSCALL(lseek, 3, fd, offset, SEEK_SET);
  87. if (IS_ERR(ret))
  88. return -PAL_ERROR_DENIED;
  89. handle->file.offset = offset;
  90. }
  91. ret = INLINE_SYSCALL(write, 3, fd, buffer, count);
  92. if (IS_ERR(ret))
  93. return unix_to_pal_error(ERRNO(ret));
  94. handle->file.offset = offset + ret;
  95. return ret;
  96. }
  97. /* 'close' operation for file streams. In this case, it will only
  98. close the file withou deleting it. */
  99. static int file_close (PAL_HANDLE handle)
  100. {
  101. int fd = handle->file.fd;
  102. int ret = INLINE_SYSCALL(close, 1, fd);
  103. /* initial realpath is part of handle object and will be freed with it */
  104. if (handle->file.realpath &&
  105. handle->file.realpath != (void *) handle + HANDLE_SIZE(file)) {
  106. free((void *) handle->file.realpath);
  107. }
  108. return IS_ERR(ret) ? unix_to_pal_error(ERRNO(ret)) : 0;
  109. }
  110. /* 'delete' operation for file streams. It will actually delete
  111. the file if we can successfully close it. */
  112. static int file_delete (PAL_HANDLE handle, int access)
  113. {
  114. if (access)
  115. return -PAL_ERROR_INVAL;
  116. INLINE_SYSCALL(unlink, 1, handle->file.realpath);
  117. return 0;
  118. }
  119. /* 'map' operation for file stream. */
  120. static int file_map (PAL_HANDLE handle, void ** addr, int prot,
  121. int offset, int size)
  122. {
  123. int fd = handle->file.fd;
  124. void * mem = *addr;
  125. int flags = MAP_FILE|HOST_FLAGS(0, prot)|(mem ? MAP_FIXED : 0);
  126. prot = HOST_PROT(prot);
  127. /* The memory will always allocated with flag MAP_PRIVATE
  128. and MAP_FILE */
  129. mem = (void *) ARCH_MMAP(mem, size, prot, flags, fd, offset);
  130. if (IS_ERR_P(mem))
  131. return -PAL_ERROR_DENIED;
  132. *addr = mem;
  133. return 0;
  134. }
  135. /* 'setlength' operation for file stream. */
  136. static int file_setlength (PAL_HANDLE handle, int length)
  137. {
  138. int ret = INLINE_SYSCALL(ftruncate, 2, handle->file.fd, length);
  139. if (IS_ERR(ret))
  140. return (ERRNO(ret) == EINVAL || ERRNO(ret) == EBADF) ?
  141. -PAL_ERROR_BADHANDLE : -PAL_ERROR_DENIED;
  142. return length;
  143. }
  144. /* 'flush' operation for file stream. */
  145. static int file_flush (PAL_HANDLE handle)
  146. {
  147. int ret = INLINE_SYSCALL(fsync, 1, handle->file.fd);
  148. if (IS_ERR(ret))
  149. return (ERRNO(ret) == EINVAL || ERRNO(ret) == EBADF) ?
  150. -PAL_ERROR_BADHANDLE : -PAL_ERROR_DENIED;
  151. return 0;
  152. }
  153. static inline int file_stat_type (struct stat * stat)
  154. {
  155. if (S_ISREG(stat->st_mode))
  156. return pal_type_file;
  157. if (S_ISDIR(stat->st_mode))
  158. return pal_type_dir;
  159. if (S_ISCHR(stat->st_mode))
  160. return pal_type_dev;
  161. if (S_ISFIFO(stat->st_mode))
  162. return pal_type_pipe;
  163. if (S_ISSOCK(stat->st_mode))
  164. return pal_type_dev;
  165. return 0;
  166. }
  167. /* copy attr content from POSIX stat struct to PAL_STREAM_ATTR */
  168. static inline void
  169. file_attrcopy (PAL_STREAM_ATTR * attr, struct stat * stat)
  170. {
  171. attr->handle_type = file_stat_type(stat);
  172. attr->disconnected = PAL_FALSE;
  173. attr->nonblocking = PAL_FALSE;
  174. attr->readable = stataccess(stat, ACCESS_R);
  175. attr->writable = stataccess(stat, ACCESS_W);
  176. attr->runnable = stataccess(stat, ACCESS_X);
  177. attr->share_flags = stat->st_mode;
  178. attr->pending_size = stat->st_size;
  179. }
  180. /* 'attrquery' operation for file streams */
  181. static int file_attrquery (const char * type, const char * uri,
  182. PAL_STREAM_ATTR * attr)
  183. {
  184. struct stat stat_buf;
  185. /* try to do the real open */
  186. int ret = INLINE_SYSCALL(stat, 2, uri, &stat_buf);
  187. /* if it failed, return the right error code */
  188. if (IS_ERR(ret))
  189. return unix_to_pal_error(ERRNO(ret));
  190. file_attrcopy(attr, &stat_buf);
  191. return 0;
  192. }
  193. /* 'attrquerybyhdl' operation for file streams */
  194. static int file_attrquerybyhdl (PAL_HANDLE handle,
  195. PAL_STREAM_ATTR * attr)
  196. {
  197. int fd = handle->hdr.fds[0];
  198. struct stat stat_buf;
  199. int ret = INLINE_SYSCALL(fstat, 2, fd, &stat_buf);
  200. if (IS_ERR(ret))
  201. return unix_to_pal_error(ERRNO(ret));
  202. file_attrcopy(attr, &stat_buf);
  203. return 0;
  204. }
  205. static int file_attrsetbyhdl (PAL_HANDLE handle,
  206. PAL_STREAM_ATTR * attr)
  207. {
  208. int fd = handle->hdr.fds[0], ret;
  209. ret = INLINE_SYSCALL(fchmod, 2, fd, attr->share_flags);
  210. if (IS_ERR(ret))
  211. return unix_to_pal_error(ERRNO(ret));
  212. return 0;
  213. }
  214. static int file_rename (PAL_HANDLE handle, const char * type,
  215. const char * uri)
  216. {
  217. char* tmp = strdup(uri);
  218. if (!tmp)
  219. return -PAL_ERROR_NOMEM;
  220. int ret = INLINE_SYSCALL(rename, 2, handle->file.realpath, uri);
  221. if (IS_ERR(ret)) {
  222. free(tmp);
  223. return unix_to_pal_error(ERRNO(ret));
  224. }
  225. /* initial realpath is part of handle object and will be freed with it */
  226. if (handle->file.realpath &&
  227. handle->file.realpath != (void *) handle + HANDLE_SIZE(file)) {
  228. free((void *) handle->file.realpath);
  229. }
  230. handle->file.realpath = tmp;
  231. return 0;
  232. }
  233. static int file_getname (PAL_HANDLE handle, char * buffer, int count)
  234. {
  235. if (!handle->file.realpath)
  236. return 0;
  237. int len = strlen(handle->file.realpath);
  238. char * tmp = strcpy_static(buffer, URI_PREFIX_FILE, count);
  239. if (!tmp || buffer + count < tmp + len + 1)
  240. return -PAL_ERROR_TOOLONG;
  241. memcpy(tmp, handle->file.realpath, len + 1);
  242. return tmp + len - buffer;
  243. }
  244. const char * file_getrealpath (PAL_HANDLE handle)
  245. {
  246. return handle->file.realpath;
  247. }
  248. struct handle_ops file_ops = {
  249. .getname = &file_getname,
  250. .getrealpath = &file_getrealpath,
  251. .open = &file_open,
  252. .read = &file_read,
  253. .write = &file_write,
  254. .close = &file_close,
  255. .delete = &file_delete,
  256. .map = &file_map,
  257. .setlength = &file_setlength,
  258. .flush = &file_flush,
  259. .attrquery = &file_attrquery,
  260. .attrquerybyhdl = &file_attrquerybyhdl,
  261. .attrsetbyhdl = &file_attrsetbyhdl,
  262. .rename = &file_rename,
  263. };
  264. /* 'open' operation for directory stream. Directory stream does not have a
  265. specific type prefix, its URI looks the same file streams, plus it
  266. ended with slashes. dir_open will be called by file_open. */
  267. static int dir_open (PAL_HANDLE * handle, const char * type, const char * uri,
  268. int access, int share, int create, int options)
  269. {
  270. int ret;
  271. int mode = HOST_PERM(share);
  272. if (create & PAL_CREATE_TRY) {
  273. ret = INLINE_SYSCALL(mkdir, 2, uri, mode);
  274. if (IS_ERR(ret) && ERRNO(ret) == EEXIST &&
  275. create & PAL_CREATE_ALWAYS)
  276. return -PAL_ERROR_STREAMEXIST;
  277. }
  278. ret = INLINE_SYSCALL(open, 3, uri, O_DIRECTORY|options|O_CLOEXEC, 0);
  279. if (IS_ERR(ret))
  280. return unix_to_pal_error(ERRNO(ret));
  281. int len = strlen(uri);
  282. PAL_HANDLE hdl = malloc(HANDLE_SIZE(dir) + len + 1);
  283. SET_HANDLE_TYPE(hdl, dir);
  284. hdl->hdr.flags |= RFD(0);
  285. hdl->dir.fd = ret;
  286. char * path = (void *) hdl + HANDLE_SIZE(dir);
  287. memcpy(path, uri, len + 1);
  288. hdl->dir.realpath = path;
  289. hdl->dir.buf = NULL;
  290. hdl->dir.ptr = NULL;
  291. hdl->dir.end = NULL;
  292. hdl->dir.endofstream = false;
  293. *handle = hdl;
  294. return 0;
  295. }
  296. struct dirent {
  297. __uint32_t d_fileno; /* file number of entry */
  298. __uint16_t d_reclen; /* length of this record */
  299. __uint8_t d_type; /* file type, see below */
  300. __uint8_t d_namlen; /* length of string in d_name */
  301. char d_name[255 + 1]; /* name must be no longer than this */
  302. };
  303. #define DT_UNKNOWN 0
  304. #define DT_FIFO 1
  305. #define DT_CHR 2
  306. #define DT_DIR 4
  307. #define DT_BLK 6
  308. #define DT_REG 8
  309. #define DT_LNK 10
  310. #define DT_SOCK 12
  311. #define DT_WHT 14
  312. #define DIRBUF_SIZE 1024
  313. /* 'read' operation for directory stream. Directory stream will not
  314. need a 'write' operation. */
  315. int dir_read (PAL_HANDLE handle, int offset, int count, void * buf)
  316. {
  317. void * dent_buf = handle->dir.buf ? : __alloca(DIRBUF_SIZE);
  318. void * ptr = handle->dir.ptr;
  319. void * end = handle->dir.end;
  320. int bytes = 0;
  321. if (ptr && ptr < end)
  322. goto output;
  323. do {
  324. if (handle->dir.endofstream)
  325. break;
  326. int size = INLINE_SYSCALL(getdents, 3, handle->dir.fd, dent_buf,
  327. DIRBUF_SIZE);
  328. if (IS_ERR(size))
  329. return -PAL_ERROR_DENIED;
  330. if (size == 0) {
  331. handle->dir.endofstream = PAL_TRUE;
  332. break;
  333. }
  334. ptr = dent_buf;
  335. end = dent_buf + size;
  336. output:
  337. while (ptr < end) {
  338. struct dirent * d = (struct dirent *) ptr;
  339. if (d->d_name[0] == '.' &&
  340. (!d->d_name[1] || d->d_name[1] == '.'))
  341. goto next;
  342. bool isdir = (d->d_type == DT_DIR);
  343. int len = d->d_namlen;
  344. if (len + (isdir ? 2 : 1) > count)
  345. break;
  346. memcpy(buf, d->d_name, len);
  347. if (isdir)
  348. ((char *) buf)[len++] = '/';
  349. ((char *) buf)[len++] = '\0';
  350. bytes += len;
  351. buf += len;
  352. count -= len;
  353. next:
  354. ptr += d->d_reclen;
  355. }
  356. } while (ptr == end);
  357. if (ptr < end) {
  358. if (!handle->dir.buf)
  359. handle->dir.buf = malloc(DIRBUF_SIZE);
  360. if (handle->dir.buf != ptr) {
  361. memmove(handle->dir.buf, ptr, end - ptr);
  362. end = handle->dir.buf + (end - ptr);
  363. ptr = handle->dir.buf;
  364. }
  365. if (!bytes)
  366. return -PAL_ERROR_OVERFLOW;
  367. }
  368. return bytes ? : -PAL_ERROR_ENDOFSTREAM;
  369. }
  370. /* 'close' operation of directory streams */
  371. static int dir_close (PAL_HANDLE handle)
  372. {
  373. int fd = handle->dir.fd;
  374. int ret = INLINE_SYSCALL(close, 1, fd);
  375. if (handle->dir.buf) {
  376. free(handle->dir.buf);
  377. handle->dir.buf = handle->dir.ptr = handle->dir.end = NULL;
  378. }
  379. /* initial realpath is part of handle object and will be freed with it */
  380. if (handle->dir.realpath &&
  381. handle->dir.realpath != (void *) handle + HANDLE_SIZE(dir)) {
  382. free((void *) handle->dir.realpath);
  383. }
  384. if (IS_ERR(ret))
  385. return -PAL_ERROR_BADHANDLE;
  386. return 0;
  387. }
  388. /* 'delete' operation of directoy streams */
  389. static int dir_delete (PAL_HANDLE handle, int access)
  390. {
  391. if (access)
  392. return -PAL_ERROR_INVAL;
  393. int ret = dir_close(handle);
  394. if (ret < 0)
  395. return ret;
  396. ret = INLINE_SYSCALL(rmdir, 1, handle->dir.realpath);
  397. return (IS_ERR(ret) && ERRNO(ret) != ENOENT) ?
  398. -PAL_ERROR_DENIED : 0;
  399. }
  400. static int dir_rename (PAL_HANDLE handle, const char * type,
  401. const char * uri)
  402. {
  403. char* tmp = strdup(uri);
  404. if (!tmp)
  405. return -PAL_ERROR_NOMEM;
  406. int ret = INLINE_SYSCALL(rename, 2, handle->dir.realpath, uri);
  407. if (IS_ERR(ret)) {
  408. free(tmp);
  409. return unix_to_pal_error(ERRNO(ret));
  410. }
  411. /* initial realpath is part of handle object and will be freed with it */
  412. if (handle->dir.realpath &&
  413. handle->dir.realpath != (void *) handle + HANDLE_SIZE(dir)) {
  414. free((void *) handle->dir.realpath);
  415. }
  416. handle->dir.realpath = tmp;
  417. return 0;
  418. }
  419. static int dir_getname (PAL_HANDLE handle, char * buffer, int count)
  420. {
  421. if (!handle->dir.realpath)
  422. return 0;
  423. int len = strlen(handle->dir.realpath);
  424. char * tmp = strcpy_static(buffer, URI_PREFIX_FILE, count);
  425. if (!tmp || buffer + count < tmp + len + 1)
  426. return -PAL_ERROR_TOOLONG;
  427. memcpy(tmp, handle->dir.realpath, len + 1);
  428. return tmp + len - buffer;
  429. }
  430. static const char * dir_getrealpath (PAL_HANDLE handle)
  431. {
  432. return handle->dir.realpath;
  433. }
  434. struct handle_ops dir_ops = {
  435. .getname = &dir_getname,
  436. .getrealpath = &dir_getrealpath,
  437. .open = &dir_open,
  438. .read = &dir_read,
  439. .close = &dir_close,
  440. .delete = &dir_delete,
  441. .attrquery = &file_attrquery,
  442. .attrquerybyhdl = &file_attrquerybyhdl,
  443. .attrsetbyhdl = &file_attrsetbyhdl,
  444. .rename = &dir_rename,
  445. };