db_files.c 15 KB

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