]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdbsupport/fileio.cc
gdbsupport: rename include guard in gdb-checked-static-cast.h
[thirdparty/binutils-gdb.git] / gdbsupport / fileio.cc
CommitLineData
7823a941 1/* File-I/O functions for GDB, the GNU debugger.
791c0056 2
1d506c26 3 Copyright (C) 2003-2024 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 26
b872057a 27fileio_error
7823a941 28host_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
517a63c2
SM
80int
81fileio_error_to_host (fileio_error errnum)
82{
83 switch (errnum)
84 {
85 case FILEIO_EPERM:
86 return EPERM;
87 case FILEIO_ENOENT:
88 return ENOENT;
89 case FILEIO_EINTR:
90 return EINTR;
91 case FILEIO_EIO:
92 return EIO;
93 case FILEIO_EBADF:
94 return EBADF;
95 case FILEIO_EACCES:
96 return EACCES;
97 case FILEIO_EFAULT:
98 return EFAULT;
99 case FILEIO_EBUSY:
100 return EBUSY;
101 case FILEIO_EEXIST:
102 return EEXIST;
103 case FILEIO_ENODEV:
104 return ENODEV;
105 case FILEIO_ENOTDIR:
106 return ENOTDIR;
107 case FILEIO_EISDIR:
108 return EISDIR;
109 case FILEIO_EINVAL:
110 return EINVAL;
111 case FILEIO_ENFILE:
112 return ENFILE;
113 case FILEIO_EMFILE:
114 return EMFILE;
115 case FILEIO_EFBIG:
116 return EFBIG;
117 case FILEIO_ENOSPC:
118 return ENOSPC;
119 case FILEIO_ESPIPE:
120 return ESPIPE;
121 case FILEIO_EROFS:
122 return EROFS;
123 case FILEIO_ENOSYS:
124 return ENOSYS;
125 case FILEIO_ENAMETOOLONG:
126 return ENAMETOOLONG;
127 }
128 return -1;
129}
130
131/* See fileio.h. */
132
819843c7
GB
133int
134fileio_to_host_openflags (int fileio_open_flags, int *open_flags_p)
135{
136 int open_flags = 0;
137
138 if (fileio_open_flags & ~FILEIO_O_SUPPORTED)
139 return -1;
140
141 if (fileio_open_flags & FILEIO_O_CREAT)
142 open_flags |= O_CREAT;
143 if (fileio_open_flags & FILEIO_O_EXCL)
144 open_flags |= O_EXCL;
145 if (fileio_open_flags & FILEIO_O_TRUNC)
146 open_flags |= O_TRUNC;
147 if (fileio_open_flags & FILEIO_O_APPEND)
148 open_flags |= O_APPEND;
149 if (fileio_open_flags & FILEIO_O_RDONLY)
150 open_flags |= O_RDONLY;
151 if (fileio_open_flags & FILEIO_O_WRONLY)
152 open_flags |= O_WRONLY;
153 if (fileio_open_flags & FILEIO_O_RDWR)
154 open_flags |= O_RDWR;
155 /* On systems supporting binary and text mode, always open files
156 in binary mode. */
157#ifdef O_BINARY
158 open_flags |= O_BINARY;
159#endif
160
161 *open_flags_p = open_flags;
162 return 0;
163}
164
3ac2e371
GB
165/* See fileio.h. */
166
167int
168fileio_to_host_mode (int fileio_mode, mode_t *mode_p)
169{
170 mode_t mode = 0;
171
172 if (fileio_mode & ~FILEIO_S_SUPPORTED)
173 return -1;
174
175 if (fileio_mode & FILEIO_S_IFREG)
176 mode |= S_IFREG;
177 if (fileio_mode & FILEIO_S_IFDIR)
178 mode |= S_IFDIR;
179 if (fileio_mode & FILEIO_S_IFCHR)
180 mode |= S_IFCHR;
181 if (fileio_mode & FILEIO_S_IRUSR)
182 mode |= S_IRUSR;
183 if (fileio_mode & FILEIO_S_IWUSR)
184 mode |= S_IWUSR;
185 if (fileio_mode & FILEIO_S_IXUSR)
186 mode |= S_IXUSR;
187#ifdef S_IRGRP
188 if (fileio_mode & FILEIO_S_IRGRP)
189 mode |= S_IRGRP;
190#endif
191#ifdef S_IWGRP
192 if (fileio_mode & FILEIO_S_IWGRP)
193 mode |= S_IWGRP;
194#endif
195#ifdef S_IXGRP
196 if (fileio_mode & FILEIO_S_IXGRP)
197 mode |= S_IXGRP;
198#endif
199 if (fileio_mode & FILEIO_S_IROTH)
200 mode |= S_IROTH;
201#ifdef S_IWOTH
202 if (fileio_mode & FILEIO_S_IWOTH)
203 mode |= S_IWOTH;
204#endif
205#ifdef S_IXOTH
206 if (fileio_mode & FILEIO_S_IXOTH)
207 mode |= S_IXOTH;
208#endif
209
210 *mode_p = mode;
211 return 0;
212}
213
791c0056
GB
214/* Convert a host-format mode_t into a bitmask of File-I/O flags. */
215
216static LONGEST
7823a941 217fileio_mode_pack (mode_t mode)
791c0056
GB
218{
219 mode_t tmode = 0;
220
221 if (S_ISREG (mode))
222 tmode |= FILEIO_S_IFREG;
223 if (S_ISDIR (mode))
224 tmode |= FILEIO_S_IFDIR;
225 if (S_ISCHR (mode))
226 tmode |= FILEIO_S_IFCHR;
227 if (mode & S_IRUSR)
228 tmode |= FILEIO_S_IRUSR;
229 if (mode & S_IWUSR)
230 tmode |= FILEIO_S_IWUSR;
231 if (mode & S_IXUSR)
232 tmode |= FILEIO_S_IXUSR;
233#ifdef S_IRGRP
234 if (mode & S_IRGRP)
235 tmode |= FILEIO_S_IRGRP;
236#endif
ecef18c5 237#ifdef S_IWGRP
791c0056
GB
238 if (mode & S_IWGRP)
239 tmode |= FILEIO_S_IWGRP;
240#endif
241#ifdef S_IXGRP
242 if (mode & S_IXGRP)
243 tmode |= FILEIO_S_IXGRP;
244#endif
245 if (mode & S_IROTH)
246 tmode |= FILEIO_S_IROTH;
247#ifdef S_IWOTH
248 if (mode & S_IWOTH)
249 tmode |= FILEIO_S_IWOTH;
250#endif
251#ifdef S_IXOTH
252 if (mode & S_IXOTH)
253 tmode |= FILEIO_S_IXOTH;
254#endif
255 return tmode;
256}
257
258/* Pack a host-format mode_t into an fio_mode_t. */
259
260static void
7823a941 261host_to_fileio_mode (mode_t num, fio_mode_t fnum)
791c0056 262{
7823a941 263 host_to_bigendian (fileio_mode_pack (num), (char *) fnum, 4);
791c0056
GB
264}
265
266/* Pack a host-format integer into an fio_ulong_t. */
267
268static void
7823a941 269host_to_fileio_ulong (LONGEST num, fio_ulong_t fnum)
791c0056 270{
7823a941 271 host_to_bigendian (num, (char *) fnum, 8);
791c0056
GB
272}
273
7823a941 274/* See fileio.h. */
791c0056
GB
275
276void
7823a941 277host_to_fileio_stat (struct stat *st, struct fio_stat *fst)
791c0056
GB
278{
279 LONGEST blksize;
280
7823a941
GB
281 host_to_fileio_uint ((long) st->st_dev, fst->fst_dev);
282 host_to_fileio_uint ((long) st->st_ino, fst->fst_ino);
283 host_to_fileio_mode (st->st_mode, fst->fst_mode);
284 host_to_fileio_uint ((long) st->st_nlink, fst->fst_nlink);
285 host_to_fileio_uint ((long) st->st_uid, fst->fst_uid);
286 host_to_fileio_uint ((long) st->st_gid, fst->fst_gid);
287 host_to_fileio_uint ((long) st->st_rdev, fst->fst_rdev);
288 host_to_fileio_ulong ((LONGEST) st->st_size, fst->fst_size);
791c0056
GB
289#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
290 blksize = st->st_blksize;
291#else
292 blksize = 512;
293#endif
7823a941 294 host_to_fileio_ulong (blksize, fst->fst_blksize);
791c0056 295#if HAVE_STRUCT_STAT_ST_BLOCKS
7823a941 296 host_to_fileio_ulong ((LONGEST) st->st_blocks, fst->fst_blocks);
791c0056
GB
297#else
298 /* FIXME: This is correct for DJGPP, but other systems that don't
299 have st_blocks, if any, might prefer 512 instead of st_blksize.
300 (eliz, 30-12-2003) */
7823a941
GB
301 host_to_fileio_ulong (((LONGEST) st->st_size + blksize - 1)
302 / blksize,
303 fst->fst_blocks);
791c0056 304#endif
7823a941
GB
305 host_to_fileio_time (st->st_atime, fst->fst_atime);
306 host_to_fileio_time (st->st_mtime, fst->fst_mtime);
307 host_to_fileio_time (st->st_ctime, fst->fst_ctime);
791c0056 308}