hostfs_user.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <stddef.h>
  7. #include <unistd.h>
  8. #include <dirent.h>
  9. #include <errno.h>
  10. #include <fcntl.h>
  11. #include <string.h>
  12. #include <sys/stat.h>
  13. #include <sys/time.h>
  14. #include <sys/types.h>
  15. #include <sys/vfs.h>
  16. #include "hostfs.h"
  17. #include "os.h"
  18. #include "user.h"
  19. #include <utime.h>
  20. static void stat64_to_hostfs(const struct stat64 *buf, struct hostfs_stat *p)
  21. {
  22. p->ino = buf->st_ino;
  23. p->mode = buf->st_mode;
  24. p->nlink = buf->st_nlink;
  25. p->uid = buf->st_uid;
  26. p->gid = buf->st_gid;
  27. p->size = buf->st_size;
  28. p->atime.tv_sec = buf->st_atime;
  29. p->atime.tv_nsec = 0;
  30. p->ctime.tv_sec = buf->st_ctime;
  31. p->ctime.tv_nsec = 0;
  32. p->mtime.tv_sec = buf->st_mtime;
  33. p->mtime.tv_nsec = 0;
  34. p->blksize = buf->st_blksize;
  35. p->blocks = buf->st_blocks;
  36. p->maj = os_major(buf->st_rdev);
  37. p->min = os_minor(buf->st_rdev);
  38. }
  39. int stat_file(const char *path, struct hostfs_stat *p, int fd)
  40. {
  41. struct stat64 buf;
  42. if (fd >= 0) {
  43. if (fstat64(fd, &buf) < 0)
  44. return -errno;
  45. } else if (lstat64(path, &buf) < 0) {
  46. return -errno;
  47. }
  48. stat64_to_hostfs(&buf, p);
  49. return 0;
  50. }
  51. int access_file(char *path, int r, int w, int x)
  52. {
  53. int mode = 0;
  54. if (r)
  55. mode = R_OK;
  56. if (w)
  57. mode |= W_OK;
  58. if (x)
  59. mode |= X_OK;
  60. if (access(path, mode) != 0)
  61. return -errno;
  62. else return 0;
  63. }
  64. int open_file(char *path, int r, int w, int append)
  65. {
  66. int mode = 0, fd;
  67. if (r && !w)
  68. mode = O_RDONLY;
  69. else if (!r && w)
  70. mode = O_WRONLY;
  71. else if (r && w)
  72. mode = O_RDWR;
  73. else panic("Impossible mode in open_file");
  74. if (append)
  75. mode |= O_APPEND;
  76. fd = open64(path, mode);
  77. if (fd < 0)
  78. return -errno;
  79. else return fd;
  80. }
  81. void *open_dir(char *path, int *err_out)
  82. {
  83. DIR *dir;
  84. dir = opendir(path);
  85. *err_out = errno;
  86. return dir;
  87. }
  88. char *read_dir(void *stream, unsigned long long *pos,
  89. unsigned long long *ino_out, int *len_out)
  90. {
  91. DIR *dir = stream;
  92. struct dirent *ent;
  93. seekdir(dir, *pos);
  94. ent = readdir(dir);
  95. if (ent == NULL)
  96. return NULL;
  97. *len_out = strlen(ent->d_name);
  98. *ino_out = ent->d_ino;
  99. *pos = telldir(dir);
  100. return ent->d_name;
  101. }
  102. int read_file(int fd, unsigned long long *offset, char *buf, int len)
  103. {
  104. int n;
  105. n = pread64(fd, buf, len, *offset);
  106. if (n < 0)
  107. return -errno;
  108. *offset += n;
  109. return n;
  110. }
  111. int write_file(int fd, unsigned long long *offset, const char *buf, int len)
  112. {
  113. int n;
  114. n = pwrite64(fd, buf, len, *offset);
  115. if (n < 0)
  116. return -errno;
  117. *offset += n;
  118. return n;
  119. }
  120. int lseek_file(int fd, long long offset, int whence)
  121. {
  122. int ret;
  123. ret = lseek64(fd, offset, whence);
  124. if (ret < 0)
  125. return -errno;
  126. return 0;
  127. }
  128. int fsync_file(int fd, int datasync)
  129. {
  130. int ret;
  131. if (datasync)
  132. ret = fdatasync(fd);
  133. else
  134. ret = fsync(fd);
  135. if (ret < 0)
  136. return -errno;
  137. return 0;
  138. }
  139. int replace_file(int oldfd, int fd)
  140. {
  141. return dup2(oldfd, fd);
  142. }
  143. void close_file(void *stream)
  144. {
  145. close(*((int *) stream));
  146. }
  147. void close_dir(void *stream)
  148. {
  149. closedir(stream);
  150. }
  151. int file_create(char *name, int ur, int uw, int ux, int gr,
  152. int gw, int gx, int or, int ow, int ox)
  153. {
  154. int mode, fd;
  155. mode = 0;
  156. mode |= ur ? S_IRUSR : 0;
  157. mode |= uw ? S_IWUSR : 0;
  158. mode |= ux ? S_IXUSR : 0;
  159. mode |= gr ? S_IRGRP : 0;
  160. mode |= gw ? S_IWGRP : 0;
  161. mode |= gx ? S_IXGRP : 0;
  162. mode |= or ? S_IROTH : 0;
  163. mode |= ow ? S_IWOTH : 0;
  164. mode |= ox ? S_IXOTH : 0;
  165. fd = open64(name, O_CREAT | O_RDWR, mode);
  166. if (fd < 0)
  167. return -errno;
  168. return fd;
  169. }
  170. int set_attr(const char *file, struct hostfs_iattr *attrs, int fd)
  171. {
  172. struct hostfs_stat st;
  173. struct timeval times[2];
  174. int err, ma;
  175. if (attrs->ia_valid & HOSTFS_ATTR_MODE) {
  176. if (fd >= 0) {
  177. if (fchmod(fd, attrs->ia_mode) != 0)
  178. return -errno;
  179. } else if (chmod(file, attrs->ia_mode) != 0) {
  180. return -errno;
  181. }
  182. }
  183. if (attrs->ia_valid & HOSTFS_ATTR_UID) {
  184. if (fd >= 0) {
  185. if (fchown(fd, attrs->ia_uid, -1))
  186. return -errno;
  187. } else if (chown(file, attrs->ia_uid, -1)) {
  188. return -errno;
  189. }
  190. }
  191. if (attrs->ia_valid & HOSTFS_ATTR_GID) {
  192. if (fd >= 0) {
  193. if (fchown(fd, -1, attrs->ia_gid))
  194. return -errno;
  195. } else if (chown(file, -1, attrs->ia_gid)) {
  196. return -errno;
  197. }
  198. }
  199. if (attrs->ia_valid & HOSTFS_ATTR_SIZE) {
  200. if (fd >= 0) {
  201. if (ftruncate(fd, attrs->ia_size))
  202. return -errno;
  203. } else if (truncate(file, attrs->ia_size)) {
  204. return -errno;
  205. }
  206. }
  207. /*
  208. * Update accessed and/or modified time, in two parts: first set
  209. * times according to the changes to perform, and then call futimes()
  210. * or utimes() to apply them.
  211. */
  212. ma = (HOSTFS_ATTR_ATIME_SET | HOSTFS_ATTR_MTIME_SET);
  213. if (attrs->ia_valid & ma) {
  214. err = stat_file(file, &st, fd);
  215. if (err != 0)
  216. return err;
  217. times[0].tv_sec = st.atime.tv_sec;
  218. times[0].tv_usec = st.atime.tv_nsec / 1000;
  219. times[1].tv_sec = st.mtime.tv_sec;
  220. times[1].tv_usec = st.mtime.tv_nsec / 1000;
  221. if (attrs->ia_valid & HOSTFS_ATTR_ATIME_SET) {
  222. times[0].tv_sec = attrs->ia_atime.tv_sec;
  223. times[0].tv_usec = attrs->ia_atime.tv_nsec / 1000;
  224. }
  225. if (attrs->ia_valid & HOSTFS_ATTR_MTIME_SET) {
  226. times[1].tv_sec = attrs->ia_mtime.tv_sec;
  227. times[1].tv_usec = attrs->ia_mtime.tv_nsec / 1000;
  228. }
  229. if (fd >= 0) {
  230. if (futimes(fd, times) != 0)
  231. return -errno;
  232. } else if (utimes(file, times) != 0) {
  233. return -errno;
  234. }
  235. }
  236. /* Note: ctime is not handled */
  237. if (attrs->ia_valid & (HOSTFS_ATTR_ATIME | HOSTFS_ATTR_MTIME)) {
  238. err = stat_file(file, &st, fd);
  239. attrs->ia_atime = st.atime;
  240. attrs->ia_mtime = st.mtime;
  241. if (err != 0)
  242. return err;
  243. }
  244. return 0;
  245. }
  246. int make_symlink(const char *from, const char *to)
  247. {
  248. int err;
  249. err = symlink(to, from);
  250. if (err)
  251. return -errno;
  252. return 0;
  253. }
  254. int unlink_file(const char *file)
  255. {
  256. int err;
  257. err = unlink(file);
  258. if (err)
  259. return -errno;
  260. return 0;
  261. }
  262. int do_mkdir(const char *file, int mode)
  263. {
  264. int err;
  265. err = mkdir(file, mode);
  266. if (err)
  267. return -errno;
  268. return 0;
  269. }
  270. int do_rmdir(const char *file)
  271. {
  272. int err;
  273. err = rmdir(file);
  274. if (err)
  275. return -errno;
  276. return 0;
  277. }
  278. int do_mknod(const char *file, int mode, unsigned int major, unsigned int minor)
  279. {
  280. int err;
  281. err = mknod(file, mode, os_makedev(major, minor));
  282. if (err)
  283. return -errno;
  284. return 0;
  285. }
  286. int link_file(const char *to, const char *from)
  287. {
  288. int err;
  289. err = link(to, from);
  290. if (err)
  291. return -errno;
  292. return 0;
  293. }
  294. int hostfs_do_readlink(char *file, char *buf, int size)
  295. {
  296. int n;
  297. n = readlink(file, buf, size);
  298. if (n < 0)
  299. return -errno;
  300. if (n < size)
  301. buf[n] = '\0';
  302. return n;
  303. }
  304. int rename_file(char *from, char *to)
  305. {
  306. int err;
  307. err = rename(from, to);
  308. if (err < 0)
  309. return -errno;
  310. return 0;
  311. }
  312. int do_statfs(char *root, long *bsize_out, long long *blocks_out,
  313. long long *bfree_out, long long *bavail_out,
  314. long long *files_out, long long *ffree_out,
  315. void *fsid_out, int fsid_size, long *namelen_out)
  316. {
  317. struct statfs64 buf;
  318. int err;
  319. err = statfs64(root, &buf);
  320. if (err < 0)
  321. return -errno;
  322. *bsize_out = buf.f_bsize;
  323. *blocks_out = buf.f_blocks;
  324. *bfree_out = buf.f_bfree;
  325. *bavail_out = buf.f_bavail;
  326. *files_out = buf.f_files;
  327. *ffree_out = buf.f_ffree;
  328. memcpy(fsid_out, &buf.f_fsid,
  329. sizeof(buf.f_fsid) > fsid_size ? fsid_size :
  330. sizeof(buf.f_fsid));
  331. *namelen_out = buf.f_namelen;
  332. return 0;
  333. }