hostfs_user.c 7.1 KB

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