hostfs_user.c 8.4 KB

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