hostfs_user.c 8.3 KB

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