17d62b00eSchristos /* File-I/O functions for GDB, the GNU debugger. 27d62b00eSchristos 3*6881a400Schristos Copyright (C) 2003-2023 Free Software Foundation, Inc. 47d62b00eSchristos 57d62b00eSchristos This file is part of GDB. 67d62b00eSchristos 77d62b00eSchristos This program is free software; you can redistribute it and/or modify 87d62b00eSchristos it under the terms of the GNU General Public License as published by 97d62b00eSchristos the Free Software Foundation; either version 3 of the License, or 107d62b00eSchristos (at your option) any later version. 117d62b00eSchristos 127d62b00eSchristos This program is distributed in the hope that it will be useful, 137d62b00eSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of 147d62b00eSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 157d62b00eSchristos GNU General Public License for more details. 167d62b00eSchristos 177d62b00eSchristos You should have received a copy of the GNU General Public License 187d62b00eSchristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 197d62b00eSchristos 207d62b00eSchristos #ifndef COMMON_FILEIO_H 217d62b00eSchristos #define COMMON_FILEIO_H 227d62b00eSchristos 237d62b00eSchristos #include <sys/stat.h> 247d62b00eSchristos 25*6881a400Schristos /* The following flags are defined to be independent of the host 26*6881a400Schristos as well as the target side implementation of these constants. 27*6881a400Schristos All constants are defined with a leading FILEIO_ in the name 28*6881a400Schristos to allow the usage of these constants together with the 29*6881a400Schristos corresponding implementation dependent constants in one module. */ 30*6881a400Schristos 31*6881a400Schristos /* open(2) flags */ 32*6881a400Schristos #define FILEIO_O_RDONLY 0x0 33*6881a400Schristos #define FILEIO_O_WRONLY 0x1 34*6881a400Schristos #define FILEIO_O_RDWR 0x2 35*6881a400Schristos #define FILEIO_O_APPEND 0x8 36*6881a400Schristos #define FILEIO_O_CREAT 0x200 37*6881a400Schristos #define FILEIO_O_TRUNC 0x400 38*6881a400Schristos #define FILEIO_O_EXCL 0x800 39*6881a400Schristos #define FILEIO_O_SUPPORTED (FILEIO_O_RDONLY | FILEIO_O_WRONLY| \ 40*6881a400Schristos FILEIO_O_RDWR | FILEIO_O_APPEND| \ 41*6881a400Schristos FILEIO_O_CREAT | FILEIO_O_TRUNC| \ 42*6881a400Schristos FILEIO_O_EXCL) 43*6881a400Schristos 44*6881a400Schristos /* mode_t bits */ 45*6881a400Schristos #define FILEIO_S_IFREG 0100000 46*6881a400Schristos #define FILEIO_S_IFDIR 040000 47*6881a400Schristos #define FILEIO_S_IFCHR 020000 48*6881a400Schristos #define FILEIO_S_IRUSR 0400 49*6881a400Schristos #define FILEIO_S_IWUSR 0200 50*6881a400Schristos #define FILEIO_S_IXUSR 0100 51*6881a400Schristos #define FILEIO_S_IRWXU 0700 52*6881a400Schristos #define FILEIO_S_IRGRP 040 53*6881a400Schristos #define FILEIO_S_IWGRP 020 54*6881a400Schristos #define FILEIO_S_IXGRP 010 55*6881a400Schristos #define FILEIO_S_IRWXG 070 56*6881a400Schristos #define FILEIO_S_IROTH 04 57*6881a400Schristos #define FILEIO_S_IWOTH 02 58*6881a400Schristos #define FILEIO_S_IXOTH 01 59*6881a400Schristos #define FILEIO_S_IRWXO 07 60*6881a400Schristos #define FILEIO_S_SUPPORTED (FILEIO_S_IFREG|FILEIO_S_IFDIR| \ 61*6881a400Schristos FILEIO_S_IRWXU|FILEIO_S_IRWXG| \ 62*6881a400Schristos FILEIO_S_IRWXO) 63*6881a400Schristos 64*6881a400Schristos /* lseek(2) flags */ 65*6881a400Schristos #define FILEIO_SEEK_SET 0 66*6881a400Schristos #define FILEIO_SEEK_CUR 1 67*6881a400Schristos #define FILEIO_SEEK_END 2 68*6881a400Schristos 69*6881a400Schristos /* errno values */ 70*6881a400Schristos enum fileio_error 71*6881a400Schristos { 72*6881a400Schristos FILEIO_SUCCESS = 0, 73*6881a400Schristos FILEIO_EPERM = 1, 74*6881a400Schristos FILEIO_ENOENT = 2, 75*6881a400Schristos FILEIO_EINTR = 4, 76*6881a400Schristos FILEIO_EIO = 5, 77*6881a400Schristos FILEIO_EBADF = 9, 78*6881a400Schristos FILEIO_EACCES = 13, 79*6881a400Schristos FILEIO_EFAULT = 14, 80*6881a400Schristos FILEIO_EBUSY = 16, 81*6881a400Schristos FILEIO_EEXIST = 17, 82*6881a400Schristos FILEIO_ENODEV = 19, 83*6881a400Schristos FILEIO_ENOTDIR = 20, 84*6881a400Schristos FILEIO_EISDIR = 21, 85*6881a400Schristos FILEIO_EINVAL = 22, 86*6881a400Schristos FILEIO_ENFILE = 23, 87*6881a400Schristos FILEIO_EMFILE = 24, 88*6881a400Schristos FILEIO_EFBIG = 27, 89*6881a400Schristos FILEIO_ENOSPC = 28, 90*6881a400Schristos FILEIO_ESPIPE = 29, 91*6881a400Schristos FILEIO_EROFS = 30, 92*6881a400Schristos FILEIO_ENOSYS = 88, 93*6881a400Schristos FILEIO_ENAMETOOLONG = 91, 94*6881a400Schristos FILEIO_EUNKNOWN = 9999, 95*6881a400Schristos }; 96*6881a400Schristos 97*6881a400Schristos #define FIO_INT_LEN 4 98*6881a400Schristos #define FIO_UINT_LEN 4 99*6881a400Schristos #define FIO_MODE_LEN 4 100*6881a400Schristos #define FIO_TIME_LEN 4 101*6881a400Schristos #define FIO_LONG_LEN 8 102*6881a400Schristos #define FIO_ULONG_LEN 8 103*6881a400Schristos 104*6881a400Schristos typedef char fio_int_t[FIO_INT_LEN]; 105*6881a400Schristos typedef char fio_uint_t[FIO_UINT_LEN]; 106*6881a400Schristos typedef char fio_mode_t[FIO_MODE_LEN]; 107*6881a400Schristos typedef char fio_time_t[FIO_TIME_LEN]; 108*6881a400Schristos typedef char fio_long_t[FIO_LONG_LEN]; 109*6881a400Schristos typedef char fio_ulong_t[FIO_ULONG_LEN]; 110*6881a400Schristos 111*6881a400Schristos /* Struct stat as used in protocol. For complete independence 112*6881a400Schristos of host/target systems, it's defined as an array with offsets 113*6881a400Schristos to the members. */ 114*6881a400Schristos 115*6881a400Schristos struct fio_stat 116*6881a400Schristos { 117*6881a400Schristos fio_uint_t fst_dev; 118*6881a400Schristos fio_uint_t fst_ino; 119*6881a400Schristos fio_mode_t fst_mode; 120*6881a400Schristos fio_uint_t fst_nlink; 121*6881a400Schristos fio_uint_t fst_uid; 122*6881a400Schristos fio_uint_t fst_gid; 123*6881a400Schristos fio_uint_t fst_rdev; 124*6881a400Schristos fio_ulong_t fst_size; 125*6881a400Schristos fio_ulong_t fst_blksize; 126*6881a400Schristos fio_ulong_t fst_blocks; 127*6881a400Schristos fio_time_t fst_atime; 128*6881a400Schristos fio_time_t fst_mtime; 129*6881a400Schristos fio_time_t fst_ctime; 130*6881a400Schristos }; 131*6881a400Schristos 132*6881a400Schristos struct fio_timeval 133*6881a400Schristos { 134*6881a400Schristos fio_time_t ftv_sec; 135*6881a400Schristos fio_long_t ftv_usec; 136*6881a400Schristos }; 137*6881a400Schristos 1387d62b00eSchristos /* Convert a host-format errno value to a File-I/O error number. */ 1397d62b00eSchristos 140*6881a400Schristos extern fileio_error host_to_fileio_error (int error); 141*6881a400Schristos 142*6881a400Schristos /* Convert a File-I/O error number to a host-format errno value. */ 143*6881a400Schristos 144*6881a400Schristos extern int fileio_error_to_host (fileio_error errnum); 1457d62b00eSchristos 1467d62b00eSchristos /* Convert File-I/O open flags FFLAGS to host format, storing 1477d62b00eSchristos the result in *FLAGS. Return 0 on success, -1 on error. */ 1487d62b00eSchristos 1497d62b00eSchristos extern int fileio_to_host_openflags (int fflags, int *flags); 1507d62b00eSchristos 1517d62b00eSchristos /* Convert File-I/O mode FMODE to host format, storing 1527d62b00eSchristos the result in *MODE. Return 0 on success, -1 on error. */ 1537d62b00eSchristos 1547d62b00eSchristos extern int fileio_to_host_mode (int fmode, mode_t *mode); 1557d62b00eSchristos 1567d62b00eSchristos /* Pack a host-format integer into a byte buffer in big-endian 1577d62b00eSchristos format. BYTES specifies the size of the integer to pack in 1587d62b00eSchristos bytes. */ 1597d62b00eSchristos 1607d62b00eSchristos static inline void 1617d62b00eSchristos host_to_bigendian (LONGEST num, char *buf, int bytes) 1627d62b00eSchristos { 1637d62b00eSchristos int i; 1647d62b00eSchristos 1657d62b00eSchristos for (i = 0; i < bytes; ++i) 1667d62b00eSchristos buf[i] = (num >> (8 * (bytes - i - 1))) & 0xff; 1677d62b00eSchristos } 1687d62b00eSchristos 1697d62b00eSchristos /* Pack a host-format integer into an fio_uint_t. */ 1707d62b00eSchristos 1717d62b00eSchristos static inline void 1727d62b00eSchristos host_to_fileio_uint (long num, fio_uint_t fnum) 1737d62b00eSchristos { 1747d62b00eSchristos host_to_bigendian ((LONGEST) num, (char *) fnum, 4); 1757d62b00eSchristos } 1767d62b00eSchristos 1777d62b00eSchristos /* Pack a host-format time_t into an fio_time_t. */ 1787d62b00eSchristos 1797d62b00eSchristos static inline void 1807d62b00eSchristos host_to_fileio_time (time_t num, fio_time_t fnum) 1817d62b00eSchristos { 1827d62b00eSchristos host_to_bigendian ((LONGEST) num, (char *) fnum, 4); 1837d62b00eSchristos } 1847d62b00eSchristos 1857d62b00eSchristos /* Pack a host-format struct stat into a struct fio_stat. */ 1867d62b00eSchristos 1877d62b00eSchristos extern void host_to_fileio_stat (struct stat *st, struct fio_stat *fst); 1887d62b00eSchristos 1897d62b00eSchristos #endif /* COMMON_FILEIO_H */ 190