fileio.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* File-I/O functions for GDB, the GNU debugger.
  2. Copyright (C) 2003-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "common-defs.h"
  15. #include "fileio.h"
  16. #include <sys/stat.h>
  17. #include <fcntl.h>
  18. /* See fileio.h. */
  19. int
  20. host_to_fileio_error (int error)
  21. {
  22. switch (error)
  23. {
  24. case EPERM:
  25. return FILEIO_EPERM;
  26. case ENOENT:
  27. return FILEIO_ENOENT;
  28. case EINTR:
  29. return FILEIO_EINTR;
  30. case EIO:
  31. return FILEIO_EIO;
  32. case EBADF:
  33. return FILEIO_EBADF;
  34. case EACCES:
  35. return FILEIO_EACCES;
  36. case EFAULT:
  37. return FILEIO_EFAULT;
  38. case EBUSY:
  39. return FILEIO_EBUSY;
  40. case EEXIST:
  41. return FILEIO_EEXIST;
  42. case ENODEV:
  43. return FILEIO_ENODEV;
  44. case ENOTDIR:
  45. return FILEIO_ENOTDIR;
  46. case EISDIR:
  47. return FILEIO_EISDIR;
  48. case EINVAL:
  49. return FILEIO_EINVAL;
  50. case ENFILE:
  51. return FILEIO_ENFILE;
  52. case EMFILE:
  53. return FILEIO_EMFILE;
  54. case EFBIG:
  55. return FILEIO_EFBIG;
  56. case ENOSPC:
  57. return FILEIO_ENOSPC;
  58. case ESPIPE:
  59. return FILEIO_ESPIPE;
  60. case EROFS:
  61. return FILEIO_EROFS;
  62. case ENOSYS:
  63. return FILEIO_ENOSYS;
  64. case ENAMETOOLONG:
  65. return FILEIO_ENAMETOOLONG;
  66. }
  67. return FILEIO_EUNKNOWN;
  68. }
  69. /* See fileio.h. */
  70. int
  71. fileio_to_host_openflags (int fileio_open_flags, int *open_flags_p)
  72. {
  73. int open_flags = 0;
  74. if (fileio_open_flags & ~FILEIO_O_SUPPORTED)
  75. return -1;
  76. if (fileio_open_flags & FILEIO_O_CREAT)
  77. open_flags |= O_CREAT;
  78. if (fileio_open_flags & FILEIO_O_EXCL)
  79. open_flags |= O_EXCL;
  80. if (fileio_open_flags & FILEIO_O_TRUNC)
  81. open_flags |= O_TRUNC;
  82. if (fileio_open_flags & FILEIO_O_APPEND)
  83. open_flags |= O_APPEND;
  84. if (fileio_open_flags & FILEIO_O_RDONLY)
  85. open_flags |= O_RDONLY;
  86. if (fileio_open_flags & FILEIO_O_WRONLY)
  87. open_flags |= O_WRONLY;
  88. if (fileio_open_flags & FILEIO_O_RDWR)
  89. open_flags |= O_RDWR;
  90. /* On systems supporting binary and text mode, always open files
  91. in binary mode. */
  92. #ifdef O_BINARY
  93. open_flags |= O_BINARY;
  94. #endif
  95. *open_flags_p = open_flags;
  96. return 0;
  97. }
  98. /* See fileio.h. */
  99. int
  100. fileio_to_host_mode (int fileio_mode, mode_t *mode_p)
  101. {
  102. mode_t mode = 0;
  103. if (fileio_mode & ~FILEIO_S_SUPPORTED)
  104. return -1;
  105. if (fileio_mode & FILEIO_S_IFREG)
  106. mode |= S_IFREG;
  107. if (fileio_mode & FILEIO_S_IFDIR)
  108. mode |= S_IFDIR;
  109. if (fileio_mode & FILEIO_S_IFCHR)
  110. mode |= S_IFCHR;
  111. if (fileio_mode & FILEIO_S_IRUSR)
  112. mode |= S_IRUSR;
  113. if (fileio_mode & FILEIO_S_IWUSR)
  114. mode |= S_IWUSR;
  115. if (fileio_mode & FILEIO_S_IXUSR)
  116. mode |= S_IXUSR;
  117. #ifdef S_IRGRP
  118. if (fileio_mode & FILEIO_S_IRGRP)
  119. mode |= S_IRGRP;
  120. #endif
  121. #ifdef S_IWGRP
  122. if (fileio_mode & FILEIO_S_IWGRP)
  123. mode |= S_IWGRP;
  124. #endif
  125. #ifdef S_IXGRP
  126. if (fileio_mode & FILEIO_S_IXGRP)
  127. mode |= S_IXGRP;
  128. #endif
  129. if (fileio_mode & FILEIO_S_IROTH)
  130. mode |= S_IROTH;
  131. #ifdef S_IWOTH
  132. if (fileio_mode & FILEIO_S_IWOTH)
  133. mode |= S_IWOTH;
  134. #endif
  135. #ifdef S_IXOTH
  136. if (fileio_mode & FILEIO_S_IXOTH)
  137. mode |= S_IXOTH;
  138. #endif
  139. *mode_p = mode;
  140. return 0;
  141. }
  142. /* Convert a host-format mode_t into a bitmask of File-I/O flags. */
  143. static LONGEST
  144. fileio_mode_pack (mode_t mode)
  145. {
  146. mode_t tmode = 0;
  147. if (S_ISREG (mode))
  148. tmode |= FILEIO_S_IFREG;
  149. if (S_ISDIR (mode))
  150. tmode |= FILEIO_S_IFDIR;
  151. if (S_ISCHR (mode))
  152. tmode |= FILEIO_S_IFCHR;
  153. if (mode & S_IRUSR)
  154. tmode |= FILEIO_S_IRUSR;
  155. if (mode & S_IWUSR)
  156. tmode |= FILEIO_S_IWUSR;
  157. if (mode & S_IXUSR)
  158. tmode |= FILEIO_S_IXUSR;
  159. #ifdef S_IRGRP
  160. if (mode & S_IRGRP)
  161. tmode |= FILEIO_S_IRGRP;
  162. #endif
  163. #ifdef S_IWGRP
  164. if (mode & S_IWGRP)
  165. tmode |= FILEIO_S_IWGRP;
  166. #endif
  167. #ifdef S_IXGRP
  168. if (mode & S_IXGRP)
  169. tmode |= FILEIO_S_IXGRP;
  170. #endif
  171. if (mode & S_IROTH)
  172. tmode |= FILEIO_S_IROTH;
  173. #ifdef S_IWOTH
  174. if (mode & S_IWOTH)
  175. tmode |= FILEIO_S_IWOTH;
  176. #endif
  177. #ifdef S_IXOTH
  178. if (mode & S_IXOTH)
  179. tmode |= FILEIO_S_IXOTH;
  180. #endif
  181. return tmode;
  182. }
  183. /* Pack a host-format mode_t into an fio_mode_t. */
  184. static void
  185. host_to_fileio_mode (mode_t num, fio_mode_t fnum)
  186. {
  187. host_to_bigendian (fileio_mode_pack (num), (char *) fnum, 4);
  188. }
  189. /* Pack a host-format integer into an fio_ulong_t. */
  190. static void
  191. host_to_fileio_ulong (LONGEST num, fio_ulong_t fnum)
  192. {
  193. host_to_bigendian (num, (char *) fnum, 8);
  194. }
  195. /* See fileio.h. */
  196. void
  197. host_to_fileio_stat (struct stat *st, struct fio_stat *fst)
  198. {
  199. LONGEST blksize;
  200. host_to_fileio_uint ((long) st->st_dev, fst->fst_dev);
  201. host_to_fileio_uint ((long) st->st_ino, fst->fst_ino);
  202. host_to_fileio_mode (st->st_mode, fst->fst_mode);
  203. host_to_fileio_uint ((long) st->st_nlink, fst->fst_nlink);
  204. host_to_fileio_uint ((long) st->st_uid, fst->fst_uid);
  205. host_to_fileio_uint ((long) st->st_gid, fst->fst_gid);
  206. host_to_fileio_uint ((long) st->st_rdev, fst->fst_rdev);
  207. host_to_fileio_ulong ((LONGEST) st->st_size, fst->fst_size);
  208. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  209. blksize = st->st_blksize;
  210. #else
  211. blksize = 512;
  212. #endif
  213. host_to_fileio_ulong (blksize, fst->fst_blksize);
  214. #if HAVE_STRUCT_STAT_ST_BLOCKS
  215. host_to_fileio_ulong ((LONGEST) st->st_blocks, fst->fst_blocks);
  216. #else
  217. /* FIXME: This is correct for DJGPP, but other systems that don't
  218. have st_blocks, if any, might prefer 512 instead of st_blksize.
  219. (eliz, 30-12-2003) */
  220. host_to_fileio_ulong (((LONGEST) st->st_size + blksize - 1)
  221. / blksize,
  222. fst->fst_blocks);
  223. #endif
  224. host_to_fileio_time (st->st_atime, fst->fst_atime);
  225. host_to_fileio_time (st->st_mtime, fst->fst_mtime);
  226. host_to_fileio_time (st->st_ctime, fst->fst_ctime);
  227. }