xref: /netbsd-src/external/gpl3/gdb/dist/gdbsupport/fileio.cc (revision 5ba1f45f2a09259cc846f20c7c5501604d633c90)
18dffb485Schristos /* File-I/O functions for GDB, the GNU debugger.
28dffb485Schristos 
3*5ba1f45fSchristos    Copyright (C) 2003-2024 Free Software Foundation, Inc.
48dffb485Schristos 
58dffb485Schristos    This file is part of GDB.
68dffb485Schristos 
78dffb485Schristos    This program is free software; you can redistribute it and/or modify
88dffb485Schristos    it under the terms of the GNU General Public License as published by
98dffb485Schristos    the Free Software Foundation; either version 3 of the License, or
108dffb485Schristos    (at your option) any later version.
118dffb485Schristos 
128dffb485Schristos    This program is distributed in the hope that it will be useful,
138dffb485Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
148dffb485Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
158dffb485Schristos    GNU General Public License for more details.
168dffb485Schristos 
178dffb485Schristos    You should have received a copy of the GNU General Public License
188dffb485Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
198dffb485Schristos 
208dffb485Schristos #include "fileio.h"
218dffb485Schristos #include <sys/stat.h>
228dffb485Schristos #include <fcntl.h>
238dffb485Schristos 
248dffb485Schristos /* See fileio.h.  */
258dffb485Schristos 
264b169a6bSchristos fileio_error
278dffb485Schristos host_to_fileio_error (int error)
288dffb485Schristos {
298dffb485Schristos   switch (error)
308dffb485Schristos     {
318dffb485Schristos       case EPERM:
328dffb485Schristos 	return FILEIO_EPERM;
338dffb485Schristos       case ENOENT:
348dffb485Schristos 	return FILEIO_ENOENT;
358dffb485Schristos       case EINTR:
368dffb485Schristos 	return FILEIO_EINTR;
378dffb485Schristos       case EIO:
388dffb485Schristos 	return FILEIO_EIO;
398dffb485Schristos       case EBADF:
408dffb485Schristos 	return FILEIO_EBADF;
418dffb485Schristos       case EACCES:
428dffb485Schristos 	return FILEIO_EACCES;
438dffb485Schristos       case EFAULT:
448dffb485Schristos 	return FILEIO_EFAULT;
458dffb485Schristos       case EBUSY:
468dffb485Schristos 	return FILEIO_EBUSY;
478dffb485Schristos       case EEXIST:
488dffb485Schristos 	return FILEIO_EEXIST;
498dffb485Schristos       case ENODEV:
508dffb485Schristos 	return FILEIO_ENODEV;
518dffb485Schristos       case ENOTDIR:
528dffb485Schristos 	return FILEIO_ENOTDIR;
538dffb485Schristos       case EISDIR:
548dffb485Schristos 	return FILEIO_EISDIR;
558dffb485Schristos       case EINVAL:
568dffb485Schristos 	return FILEIO_EINVAL;
578dffb485Schristos       case ENFILE:
588dffb485Schristos 	return FILEIO_ENFILE;
598dffb485Schristos       case EMFILE:
608dffb485Schristos 	return FILEIO_EMFILE;
618dffb485Schristos       case EFBIG:
628dffb485Schristos 	return FILEIO_EFBIG;
638dffb485Schristos       case ENOSPC:
648dffb485Schristos 	return FILEIO_ENOSPC;
658dffb485Schristos       case ESPIPE:
668dffb485Schristos 	return FILEIO_ESPIPE;
678dffb485Schristos       case EROFS:
688dffb485Schristos 	return FILEIO_EROFS;
698dffb485Schristos       case ENOSYS:
708dffb485Schristos 	return FILEIO_ENOSYS;
718dffb485Schristos       case ENAMETOOLONG:
728dffb485Schristos 	return FILEIO_ENAMETOOLONG;
738dffb485Schristos     }
748dffb485Schristos   return FILEIO_EUNKNOWN;
758dffb485Schristos }
768dffb485Schristos 
778dffb485Schristos /* See fileio.h.  */
788dffb485Schristos 
798dffb485Schristos int
804b169a6bSchristos fileio_error_to_host (fileio_error errnum)
814b169a6bSchristos {
824b169a6bSchristos   switch (errnum)
834b169a6bSchristos     {
844b169a6bSchristos       case FILEIO_EPERM:
854b169a6bSchristos 	return EPERM;
864b169a6bSchristos       case FILEIO_ENOENT:
874b169a6bSchristos 	return ENOENT;
884b169a6bSchristos       case FILEIO_EINTR:
894b169a6bSchristos 	return EINTR;
904b169a6bSchristos       case FILEIO_EIO:
914b169a6bSchristos 	return EIO;
924b169a6bSchristos       case FILEIO_EBADF:
934b169a6bSchristos 	return EBADF;
944b169a6bSchristos       case FILEIO_EACCES:
954b169a6bSchristos 	return EACCES;
964b169a6bSchristos       case FILEIO_EFAULT:
974b169a6bSchristos 	return EFAULT;
984b169a6bSchristos       case FILEIO_EBUSY:
994b169a6bSchristos 	return EBUSY;
1004b169a6bSchristos       case FILEIO_EEXIST:
1014b169a6bSchristos 	return EEXIST;
1024b169a6bSchristos       case FILEIO_ENODEV:
1034b169a6bSchristos 	return ENODEV;
1044b169a6bSchristos       case FILEIO_ENOTDIR:
1054b169a6bSchristos 	return ENOTDIR;
1064b169a6bSchristos       case FILEIO_EISDIR:
1074b169a6bSchristos 	return EISDIR;
1084b169a6bSchristos       case FILEIO_EINVAL:
1094b169a6bSchristos 	return EINVAL;
1104b169a6bSchristos       case FILEIO_ENFILE:
1114b169a6bSchristos 	return ENFILE;
1124b169a6bSchristos       case FILEIO_EMFILE:
1134b169a6bSchristos 	return EMFILE;
1144b169a6bSchristos       case FILEIO_EFBIG:
1154b169a6bSchristos 	return EFBIG;
1164b169a6bSchristos       case FILEIO_ENOSPC:
1174b169a6bSchristos 	return ENOSPC;
1184b169a6bSchristos       case FILEIO_ESPIPE:
1194b169a6bSchristos 	return ESPIPE;
1204b169a6bSchristos       case FILEIO_EROFS:
1214b169a6bSchristos 	return EROFS;
1224b169a6bSchristos       case FILEIO_ENOSYS:
1234b169a6bSchristos 	return ENOSYS;
1244b169a6bSchristos       case FILEIO_ENAMETOOLONG:
1254b169a6bSchristos 	return ENAMETOOLONG;
1264b169a6bSchristos     }
1274b169a6bSchristos   return -1;
1284b169a6bSchristos }
1294b169a6bSchristos 
1304b169a6bSchristos /* See fileio.h.  */
1314b169a6bSchristos 
1324b169a6bSchristos int
1338dffb485Schristos fileio_to_host_openflags (int fileio_open_flags, int *open_flags_p)
1348dffb485Schristos {
1358dffb485Schristos   int open_flags = 0;
1368dffb485Schristos 
1378dffb485Schristos   if (fileio_open_flags & ~FILEIO_O_SUPPORTED)
1388dffb485Schristos     return -1;
1398dffb485Schristos 
1408dffb485Schristos   if (fileio_open_flags & FILEIO_O_CREAT)
1418dffb485Schristos     open_flags |= O_CREAT;
1428dffb485Schristos   if (fileio_open_flags & FILEIO_O_EXCL)
1438dffb485Schristos     open_flags |= O_EXCL;
1448dffb485Schristos   if (fileio_open_flags & FILEIO_O_TRUNC)
1458dffb485Schristos     open_flags |= O_TRUNC;
1468dffb485Schristos   if (fileio_open_flags & FILEIO_O_APPEND)
1478dffb485Schristos     open_flags |= O_APPEND;
1488dffb485Schristos   if (fileio_open_flags & FILEIO_O_RDONLY)
1498dffb485Schristos     open_flags |= O_RDONLY;
1508dffb485Schristos   if (fileio_open_flags & FILEIO_O_WRONLY)
1518dffb485Schristos     open_flags |= O_WRONLY;
1528dffb485Schristos   if (fileio_open_flags & FILEIO_O_RDWR)
1538dffb485Schristos     open_flags |= O_RDWR;
1548dffb485Schristos   /* On systems supporting binary and text mode, always open files
1558dffb485Schristos      in binary mode. */
1568dffb485Schristos #ifdef O_BINARY
1578dffb485Schristos   open_flags |= O_BINARY;
1588dffb485Schristos #endif
1598dffb485Schristos 
1608dffb485Schristos   *open_flags_p = open_flags;
1618dffb485Schristos   return 0;
1628dffb485Schristos }
1638dffb485Schristos 
1648dffb485Schristos /* See fileio.h.  */
1658dffb485Schristos 
1668dffb485Schristos int
1678dffb485Schristos fileio_to_host_mode (int fileio_mode, mode_t *mode_p)
1688dffb485Schristos {
1698dffb485Schristos   mode_t mode = 0;
1708dffb485Schristos 
1718dffb485Schristos   if (fileio_mode & ~FILEIO_S_SUPPORTED)
1728dffb485Schristos     return -1;
1738dffb485Schristos 
1748dffb485Schristos   if (fileio_mode & FILEIO_S_IFREG)
1758dffb485Schristos     mode |= S_IFREG;
1768dffb485Schristos   if (fileio_mode & FILEIO_S_IFDIR)
1778dffb485Schristos     mode |= S_IFDIR;
1788dffb485Schristos   if (fileio_mode & FILEIO_S_IFCHR)
1798dffb485Schristos     mode |= S_IFCHR;
1808dffb485Schristos   if (fileio_mode & FILEIO_S_IRUSR)
1818dffb485Schristos     mode |= S_IRUSR;
1828dffb485Schristos   if (fileio_mode & FILEIO_S_IWUSR)
1838dffb485Schristos     mode |= S_IWUSR;
1848dffb485Schristos   if (fileio_mode & FILEIO_S_IXUSR)
1858dffb485Schristos     mode |= S_IXUSR;
1868dffb485Schristos #ifdef S_IRGRP
1878dffb485Schristos   if (fileio_mode & FILEIO_S_IRGRP)
1888dffb485Schristos     mode |= S_IRGRP;
1898dffb485Schristos #endif
1908dffb485Schristos #ifdef S_IWGRP
1918dffb485Schristos   if (fileio_mode & FILEIO_S_IWGRP)
1928dffb485Schristos     mode |= S_IWGRP;
1938dffb485Schristos #endif
1948dffb485Schristos #ifdef S_IXGRP
1958dffb485Schristos   if (fileio_mode & FILEIO_S_IXGRP)
1968dffb485Schristos     mode |= S_IXGRP;
1978dffb485Schristos #endif
1988dffb485Schristos   if (fileio_mode & FILEIO_S_IROTH)
1998dffb485Schristos     mode |= S_IROTH;
2008dffb485Schristos #ifdef S_IWOTH
2018dffb485Schristos   if (fileio_mode & FILEIO_S_IWOTH)
2028dffb485Schristos     mode |= S_IWOTH;
2038dffb485Schristos #endif
2048dffb485Schristos #ifdef S_IXOTH
2058dffb485Schristos   if (fileio_mode & FILEIO_S_IXOTH)
2068dffb485Schristos     mode |= S_IXOTH;
2078dffb485Schristos #endif
2088dffb485Schristos 
2098dffb485Schristos   *mode_p = mode;
2108dffb485Schristos   return 0;
2118dffb485Schristos }
2128dffb485Schristos 
2138dffb485Schristos /* Convert a host-format mode_t into a bitmask of File-I/O flags.  */
2148dffb485Schristos 
2158dffb485Schristos static LONGEST
2168dffb485Schristos fileio_mode_pack (mode_t mode)
2178dffb485Schristos {
2188dffb485Schristos   mode_t tmode = 0;
2198dffb485Schristos 
2208dffb485Schristos   if (S_ISREG (mode))
2218dffb485Schristos     tmode |= FILEIO_S_IFREG;
2228dffb485Schristos   if (S_ISDIR (mode))
2238dffb485Schristos     tmode |= FILEIO_S_IFDIR;
2248dffb485Schristos   if (S_ISCHR (mode))
2258dffb485Schristos     tmode |= FILEIO_S_IFCHR;
2268dffb485Schristos   if (mode & S_IRUSR)
2278dffb485Schristos     tmode |= FILEIO_S_IRUSR;
2288dffb485Schristos   if (mode & S_IWUSR)
2298dffb485Schristos     tmode |= FILEIO_S_IWUSR;
2308dffb485Schristos   if (mode & S_IXUSR)
2318dffb485Schristos     tmode |= FILEIO_S_IXUSR;
2328dffb485Schristos #ifdef S_IRGRP
2338dffb485Schristos   if (mode & S_IRGRP)
2348dffb485Schristos     tmode |= FILEIO_S_IRGRP;
2358dffb485Schristos #endif
2368dffb485Schristos #ifdef S_IWGRP
2378dffb485Schristos   if (mode & S_IWGRP)
2388dffb485Schristos     tmode |= FILEIO_S_IWGRP;
2398dffb485Schristos #endif
2408dffb485Schristos #ifdef S_IXGRP
2418dffb485Schristos   if (mode & S_IXGRP)
2428dffb485Schristos     tmode |= FILEIO_S_IXGRP;
2438dffb485Schristos #endif
2448dffb485Schristos   if (mode & S_IROTH)
2458dffb485Schristos     tmode |= FILEIO_S_IROTH;
2468dffb485Schristos #ifdef S_IWOTH
2478dffb485Schristos   if (mode & S_IWOTH)
2488dffb485Schristos     tmode |= FILEIO_S_IWOTH;
2498dffb485Schristos #endif
2508dffb485Schristos #ifdef S_IXOTH
2518dffb485Schristos   if (mode & S_IXOTH)
2528dffb485Schristos     tmode |= FILEIO_S_IXOTH;
2538dffb485Schristos #endif
2548dffb485Schristos   return tmode;
2558dffb485Schristos }
2568dffb485Schristos 
2578dffb485Schristos /* Pack a host-format mode_t into an fio_mode_t.  */
2588dffb485Schristos 
2598dffb485Schristos static void
2608dffb485Schristos host_to_fileio_mode (mode_t num, fio_mode_t fnum)
2618dffb485Schristos {
2628dffb485Schristos   host_to_bigendian (fileio_mode_pack (num), (char *) fnum, 4);
2638dffb485Schristos }
2648dffb485Schristos 
2658dffb485Schristos /* Pack a host-format integer into an fio_ulong_t.  */
2668dffb485Schristos 
2678dffb485Schristos static void
2688dffb485Schristos host_to_fileio_ulong (LONGEST num, fio_ulong_t fnum)
2698dffb485Schristos {
2708dffb485Schristos   host_to_bigendian (num, (char *) fnum, 8);
2718dffb485Schristos }
2728dffb485Schristos 
2738dffb485Schristos /* See fileio.h.  */
2748dffb485Schristos 
2758dffb485Schristos void
2768dffb485Schristos host_to_fileio_stat (struct stat *st, struct fio_stat *fst)
2778dffb485Schristos {
2788dffb485Schristos   LONGEST blksize;
2798dffb485Schristos 
2808dffb485Schristos   host_to_fileio_uint ((long) st->st_dev, fst->fst_dev);
2818dffb485Schristos   host_to_fileio_uint ((long) st->st_ino, fst->fst_ino);
2828dffb485Schristos   host_to_fileio_mode (st->st_mode, fst->fst_mode);
2838dffb485Schristos   host_to_fileio_uint ((long) st->st_nlink, fst->fst_nlink);
2848dffb485Schristos   host_to_fileio_uint ((long) st->st_uid, fst->fst_uid);
2858dffb485Schristos   host_to_fileio_uint ((long) st->st_gid, fst->fst_gid);
2868dffb485Schristos   host_to_fileio_uint ((long) st->st_rdev, fst->fst_rdev);
2878dffb485Schristos   host_to_fileio_ulong ((LONGEST) st->st_size, fst->fst_size);
2888dffb485Schristos #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
2898dffb485Schristos   blksize = st->st_blksize;
2908dffb485Schristos #else
2918dffb485Schristos   blksize = 512;
2928dffb485Schristos #endif
2938dffb485Schristos   host_to_fileio_ulong (blksize, fst->fst_blksize);
2948dffb485Schristos #if HAVE_STRUCT_STAT_ST_BLOCKS
2958dffb485Schristos   host_to_fileio_ulong ((LONGEST) st->st_blocks, fst->fst_blocks);
2968dffb485Schristos #else
2978dffb485Schristos   /* FIXME: This is correct for DJGPP, but other systems that don't
2988dffb485Schristos      have st_blocks, if any, might prefer 512 instead of st_blksize.
2998dffb485Schristos      (eliz, 30-12-2003)  */
3008dffb485Schristos   host_to_fileio_ulong (((LONGEST) st->st_size + blksize - 1)
3018dffb485Schristos 			/ blksize,
3028dffb485Schristos 			fst->fst_blocks);
3038dffb485Schristos #endif
3048dffb485Schristos   host_to_fileio_time (st->st_atime, fst->fst_atime);
3058dffb485Schristos   host_to_fileio_time (st->st_mtime, fst->fst_mtime);
3068dffb485Schristos   host_to_fileio_time (st->st_ctime, fst->fst_ctime);
3078dffb485Schristos }
308