shim_stat.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_stat.c
  17. *
  18. * Implementation of system call "stat", "lstat", "fstat" and "readlink".
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_table.h>
  22. #include <shim_handle.h>
  23. #include <shim_fs.h>
  24. #include <shim_profile.h>
  25. #include <pal.h>
  26. #include <pal_error.h>
  27. #include <errno.h>
  28. int shim_do_stat (const char * file, struct stat * stat)
  29. {
  30. if (!file)
  31. return -EFAULT;
  32. int ret;
  33. struct shim_dentry * dent = NULL;
  34. if ((ret = path_lookupat(NULL, file, LOOKUP_ACCESS|LOOKUP_FOLLOW, &dent, NULL)) < 0)
  35. goto out;
  36. struct shim_mount * fs = dent->fs;
  37. if (!fs->d_ops || !fs->d_ops->stat) {
  38. ret = -EACCES;
  39. goto out_dentry;
  40. }
  41. ret = fs->d_ops->stat(dent, stat);
  42. out_dentry:
  43. put_dentry(dent);
  44. out:
  45. return ret;
  46. }
  47. int shim_do_lstat (const char * file, struct stat * stat)
  48. {
  49. if (!file)
  50. return -EFAULT;
  51. int ret;
  52. struct shim_dentry * dent = NULL;
  53. if ((ret = path_lookupat(NULL, file, LOOKUP_ACCESS, &dent, NULL)) < 0)
  54. goto out;
  55. struct shim_mount * fs = dent->fs;
  56. if (!fs->d_ops || !fs->d_ops->stat) {
  57. ret = -EACCES;
  58. goto out_dentry;
  59. }
  60. ret = fs->d_ops->stat(dent, stat);
  61. out_dentry:
  62. put_dentry(dent);
  63. out:
  64. return ret;
  65. }
  66. int shim_do_fstat (int fd, struct stat * stat)
  67. {
  68. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  69. if (!hdl)
  70. return -EBADF;
  71. int ret = -EACCES;
  72. struct shim_mount * fs = hdl->fs;
  73. if (!fs || !fs->fs_ops)
  74. goto out;
  75. if (!fs->fs_ops->hstat)
  76. goto out;
  77. ret = fs->fs_ops->hstat(hdl, stat);
  78. out:
  79. put_handle(hdl);
  80. return ret;
  81. }
  82. int shim_do_readlink (const char * file, char * buf, int bufsize)
  83. {
  84. if (!file)
  85. return -EFAULT;
  86. if (bufsize <= 0)
  87. return -EINVAL;
  88. int ret;
  89. struct shim_dentry * dent = NULL;
  90. struct shim_qstr qstr = QSTR_INIT;
  91. if ((ret = path_lookupat(NULL, file, LOOKUP_ACCESS, &dent, NULL)) < 0)
  92. return ret;
  93. ret = -EINVAL;
  94. /* The correct behavior is to return -EINVAL if file is not a
  95. symbolic link */
  96. if (!(dent->state & DENTRY_ISLINK))
  97. goto out;
  98. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->follow_link)
  99. goto out;
  100. ret = dent->fs->d_ops->follow_link(dent, &qstr);
  101. if (ret < 0)
  102. goto out;
  103. ret = -ENAMETOOLONG;
  104. if (qstr.len >= bufsize)
  105. goto out;
  106. memcpy(buf, qstrgetstr(&qstr), qstr.len);
  107. ret = qstr.len;
  108. out:
  109. put_dentry(dent);
  110. return ret;
  111. }
  112. static int __do_statfs (struct shim_mount * fs, struct statfs * buf)
  113. {
  114. if (!buf)
  115. return -EFAULT;
  116. memset(buf, 0, sizeof(*buf));
  117. buf->f_bsize = 4096;
  118. buf->f_blocks = 20000000;
  119. buf->f_bfree = 10000000;
  120. buf->f_bavail = 10000000;
  121. debug("statfs: %ld %ld %ld\n", buf->f_blocks, buf->f_bfree,
  122. buf->f_bavail);
  123. return 0;
  124. }
  125. int shim_do_statfs (const char * path, struct statfs * buf)
  126. {
  127. if (!path)
  128. return -EFAULT;
  129. int ret;
  130. struct shim_dentry * dent = NULL;
  131. if ((ret = path_lookupat(NULL, path, LOOKUP_ACCESS|LOOKUP_FOLLOW, &dent, NULL)) < 0)
  132. return ret;
  133. struct shim_mount * fs = dent->fs;
  134. put_dentry(dent);
  135. return __do_statfs (fs, buf);
  136. }
  137. int shim_do_fstatfs (int fd, struct statfs * buf)
  138. {
  139. struct shim_handle * hdl = get_fd_handle(fd, NULL, NULL);
  140. if (!hdl)
  141. return -EBADF;
  142. struct shim_mount * fs = hdl->fs;
  143. put_handle(hdl);
  144. return __do_statfs (fs, buf);
  145. }