xref: /netbsd-src/external/gpl3/gdb.old/dist/gdbsupport/fileio.cc (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
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 #include "common-defs.h"
217d62b00eSchristos #include "fileio.h"
227d62b00eSchristos #include <sys/stat.h>
237d62b00eSchristos #include <fcntl.h>
247d62b00eSchristos 
257d62b00eSchristos /* See fileio.h.  */
267d62b00eSchristos 
27*6881a400Schristos fileio_error
287d62b00eSchristos host_to_fileio_error (int error)
297d62b00eSchristos {
307d62b00eSchristos   switch (error)
317d62b00eSchristos     {
327d62b00eSchristos       case EPERM:
337d62b00eSchristos 	return FILEIO_EPERM;
347d62b00eSchristos       case ENOENT:
357d62b00eSchristos 	return FILEIO_ENOENT;
367d62b00eSchristos       case EINTR:
377d62b00eSchristos 	return FILEIO_EINTR;
387d62b00eSchristos       case EIO:
397d62b00eSchristos 	return FILEIO_EIO;
407d62b00eSchristos       case EBADF:
417d62b00eSchristos 	return FILEIO_EBADF;
427d62b00eSchristos       case EACCES:
437d62b00eSchristos 	return FILEIO_EACCES;
447d62b00eSchristos       case EFAULT:
457d62b00eSchristos 	return FILEIO_EFAULT;
467d62b00eSchristos       case EBUSY:
477d62b00eSchristos 	return FILEIO_EBUSY;
487d62b00eSchristos       case EEXIST:
497d62b00eSchristos 	return FILEIO_EEXIST;
507d62b00eSchristos       case ENODEV:
517d62b00eSchristos 	return FILEIO_ENODEV;
527d62b00eSchristos       case ENOTDIR:
537d62b00eSchristos 	return FILEIO_ENOTDIR;
547d62b00eSchristos       case EISDIR:
557d62b00eSchristos 	return FILEIO_EISDIR;
567d62b00eSchristos       case EINVAL:
577d62b00eSchristos 	return FILEIO_EINVAL;
587d62b00eSchristos       case ENFILE:
597d62b00eSchristos 	return FILEIO_ENFILE;
607d62b00eSchristos       case EMFILE:
617d62b00eSchristos 	return FILEIO_EMFILE;
627d62b00eSchristos       case EFBIG:
637d62b00eSchristos 	return FILEIO_EFBIG;
647d62b00eSchristos       case ENOSPC:
657d62b00eSchristos 	return FILEIO_ENOSPC;
667d62b00eSchristos       case ESPIPE:
677d62b00eSchristos 	return FILEIO_ESPIPE;
687d62b00eSchristos       case EROFS:
697d62b00eSchristos 	return FILEIO_EROFS;
707d62b00eSchristos       case ENOSYS:
717d62b00eSchristos 	return FILEIO_ENOSYS;
727d62b00eSchristos       case ENAMETOOLONG:
737d62b00eSchristos 	return FILEIO_ENAMETOOLONG;
747d62b00eSchristos     }
757d62b00eSchristos   return FILEIO_EUNKNOWN;
767d62b00eSchristos }
777d62b00eSchristos 
787d62b00eSchristos /* See fileio.h.  */
797d62b00eSchristos 
807d62b00eSchristos int
81*6881a400Schristos fileio_error_to_host (fileio_error errnum)
82*6881a400Schristos {
83*6881a400Schristos   switch (errnum)
84*6881a400Schristos     {
85*6881a400Schristos       case FILEIO_EPERM:
86*6881a400Schristos 	return EPERM;
87*6881a400Schristos       case FILEIO_ENOENT:
88*6881a400Schristos 	return ENOENT;
89*6881a400Schristos       case FILEIO_EINTR:
90*6881a400Schristos 	return EINTR;
91*6881a400Schristos       case FILEIO_EIO:
92*6881a400Schristos 	return EIO;
93*6881a400Schristos       case FILEIO_EBADF:
94*6881a400Schristos 	return EBADF;
95*6881a400Schristos       case FILEIO_EACCES:
96*6881a400Schristos 	return EACCES;
97*6881a400Schristos       case FILEIO_EFAULT:
98*6881a400Schristos 	return EFAULT;
99*6881a400Schristos       case FILEIO_EBUSY:
100*6881a400Schristos 	return EBUSY;
101*6881a400Schristos       case FILEIO_EEXIST:
102*6881a400Schristos 	return EEXIST;
103*6881a400Schristos       case FILEIO_ENODEV:
104*6881a400Schristos 	return ENODEV;
105*6881a400Schristos       case FILEIO_ENOTDIR:
106*6881a400Schristos 	return ENOTDIR;
107*6881a400Schristos       case FILEIO_EISDIR:
108*6881a400Schristos 	return EISDIR;
109*6881a400Schristos       case FILEIO_EINVAL:
110*6881a400Schristos 	return EINVAL;
111*6881a400Schristos       case FILEIO_ENFILE:
112*6881a400Schristos 	return ENFILE;
113*6881a400Schristos       case FILEIO_EMFILE:
114*6881a400Schristos 	return EMFILE;
115*6881a400Schristos       case FILEIO_EFBIG:
116*6881a400Schristos 	return EFBIG;
117*6881a400Schristos       case FILEIO_ENOSPC:
118*6881a400Schristos 	return ENOSPC;
119*6881a400Schristos       case FILEIO_ESPIPE:
120*6881a400Schristos 	return ESPIPE;
121*6881a400Schristos       case FILEIO_EROFS:
122*6881a400Schristos 	return EROFS;
123*6881a400Schristos       case FILEIO_ENOSYS:
124*6881a400Schristos 	return ENOSYS;
125*6881a400Schristos       case FILEIO_ENAMETOOLONG:
126*6881a400Schristos 	return ENAMETOOLONG;
127*6881a400Schristos     }
128*6881a400Schristos   return -1;
129*6881a400Schristos }
130*6881a400Schristos 
131*6881a400Schristos /* See fileio.h.  */
132*6881a400Schristos 
133*6881a400Schristos int
1347d62b00eSchristos fileio_to_host_openflags (int fileio_open_flags, int *open_flags_p)
1357d62b00eSchristos {
1367d62b00eSchristos   int open_flags = 0;
1377d62b00eSchristos 
1387d62b00eSchristos   if (fileio_open_flags & ~FILEIO_O_SUPPORTED)
1397d62b00eSchristos     return -1;
1407d62b00eSchristos 
1417d62b00eSchristos   if (fileio_open_flags & FILEIO_O_CREAT)
1427d62b00eSchristos     open_flags |= O_CREAT;
1437d62b00eSchristos   if (fileio_open_flags & FILEIO_O_EXCL)
1447d62b00eSchristos     open_flags |= O_EXCL;
1457d62b00eSchristos   if (fileio_open_flags & FILEIO_O_TRUNC)
1467d62b00eSchristos     open_flags |= O_TRUNC;
1477d62b00eSchristos   if (fileio_open_flags & FILEIO_O_APPEND)
1487d62b00eSchristos     open_flags |= O_APPEND;
1497d62b00eSchristos   if (fileio_open_flags & FILEIO_O_RDONLY)
1507d62b00eSchristos     open_flags |= O_RDONLY;
1517d62b00eSchristos   if (fileio_open_flags & FILEIO_O_WRONLY)
1527d62b00eSchristos     open_flags |= O_WRONLY;
1537d62b00eSchristos   if (fileio_open_flags & FILEIO_O_RDWR)
1547d62b00eSchristos     open_flags |= O_RDWR;
1557d62b00eSchristos   /* On systems supporting binary and text mode, always open files
1567d62b00eSchristos      in binary mode. */
1577d62b00eSchristos #ifdef O_BINARY
1587d62b00eSchristos   open_flags |= O_BINARY;
1597d62b00eSchristos #endif
1607d62b00eSchristos 
1617d62b00eSchristos   *open_flags_p = open_flags;
1627d62b00eSchristos   return 0;
1637d62b00eSchristos }
1647d62b00eSchristos 
1657d62b00eSchristos /* See fileio.h.  */
1667d62b00eSchristos 
1677d62b00eSchristos int
1687d62b00eSchristos fileio_to_host_mode (int fileio_mode, mode_t *mode_p)
1697d62b00eSchristos {
1707d62b00eSchristos   mode_t mode = 0;
1717d62b00eSchristos 
1727d62b00eSchristos   if (fileio_mode & ~FILEIO_S_SUPPORTED)
1737d62b00eSchristos     return -1;
1747d62b00eSchristos 
1757d62b00eSchristos   if (fileio_mode & FILEIO_S_IFREG)
1767d62b00eSchristos     mode |= S_IFREG;
1777d62b00eSchristos   if (fileio_mode & FILEIO_S_IFDIR)
1787d62b00eSchristos     mode |= S_IFDIR;
1797d62b00eSchristos   if (fileio_mode & FILEIO_S_IFCHR)
1807d62b00eSchristos     mode |= S_IFCHR;
1817d62b00eSchristos   if (fileio_mode & FILEIO_S_IRUSR)
1827d62b00eSchristos     mode |= S_IRUSR;
1837d62b00eSchristos   if (fileio_mode & FILEIO_S_IWUSR)
1847d62b00eSchristos     mode |= S_IWUSR;
1857d62b00eSchristos   if (fileio_mode & FILEIO_S_IXUSR)
1867d62b00eSchristos     mode |= S_IXUSR;
1877d62b00eSchristos #ifdef S_IRGRP
1887d62b00eSchristos   if (fileio_mode & FILEIO_S_IRGRP)
1897d62b00eSchristos     mode |= S_IRGRP;
1907d62b00eSchristos #endif
1917d62b00eSchristos #ifdef S_IWGRP
1927d62b00eSchristos   if (fileio_mode & FILEIO_S_IWGRP)
1937d62b00eSchristos     mode |= S_IWGRP;
1947d62b00eSchristos #endif
1957d62b00eSchristos #ifdef S_IXGRP
1967d62b00eSchristos   if (fileio_mode & FILEIO_S_IXGRP)
1977d62b00eSchristos     mode |= S_IXGRP;
1987d62b00eSchristos #endif
1997d62b00eSchristos   if (fileio_mode & FILEIO_S_IROTH)
2007d62b00eSchristos     mode |= S_IROTH;
2017d62b00eSchristos #ifdef S_IWOTH
2027d62b00eSchristos   if (fileio_mode & FILEIO_S_IWOTH)
2037d62b00eSchristos     mode |= S_IWOTH;
2047d62b00eSchristos #endif
2057d62b00eSchristos #ifdef S_IXOTH
2067d62b00eSchristos   if (fileio_mode & FILEIO_S_IXOTH)
2077d62b00eSchristos     mode |= S_IXOTH;
2087d62b00eSchristos #endif
2097d62b00eSchristos 
2107d62b00eSchristos   *mode_p = mode;
2117d62b00eSchristos   return 0;
2127d62b00eSchristos }
2137d62b00eSchristos 
2147d62b00eSchristos /* Convert a host-format mode_t into a bitmask of File-I/O flags.  */
2157d62b00eSchristos 
2167d62b00eSchristos static LONGEST
2177d62b00eSchristos fileio_mode_pack (mode_t mode)
2187d62b00eSchristos {
2197d62b00eSchristos   mode_t tmode = 0;
2207d62b00eSchristos 
2217d62b00eSchristos   if (S_ISREG (mode))
2227d62b00eSchristos     tmode |= FILEIO_S_IFREG;
2237d62b00eSchristos   if (S_ISDIR (mode))
2247d62b00eSchristos     tmode |= FILEIO_S_IFDIR;
2257d62b00eSchristos   if (S_ISCHR (mode))
2267d62b00eSchristos     tmode |= FILEIO_S_IFCHR;
2277d62b00eSchristos   if (mode & S_IRUSR)
2287d62b00eSchristos     tmode |= FILEIO_S_IRUSR;
2297d62b00eSchristos   if (mode & S_IWUSR)
2307d62b00eSchristos     tmode |= FILEIO_S_IWUSR;
2317d62b00eSchristos   if (mode & S_IXUSR)
2327d62b00eSchristos     tmode |= FILEIO_S_IXUSR;
2337d62b00eSchristos #ifdef S_IRGRP
2347d62b00eSchristos   if (mode & S_IRGRP)
2357d62b00eSchristos     tmode |= FILEIO_S_IRGRP;
2367d62b00eSchristos #endif
2377d62b00eSchristos #ifdef S_IWGRP
2387d62b00eSchristos   if (mode & S_IWGRP)
2397d62b00eSchristos     tmode |= FILEIO_S_IWGRP;
2407d62b00eSchristos #endif
2417d62b00eSchristos #ifdef S_IXGRP
2427d62b00eSchristos   if (mode & S_IXGRP)
2437d62b00eSchristos     tmode |= FILEIO_S_IXGRP;
2447d62b00eSchristos #endif
2457d62b00eSchristos   if (mode & S_IROTH)
2467d62b00eSchristos     tmode |= FILEIO_S_IROTH;
2477d62b00eSchristos #ifdef S_IWOTH
2487d62b00eSchristos   if (mode & S_IWOTH)
2497d62b00eSchristos     tmode |= FILEIO_S_IWOTH;
2507d62b00eSchristos #endif
2517d62b00eSchristos #ifdef S_IXOTH
2527d62b00eSchristos   if (mode & S_IXOTH)
2537d62b00eSchristos     tmode |= FILEIO_S_IXOTH;
2547d62b00eSchristos #endif
2557d62b00eSchristos   return tmode;
2567d62b00eSchristos }
2577d62b00eSchristos 
2587d62b00eSchristos /* Pack a host-format mode_t into an fio_mode_t.  */
2597d62b00eSchristos 
2607d62b00eSchristos static void
2617d62b00eSchristos host_to_fileio_mode (mode_t num, fio_mode_t fnum)
2627d62b00eSchristos {
2637d62b00eSchristos   host_to_bigendian (fileio_mode_pack (num), (char *) fnum, 4);
2647d62b00eSchristos }
2657d62b00eSchristos 
2667d62b00eSchristos /* Pack a host-format integer into an fio_ulong_t.  */
2677d62b00eSchristos 
2687d62b00eSchristos static void
2697d62b00eSchristos host_to_fileio_ulong (LONGEST num, fio_ulong_t fnum)
2707d62b00eSchristos {
2717d62b00eSchristos   host_to_bigendian (num, (char *) fnum, 8);
2727d62b00eSchristos }
2737d62b00eSchristos 
2747d62b00eSchristos /* See fileio.h.  */
2757d62b00eSchristos 
2767d62b00eSchristos void
2777d62b00eSchristos host_to_fileio_stat (struct stat *st, struct fio_stat *fst)
2787d62b00eSchristos {
2797d62b00eSchristos   LONGEST blksize;
2807d62b00eSchristos 
2817d62b00eSchristos   host_to_fileio_uint ((long) st->st_dev, fst->fst_dev);
2827d62b00eSchristos   host_to_fileio_uint ((long) st->st_ino, fst->fst_ino);
2837d62b00eSchristos   host_to_fileio_mode (st->st_mode, fst->fst_mode);
2847d62b00eSchristos   host_to_fileio_uint ((long) st->st_nlink, fst->fst_nlink);
2857d62b00eSchristos   host_to_fileio_uint ((long) st->st_uid, fst->fst_uid);
2867d62b00eSchristos   host_to_fileio_uint ((long) st->st_gid, fst->fst_gid);
2877d62b00eSchristos   host_to_fileio_uint ((long) st->st_rdev, fst->fst_rdev);
2887d62b00eSchristos   host_to_fileio_ulong ((LONGEST) st->st_size, fst->fst_size);
2897d62b00eSchristos #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
2907d62b00eSchristos   blksize = st->st_blksize;
2917d62b00eSchristos #else
2927d62b00eSchristos   blksize = 512;
2937d62b00eSchristos #endif
2947d62b00eSchristos   host_to_fileio_ulong (blksize, fst->fst_blksize);
2957d62b00eSchristos #if HAVE_STRUCT_STAT_ST_BLOCKS
2967d62b00eSchristos   host_to_fileio_ulong ((LONGEST) st->st_blocks, fst->fst_blocks);
2977d62b00eSchristos #else
2987d62b00eSchristos   /* FIXME: This is correct for DJGPP, but other systems that don't
2997d62b00eSchristos      have st_blocks, if any, might prefer 512 instead of st_blksize.
3007d62b00eSchristos      (eliz, 30-12-2003)  */
3017d62b00eSchristos   host_to_fileio_ulong (((LONGEST) st->st_size + blksize - 1)
3027d62b00eSchristos 			/ blksize,
3037d62b00eSchristos 			fst->fst_blocks);
3047d62b00eSchristos #endif
3057d62b00eSchristos   host_to_fileio_time (st->st_atime, fst->fst_atime);
3067d62b00eSchristos   host_to_fileio_time (st->st_mtime, fst->fst_mtime);
3077d62b00eSchristos   host_to_fileio_time (st->st_ctime, fst->fst_ctime);
3087d62b00eSchristos }
309