Commit | Line | Data |
---|---|---|
7823a941 | 1 | /* File-I/O functions for GDB, the GNU debugger. |
791c0056 | 2 | |
3666a048 | 3 | Copyright (C) 2003-2021 Free Software Foundation, Inc. |
791c0056 GB |
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" | |
7823a941 | 21 | #include "fileio.h" |
791c0056 | 22 | #include <sys/stat.h> |
819843c7 | 23 | #include <fcntl.h> |
791c0056 | 24 | |
7823a941 | 25 | /* See fileio.h. */ |
b88bb450 GB |
26 | |
27 | int | |
7823a941 | 28 | host_to_fileio_error (int error) |
b88bb450 GB |
29 | { |
30 | switch (error) | |
31 | { | |
32 | case EPERM: | |
dda83cd7 | 33 | return FILEIO_EPERM; |
b88bb450 | 34 | case ENOENT: |
dda83cd7 | 35 | return FILEIO_ENOENT; |
b88bb450 | 36 | case EINTR: |
dda83cd7 | 37 | return FILEIO_EINTR; |
b88bb450 | 38 | case EIO: |
dda83cd7 | 39 | return FILEIO_EIO; |
b88bb450 | 40 | case EBADF: |
dda83cd7 | 41 | return FILEIO_EBADF; |
b88bb450 | 42 | case EACCES: |
dda83cd7 | 43 | return FILEIO_EACCES; |
b88bb450 | 44 | case EFAULT: |
dda83cd7 | 45 | return FILEIO_EFAULT; |
b88bb450 | 46 | case EBUSY: |
dda83cd7 | 47 | return FILEIO_EBUSY; |
b88bb450 | 48 | case EEXIST: |
dda83cd7 | 49 | return FILEIO_EEXIST; |
b88bb450 | 50 | case ENODEV: |
dda83cd7 | 51 | return FILEIO_ENODEV; |
b88bb450 | 52 | case ENOTDIR: |
dda83cd7 | 53 | return FILEIO_ENOTDIR; |
b88bb450 | 54 | case EISDIR: |
dda83cd7 | 55 | return FILEIO_EISDIR; |
b88bb450 | 56 | case EINVAL: |
dda83cd7 | 57 | return FILEIO_EINVAL; |
b88bb450 | 58 | case ENFILE: |
dda83cd7 | 59 | return FILEIO_ENFILE; |
b88bb450 | 60 | case EMFILE: |
dda83cd7 | 61 | return FILEIO_EMFILE; |
b88bb450 | 62 | case EFBIG: |
dda83cd7 | 63 | return FILEIO_EFBIG; |
b88bb450 | 64 | case ENOSPC: |
dda83cd7 | 65 | return FILEIO_ENOSPC; |
b88bb450 | 66 | case ESPIPE: |
dda83cd7 | 67 | return FILEIO_ESPIPE; |
b88bb450 | 68 | case EROFS: |
dda83cd7 | 69 | return FILEIO_EROFS; |
b88bb450 | 70 | case ENOSYS: |
dda83cd7 | 71 | return FILEIO_ENOSYS; |
b88bb450 | 72 | case ENAMETOOLONG: |
dda83cd7 | 73 | return FILEIO_ENAMETOOLONG; |
b88bb450 GB |
74 | } |
75 | return FILEIO_EUNKNOWN; | |
76 | } | |
77 | ||
819843c7 GB |
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 | ||
3ac2e371 GB |
112 | /* See fileio.h. */ |
113 | ||
114 | int | |
115 | fileio_to_host_mode (int fileio_mode, mode_t *mode_p) | |
116 | { | |
117 | mode_t mode = 0; | |
118 | ||
119 | if (fileio_mode & ~FILEIO_S_SUPPORTED) | |
120 | return -1; | |
121 | ||
122 | if (fileio_mode & FILEIO_S_IFREG) | |
123 | mode |= S_IFREG; | |
124 | if (fileio_mode & FILEIO_S_IFDIR) | |
125 | mode |= S_IFDIR; | |
126 | if (fileio_mode & FILEIO_S_IFCHR) | |
127 | mode |= S_IFCHR; | |
128 | if (fileio_mode & FILEIO_S_IRUSR) | |
129 | mode |= S_IRUSR; | |
130 | if (fileio_mode & FILEIO_S_IWUSR) | |
131 | mode |= S_IWUSR; | |
132 | if (fileio_mode & FILEIO_S_IXUSR) | |
133 | mode |= S_IXUSR; | |
134 | #ifdef S_IRGRP | |
135 | if (fileio_mode & FILEIO_S_IRGRP) | |
136 | mode |= S_IRGRP; | |
137 | #endif | |
138 | #ifdef S_IWGRP | |
139 | if (fileio_mode & FILEIO_S_IWGRP) | |
140 | mode |= S_IWGRP; | |
141 | #endif | |
142 | #ifdef S_IXGRP | |
143 | if (fileio_mode & FILEIO_S_IXGRP) | |
144 | mode |= S_IXGRP; | |
145 | #endif | |
146 | if (fileio_mode & FILEIO_S_IROTH) | |
147 | mode |= S_IROTH; | |
148 | #ifdef S_IWOTH | |
149 | if (fileio_mode & FILEIO_S_IWOTH) | |
150 | mode |= S_IWOTH; | |
151 | #endif | |
152 | #ifdef S_IXOTH | |
153 | if (fileio_mode & FILEIO_S_IXOTH) | |
154 | mode |= S_IXOTH; | |
155 | #endif | |
156 | ||
157 | *mode_p = mode; | |
158 | return 0; | |
159 | } | |
160 | ||
791c0056 GB |
161 | /* Convert a host-format mode_t into a bitmask of File-I/O flags. */ |
162 | ||
163 | static LONGEST | |
7823a941 | 164 | fileio_mode_pack (mode_t mode) |
791c0056 GB |
165 | { |
166 | mode_t tmode = 0; | |
167 | ||
168 | if (S_ISREG (mode)) | |
169 | tmode |= FILEIO_S_IFREG; | |
170 | if (S_ISDIR (mode)) | |
171 | tmode |= FILEIO_S_IFDIR; | |
172 | if (S_ISCHR (mode)) | |
173 | tmode |= FILEIO_S_IFCHR; | |
174 | if (mode & S_IRUSR) | |
175 | tmode |= FILEIO_S_IRUSR; | |
176 | if (mode & S_IWUSR) | |
177 | tmode |= FILEIO_S_IWUSR; | |
178 | if (mode & S_IXUSR) | |
179 | tmode |= FILEIO_S_IXUSR; | |
180 | #ifdef S_IRGRP | |
181 | if (mode & S_IRGRP) | |
182 | tmode |= FILEIO_S_IRGRP; | |
183 | #endif | |
ecef18c5 | 184 | #ifdef S_IWGRP |
791c0056 GB |
185 | if (mode & S_IWGRP) |
186 | tmode |= FILEIO_S_IWGRP; | |
187 | #endif | |
188 | #ifdef S_IXGRP | |
189 | if (mode & S_IXGRP) | |
190 | tmode |= FILEIO_S_IXGRP; | |
191 | #endif | |
192 | if (mode & S_IROTH) | |
193 | tmode |= FILEIO_S_IROTH; | |
194 | #ifdef S_IWOTH | |
195 | if (mode & S_IWOTH) | |
196 | tmode |= FILEIO_S_IWOTH; | |
197 | #endif | |
198 | #ifdef S_IXOTH | |
199 | if (mode & S_IXOTH) | |
200 | tmode |= FILEIO_S_IXOTH; | |
201 | #endif | |
202 | return tmode; | |
203 | } | |
204 | ||
205 | /* Pack a host-format mode_t into an fio_mode_t. */ | |
206 | ||
207 | static void | |
7823a941 | 208 | host_to_fileio_mode (mode_t num, fio_mode_t fnum) |
791c0056 | 209 | { |
7823a941 | 210 | host_to_bigendian (fileio_mode_pack (num), (char *) fnum, 4); |
791c0056 GB |
211 | } |
212 | ||
213 | /* Pack a host-format integer into an fio_ulong_t. */ | |
214 | ||
215 | static void | |
7823a941 | 216 | host_to_fileio_ulong (LONGEST num, fio_ulong_t fnum) |
791c0056 | 217 | { |
7823a941 | 218 | host_to_bigendian (num, (char *) fnum, 8); |
791c0056 GB |
219 | } |
220 | ||
7823a941 | 221 | /* See fileio.h. */ |
791c0056 GB |
222 | |
223 | void | |
7823a941 | 224 | host_to_fileio_stat (struct stat *st, struct fio_stat *fst) |
791c0056 GB |
225 | { |
226 | LONGEST blksize; | |
227 | ||
7823a941 GB |
228 | host_to_fileio_uint ((long) st->st_dev, fst->fst_dev); |
229 | host_to_fileio_uint ((long) st->st_ino, fst->fst_ino); | |
230 | host_to_fileio_mode (st->st_mode, fst->fst_mode); | |
231 | host_to_fileio_uint ((long) st->st_nlink, fst->fst_nlink); | |
232 | host_to_fileio_uint ((long) st->st_uid, fst->fst_uid); | |
233 | host_to_fileio_uint ((long) st->st_gid, fst->fst_gid); | |
234 | host_to_fileio_uint ((long) st->st_rdev, fst->fst_rdev); | |
235 | host_to_fileio_ulong ((LONGEST) st->st_size, fst->fst_size); | |
791c0056 GB |
236 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
237 | blksize = st->st_blksize; | |
238 | #else | |
239 | blksize = 512; | |
240 | #endif | |
7823a941 | 241 | host_to_fileio_ulong (blksize, fst->fst_blksize); |
791c0056 | 242 | #if HAVE_STRUCT_STAT_ST_BLOCKS |
7823a941 | 243 | host_to_fileio_ulong ((LONGEST) st->st_blocks, fst->fst_blocks); |
791c0056 GB |
244 | #else |
245 | /* FIXME: This is correct for DJGPP, but other systems that don't | |
246 | have st_blocks, if any, might prefer 512 instead of st_blksize. | |
247 | (eliz, 30-12-2003) */ | |
7823a941 GB |
248 | host_to_fileio_ulong (((LONGEST) st->st_size + blksize - 1) |
249 | / blksize, | |
250 | fst->fst_blocks); | |
791c0056 | 251 | #endif |
7823a941 GB |
252 | host_to_fileio_time (st->st_atime, fst->fst_atime); |
253 | host_to_fileio_time (st->st_mtime, fst->fst_mtime); | |
254 | host_to_fileio_time (st->st_ctime, fst->fst_ctime); | |
791c0056 | 255 | } |