hostfs_user.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. {
  90. DIR *dir = stream;
  91. struct dirent *ent;
  92. seekdir(dir, *pos);
  93. ent = readdir(dir);
  94. if (ent == NULL)
  95. return NULL;
  96. *len_out = strlen(ent->d_name);
  97. *ino_out = ent->d_ino;
  98. *pos = telldir(dir);
  99. return ent->d_name;
  100. }
  101. int read_file(int fd, unsigned long long *offset, char *buf, int len)
  102. {
  103. int n;
  104. n = pread64(fd, buf, len, *offset);
  105. if (n < 0)
  106. return -errno;
  107. *offset += n;
  108. return n;
  109. }
  110. int write_file(int fd, unsigned long long *offset, const char *buf, int len)
  111. {
  112. int n;
  113. n = pwrite64(fd, buf, len, *offset);
  114. if (n < 0)
  115. return -errno;
  116. *offset += n;
  117. return n;
  118. }
  119. int lseek_file(int fd, long long offset, int whence)
  120. {
  121. int ret;
  122. ret = lseek64(fd, offset, whence);
  123. if (ret < 0)
  124. return -errno;
  125. return 0;
  126. }
  127. int fsync_file(int fd, int datasync)
  128. {
  129. int ret;
  130. if (datasync)
  131. ret = fdatasync(fd);
  132. else
  133. ret = fsync(fd);
  134. if (ret < 0)
  135. return -errno;
  136. return 0;
  137. }
  138. int replace_file(int oldfd, int fd)
  139. {
  140. return dup2(oldfd, fd);
  141. }
  142. void close_file(void *stream)
  143. {
  144. close(*((int *) stream));
  145. }
  146. void close_dir(void *stream)
  147. {
  148. closedir(stream);
  149. }
  150. int file_create(char *name, int ur, int uw, int ux, int gr,
  151. int gw, int gx, int or, int ow, int ox)
  152. {
  153. int mode, fd;
  154. mode = 0;
  155. mode |= ur ? S_IRUSR : 0;
  156. mode |= uw ? S_IWUSR : 0;
  157. mode |= ux ? S_IXUSR : 0;
  158. mode |= gr ? S_IRGRP : 0;
  159. mode |= gw ? S_IWGRP : 0;
  160. mode |= gx ? S_IXGRP : 0;
  161. mode |= or ? S_IROTH : 0;
  162. mode |= ow ? S_IWOTH : 0;
  163. mode |= ox ? S_IXOTH : 0;
  164. fd = open64(name, O_CREAT | O_RDWR, mode);
  165. if (fd < 0)
  166. return -errno;
  167. return fd;
  168. }
  169. int set_attr(const char *file, struct hostfs_iattr *attrs, int fd)
  170. {
  171. struct hostfs_stat st;
  172. struct timeval times[2];
  173. int err, ma;
  174. if (attrs->ia_valid & HOSTFS_ATTR_MODE) {
  175. if (fd >= 0) {
  176. if (fchmod(fd, attrs->ia_mode) != 0)
  177. return -errno;
  178. } else if (chmod(file, attrs->ia_mode) != 0) {
  179. return -errno;
  180. }
  181. }
  182. if (attrs->ia_valid & HOSTFS_ATTR_UID) {
  183. if (fd >= 0) {
  184. if (fchown(fd, attrs->ia_uid, -1))
  185. return -errno;
  186. } else if (chown(file, attrs->ia_uid, -1)) {
  187. return -errno;
  188. }
  189. }
  190. if (attrs->ia_valid & HOSTFS_ATTR_GID) {
  191. if (fd >= 0) {
  192. if (fchown(fd, -1, attrs->ia_gid))
  193. return -errno;
  194. } else if (chown(file, -1, attrs->ia_gid)) {
  195. return -errno;
  196. }
  197. }
  198. if (attrs->ia_valid & HOSTFS_ATTR_SIZE) {
  199. if (fd >= 0) {
  200. if (ftruncate(fd, attrs->ia_size))
  201. return -errno;
  202. } else if (truncate(file, attrs->ia_size)) {
  203. return -errno;
  204. }
  205. }
  206. /*
  207. * Update accessed and/or modified time, in two parts: first set
  208. * times according to the changes to perform, and then call futimes()
  209. * or utimes() to apply them.
  210. */
  211. ma = (HOSTFS_ATTR_ATIME_SET | HOSTFS_ATTR_MTIME_SET);
  212. if (attrs->ia_valid & ma) {
  213. err = stat_file(file, &st, fd);
  214. if (err != 0)
  215. return err;
  216. times[0].tv_sec = st.atime.tv_sec;
  217. times[0].tv_usec = st.atime.tv_nsec / 1000;
  218. times[1].tv_sec = st.mtime.tv_sec;
  219. times[1].tv_usec = st.mtime.tv_nsec / 1000;
  220. if (attrs->ia_valid & HOSTFS_ATTR_ATIME_SET) {
  221. times[0].tv_sec = attrs->ia_atime.tv_sec;
  222. times[0].tv_usec = attrs->ia_atime.tv_nsec / 1000;
  223. }
  224. if (attrs->ia_valid & HOSTFS_ATTR_MTIME_SET) {
  225. times[1].tv_sec = attrs->ia_mtime.tv_sec;
  226. times[1].tv_usec = attrs->ia_mtime.tv_nsec / 1000;
  227. }
  228. if (fd >= 0) {
  229. if (futimes(fd, times) != 0)
  230. return -errno;
  231. } else if (utimes(file, times) != 0) {
  232. return -errno;
  233. }
  234. }
  235. /* Note: ctime is not handled */
  236. if (attrs->ia_valid & (HOSTFS_ATTR_ATIME | HOSTFS_ATTR_MTIME)) {
  237. err = stat_file(file, &st, fd);
  238. attrs->ia_atime = st.atime;
  239. attrs->ia_mtime = st.mtime;
  240. if (err != 0)
  241. return err;
  242. }
  243. return 0;
  244. }
  245. int make_symlink(const char *from, const char *to)
  246. {
  247. int err;
  248. err = symlink(to, from);
  249. if (err)
  250. return -errno;
  251. return 0;
  252. }
  253. int unlink_file(const char *file)
  254. {
  255. int err;
  256. err = unlink(file);
  257. if (err)
  258. return -errno;
  259. return 0;
  260. }
  261. int do_mkdir(const char *file, int mode)
  262. {
  263. int err;
  264. err = mkdir(file, mode);
  265. if (err)
  266. return -errno;
  267. return 0;
  268. }
  269. int do_rmdir(const char *file)
  270. {
  271. int err;
  272. err = rmdir(file);
  273. if (err)
  274. return -errno;
  275. return 0;
  276. }
  277. int do_mknod(const char *file, int mode, unsigned int major, unsigned int minor)
  278. {
  279. int err;
  280. err = mknod(file, mode, os_makedev(major, minor));
  281. if (err)
  282. return -errno;
  283. return 0;
  284. }
  285. int link_file(const char *to, const char *from)
  286. {
  287. int err;
  288. err = link(to, from);
  289. if (err)
  290. return -errno;
  291. return 0;
  292. }
  293. int hostfs_do_readlink(char *file, char *buf, int size)
  294. {
  295. int n;
  296. n = readlink(file, buf, size);
  297. if (n < 0)
  298. return -errno;
  299. if (n < size)
  300. buf[n] = '\0';
  301. return n;
  302. }
  303. int rename_file(char *from, char *to)
  304. {
  305. int err;
  306. err = rename(from, to);
  307. if (err < 0)
  308. return -errno;
  309. return 0;
  310. }
  311. int do_statfs(char *root, long *bsize_out, long long *blocks_out,
  312. long long *bfree_out, long long *bavail_out,
  313. long long *files_out, long long *ffree_out,
  314. void *fsid_out, int fsid_size, long *namelen_out)
  315. {
  316. struct statfs64 buf;
  317. int err;
  318. err = statfs64(root, &buf);
  319. if (err < 0)
  320. return -errno;
  321. *bsize_out = buf.f_bsize;
  322. *blocks_out = buf.f_blocks;
  323. *bfree_out = buf.f_bfree;
  324. *bavail_out = buf.f_bavail;
  325. *files_out = buf.f_files;
  326. *ffree_out = buf.f_ffree;
  327. memcpy(fsid_out, &buf.f_fsid,
  328. sizeof(buf.f_fsid) > fsid_size ? fsid_size :
  329. sizeof(buf.f_fsid));
  330. *namelen_out = buf.f_namelen;
  331. return 0;
  332. }