db_files.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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->__in.flags |= RFD(0)|WFD(0)|WRITEABLE(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. if (handle->file.realpath &&
  106. handle->file.realpath != (void *) handle + HANDLE_SIZE(file))
  107. free((void *) handle->file.realpath);
  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->writeable = 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->__in.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->__in.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. int ret = INLINE_SYSCALL(rename, 2, handle->file.realpath, uri);
  218. if (IS_ERR(ret))
  219. return unix_to_pal_error(ERRNO(ret));
  220. handle->file.realpath = remalloc(uri, strlen(uri));
  221. return 0;
  222. }
  223. static int file_getname (PAL_HANDLE handle, char * buffer, int count)
  224. {
  225. if (!handle->file.realpath)
  226. return 0;
  227. int len = strlen(handle->file.realpath);
  228. char * tmp = strcpy_static(buffer, "file:", count);
  229. if (!tmp || buffer + count < tmp + len + 1)
  230. return -PAL_ERROR_TOOLONG;
  231. memcpy(tmp, handle->file.realpath, len + 1);
  232. return tmp + len - buffer;
  233. }
  234. const char * file_getrealpath (PAL_HANDLE handle)
  235. {
  236. return handle->file.realpath;
  237. }
  238. struct handle_ops file_ops = {
  239. .getname = &file_getname,
  240. .getrealpath = &file_getrealpath,
  241. .open = &file_open,
  242. .read = &file_read,
  243. .write = &file_write,
  244. .close = &file_close,
  245. .delete = &file_delete,
  246. .map = &file_map,
  247. .setlength = &file_setlength,
  248. .flush = &file_flush,
  249. .attrquery = &file_attrquery,
  250. .attrquerybyhdl = &file_attrquerybyhdl,
  251. .attrsetbyhdl = &file_attrsetbyhdl,
  252. .rename = &file_rename,
  253. };
  254. /* 'open' operation for directory stream. Directory stream does not have a
  255. specific type prefix, its URI looks the same file streams, plus it
  256. ended with slashes. dir_open will be called by file_open. */
  257. static int dir_open (PAL_HANDLE * handle, const char * type, const char * uri,
  258. int access, int share, int create, int options)
  259. {
  260. int ret;
  261. int mode = HOST_PERM(share);
  262. if (create & PAL_CREAT_TRY) {
  263. ret = INLINE_SYSCALL(mkdir, 2, uri, mode);
  264. if (IS_ERR(ret) && ERRNO(ret) == EEXIST &&
  265. create & PAL_CREAT_ALWAYS)
  266. return -PAL_ERROR_STREAMEXIST;
  267. }
  268. ret = INLINE_SYSCALL(open, 3, uri, O_DIRECTORY|options|O_CLOEXEC, 0);
  269. if (IS_ERR(ret))
  270. return unix_to_pal_error(ERRNO(ret));
  271. int len = strlen(uri);
  272. PAL_HANDLE hdl = malloc(HANDLE_SIZE(dir) + len + 1);
  273. SET_HANDLE_TYPE(hdl, dir);
  274. hdl->__in.flags |= RFD(0);
  275. hdl->dir.fd = ret;
  276. char * path = (void *) hdl + HANDLE_SIZE(dir);
  277. memcpy(path, uri, len + 1);
  278. hdl->dir.realpath = path;
  279. hdl->dir.buf = NULL;
  280. hdl->dir.ptr = NULL;
  281. hdl->dir.end = NULL;
  282. hdl->dir.endofstream = false;
  283. *handle = hdl;
  284. return 0;
  285. }
  286. struct dirent {
  287. __uint32_t d_fileno; /* file number of entry */
  288. __uint16_t d_reclen; /* length of this record */
  289. __uint8_t d_type; /* file type, see below */
  290. __uint8_t d_namlen; /* length of string in d_name */
  291. char d_name[255 + 1]; /* name must be no longer than this */
  292. };
  293. #define DT_UNKNOWN 0
  294. #define DT_FIFO 1
  295. #define DT_CHR 2
  296. #define DT_DIR 4
  297. #define DT_BLK 6
  298. #define DT_REG 8
  299. #define DT_LNK 10
  300. #define DT_SOCK 12
  301. #define DT_WHT 14
  302. #define DIRBUF_SIZE 1024
  303. /* 'read' operation for directory stream. Directory stream will not
  304. need a 'write' operat4on. */
  305. int dir_read (PAL_HANDLE handle, int offset, int count, void * buf)
  306. {
  307. void * dent_buf = handle->dir.buf ? : __alloca(DIRBUF_SIZE);
  308. void * ptr = handle->dir.ptr;
  309. void * end = handle->dir.end;
  310. int bytes = 0;
  311. if (ptr && ptr < end)
  312. goto output;
  313. do {
  314. if (handle->dir.endofstream)
  315. break;
  316. int size = INLINE_SYSCALL(getdents, 3, handle->dir.fd, dent_buf,
  317. DIRBUF_SIZE);
  318. if (IS_ERR(size))
  319. return -PAL_ERROR_DENIED;
  320. if (size == 0) {
  321. handle->dir.endofstream = PAL_TRUE;
  322. break;
  323. }
  324. ptr = dent_buf;
  325. end = dent_buf + size;
  326. output:
  327. while (ptr < end) {
  328. struct dirent * d = (struct dirent *) ptr;
  329. if (d->d_name[0] == '.' &&
  330. (!d->d_name[1] || d->d_name[1] == '.'))
  331. goto next;
  332. bool isdir = (d->d_type == DT_DIR);
  333. int len = d->d_namlen;
  334. if (len + (isdir ? 2 : 1) > count)
  335. break;
  336. memcpy(buf, d->d_name, len);
  337. if (isdir)
  338. ((char *) buf)[len++] = '/';
  339. ((char *) buf)[len++] = '\0';
  340. bytes += len;
  341. buf += len;
  342. count -= len;
  343. next:
  344. ptr += d->d_reclen;
  345. }
  346. } while (ptr == end);
  347. if (ptr < end) {
  348. if (!handle->dir.buf)
  349. handle->dir.buf = malloc(DIRBUF_SIZE);
  350. if (handle->dir.buf != ptr) {
  351. memmove(handle->dir.buf, ptr, end - ptr);
  352. end = handle->dir.buf + (end - ptr);
  353. ptr = handle->dir.buf;
  354. }
  355. if (!bytes)
  356. return -PAL_ERROR_OVERFLOW;
  357. }
  358. return bytes ? : -PAL_ERROR_ENDOFSTREAM;
  359. }
  360. /* 'close' operation of directory streams */
  361. static int dir_close (PAL_HANDLE handle)
  362. {
  363. int fd = handle->dir.fd;
  364. int ret = INLINE_SYSCALL(close, 1, fd);
  365. if (handle->dir.buf) {
  366. free(handle->dir.buf);
  367. handle->dir.buf = handle->dir.ptr = handle->dir.end = NULL;
  368. }
  369. if (handle->dir.realpath &&
  370. handle->dir.realpath != (void *) handle + HANDLE_SIZE(dir))
  371. free((void *) handle->dir.realpath);
  372. if (IS_ERR(ret))
  373. return -PAL_ERROR_BADHANDLE;
  374. return 0;
  375. }
  376. /* 'delete' operation of directoy streams */
  377. static int dir_delete (PAL_HANDLE handle, int access)
  378. {
  379. if (access)
  380. return -PAL_ERROR_INVAL;
  381. int ret = dir_close(handle);
  382. if (ret < 0)
  383. return ret;
  384. ret = INLINE_SYSCALL(rmdir, 1, handle->dir.realpath);
  385. return (IS_ERR(ret) && ERRNO(ret) != ENOENT) ?
  386. -PAL_ERROR_DENIED : 0;
  387. }
  388. static int dir_rename (PAL_HANDLE handle, const char * type,
  389. const char * uri)
  390. {
  391. int ret = INLINE_SYSCALL(rename, 2, handle->dir.realpath, uri);
  392. if (IS_ERR(ret))
  393. return unix_to_pal_error(ERRNO(ret));
  394. handle->dir.realpath = remalloc(uri, strlen(uri));
  395. return 0;
  396. }
  397. static int dir_getname (PAL_HANDLE handle, char * buffer, int count)
  398. {
  399. if (!handle->dir.realpath)
  400. return 0;
  401. int len = strlen(handle->dir.realpath);
  402. char * tmp = strcpy_static(buffer, "file:", count);
  403. if (!tmp || buffer + count < tmp + len + 1)
  404. return -PAL_ERROR_TOOLONG;
  405. memcpy(tmp, handle->dir.realpath, len + 1);
  406. return tmp + len - buffer;
  407. }
  408. static const char * dir_getrealpath (PAL_HANDLE handle)
  409. {
  410. return handle->dir.realpath;
  411. }
  412. struct handle_ops dir_ops = {
  413. .getname = &dir_getname,
  414. .getrealpath = &dir_getrealpath,
  415. .open = &dir_open,
  416. .read = &dir_read,
  417. .close = &dir_close,
  418. .delete = &dir_delete,
  419. .attrquery = &file_attrquery,
  420. .attrquerybyhdl = &file_attrquerybyhdl,
  421. .attrsetbyhdl = &file_attrsetbyhdl,
  422. .rename = &dir_rename,
  423. };