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