fileio.h revision 1.1.1.2
1/* File-I/O functions for GDB, the GNU debugger.
2
3   Copyright (C) 2003-2023 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#ifndef COMMON_FILEIO_H
21#define COMMON_FILEIO_H
22
23#include <sys/stat.h>
24
25/* The following flags are defined to be independent of the host
26   as well as the target side implementation of these constants.
27   All constants are defined with a leading FILEIO_ in the name
28   to allow the usage of these constants together with the
29   corresponding implementation dependent constants in one module. */
30
31/* open(2) flags */
32#define FILEIO_O_RDONLY           0x0
33#define FILEIO_O_WRONLY           0x1
34#define FILEIO_O_RDWR             0x2
35#define FILEIO_O_APPEND           0x8
36#define FILEIO_O_CREAT          0x200
37#define FILEIO_O_TRUNC          0x400
38#define FILEIO_O_EXCL           0x800
39#define FILEIO_O_SUPPORTED      (FILEIO_O_RDONLY | FILEIO_O_WRONLY| \
40				 FILEIO_O_RDWR   | FILEIO_O_APPEND| \
41				 FILEIO_O_CREAT  | FILEIO_O_TRUNC| \
42				 FILEIO_O_EXCL)
43
44/* mode_t bits */
45#define FILEIO_S_IFREG        0100000
46#define FILEIO_S_IFDIR         040000
47#define FILEIO_S_IFCHR         020000
48#define FILEIO_S_IRUSR           0400
49#define FILEIO_S_IWUSR           0200
50#define FILEIO_S_IXUSR           0100
51#define FILEIO_S_IRWXU           0700
52#define FILEIO_S_IRGRP            040
53#define FILEIO_S_IWGRP            020
54#define FILEIO_S_IXGRP            010
55#define FILEIO_S_IRWXG            070
56#define FILEIO_S_IROTH             04
57#define FILEIO_S_IWOTH             02
58#define FILEIO_S_IXOTH             01
59#define FILEIO_S_IRWXO             07
60#define FILEIO_S_SUPPORTED         (FILEIO_S_IFREG|FILEIO_S_IFDIR|  \
61				    FILEIO_S_IRWXU|FILEIO_S_IRWXG|  \
62                                    FILEIO_S_IRWXO)
63
64/* lseek(2) flags */
65#define FILEIO_SEEK_SET             0
66#define FILEIO_SEEK_CUR             1
67#define FILEIO_SEEK_END             2
68
69/* errno values */
70enum fileio_error
71{
72  FILEIO_SUCCESS      =    0,
73  FILEIO_EPERM        =    1,
74  FILEIO_ENOENT       =    2,
75  FILEIO_EINTR        =    4,
76  FILEIO_EIO          =    5,
77  FILEIO_EBADF        =    9,
78  FILEIO_EACCES       =   13,
79  FILEIO_EFAULT       =   14,
80  FILEIO_EBUSY        =   16,
81  FILEIO_EEXIST       =   17,
82  FILEIO_ENODEV       =   19,
83  FILEIO_ENOTDIR      =   20,
84  FILEIO_EISDIR       =   21,
85  FILEIO_EINVAL       =   22,
86  FILEIO_ENFILE       =   23,
87  FILEIO_EMFILE       =   24,
88  FILEIO_EFBIG        =   27,
89  FILEIO_ENOSPC       =   28,
90  FILEIO_ESPIPE       =   29,
91  FILEIO_EROFS        =   30,
92  FILEIO_ENOSYS       =   88,
93  FILEIO_ENAMETOOLONG =   91,
94  FILEIO_EUNKNOWN     = 9999,
95};
96
97#define FIO_INT_LEN   4
98#define FIO_UINT_LEN  4
99#define FIO_MODE_LEN  4
100#define FIO_TIME_LEN  4
101#define FIO_LONG_LEN  8
102#define FIO_ULONG_LEN 8
103
104typedef char fio_int_t[FIO_INT_LEN];
105typedef char fio_uint_t[FIO_UINT_LEN];
106typedef char fio_mode_t[FIO_MODE_LEN];
107typedef char fio_time_t[FIO_TIME_LEN];
108typedef char fio_long_t[FIO_LONG_LEN];
109typedef char fio_ulong_t[FIO_ULONG_LEN];
110
111/* Struct stat as used in protocol.  For complete independence
112   of host/target systems, it's defined as an array with offsets
113   to the members. */
114
115struct fio_stat
116{
117  fio_uint_t  fst_dev;
118  fio_uint_t  fst_ino;
119  fio_mode_t  fst_mode;
120  fio_uint_t  fst_nlink;
121  fio_uint_t  fst_uid;
122  fio_uint_t  fst_gid;
123  fio_uint_t  fst_rdev;
124  fio_ulong_t fst_size;
125  fio_ulong_t fst_blksize;
126  fio_ulong_t fst_blocks;
127  fio_time_t  fst_atime;
128  fio_time_t  fst_mtime;
129  fio_time_t  fst_ctime;
130};
131
132struct fio_timeval
133{
134  fio_time_t  ftv_sec;
135  fio_long_t  ftv_usec;
136};
137
138/* Convert a host-format errno value to a File-I/O error number.  */
139
140extern fileio_error host_to_fileio_error (int error);
141
142/* Convert a File-I/O error number to a host-format errno value.  */
143
144extern int fileio_error_to_host (fileio_error errnum);
145
146/* Convert File-I/O open flags FFLAGS to host format, storing
147   the result in *FLAGS.  Return 0 on success, -1 on error.  */
148
149extern int fileio_to_host_openflags (int fflags, int *flags);
150
151/* Convert File-I/O mode FMODE to host format, storing
152   the result in *MODE.  Return 0 on success, -1 on error.  */
153
154extern int fileio_to_host_mode (int fmode, mode_t *mode);
155
156/* Pack a host-format integer into a byte buffer in big-endian
157   format.  BYTES specifies the size of the integer to pack in
158   bytes.  */
159
160static inline void
161host_to_bigendian (LONGEST num, char *buf, int bytes)
162{
163  int i;
164
165  for (i = 0; i < bytes; ++i)
166    buf[i] = (num >> (8 * (bytes - i - 1))) & 0xff;
167}
168
169/* Pack a host-format integer into an fio_uint_t.  */
170
171static inline void
172host_to_fileio_uint (long num, fio_uint_t fnum)
173{
174  host_to_bigendian ((LONGEST) num, (char *) fnum, 4);
175}
176
177/* Pack a host-format time_t into an fio_time_t.  */
178
179static inline void
180host_to_fileio_time (time_t num, fio_time_t fnum)
181{
182  host_to_bigendian ((LONGEST) num, (char *) fnum, 4);
183}
184
185/* Pack a host-format struct stat into a struct fio_stat.  */
186
187extern void host_to_fileio_stat (struct stat *st, struct fio_stat *fst);
188
189#endif /* COMMON_FILEIO_H */
190