| 1 | /* Remote File-I/O communications |
| 2 | |
| 3 | Copyright (C) 2003-2015 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 | #include "common-defs.h" |
| 21 | #include "common-remote-fileio.h" |
| 22 | #include <sys/stat.h> |
| 23 | |
| 24 | /* Convert a host-format mode_t into a bitmask of File-I/O flags. */ |
| 25 | |
| 26 | static LONGEST |
| 27 | remote_fileio_mode_to_target (mode_t mode) |
| 28 | { |
| 29 | mode_t tmode = 0; |
| 30 | |
| 31 | if (S_ISREG (mode)) |
| 32 | tmode |= FILEIO_S_IFREG; |
| 33 | if (S_ISDIR (mode)) |
| 34 | tmode |= FILEIO_S_IFDIR; |
| 35 | if (S_ISCHR (mode)) |
| 36 | tmode |= FILEIO_S_IFCHR; |
| 37 | if (mode & S_IRUSR) |
| 38 | tmode |= FILEIO_S_IRUSR; |
| 39 | if (mode & S_IWUSR) |
| 40 | tmode |= FILEIO_S_IWUSR; |
| 41 | if (mode & S_IXUSR) |
| 42 | tmode |= FILEIO_S_IXUSR; |
| 43 | #ifdef S_IRGRP |
| 44 | if (mode & S_IRGRP) |
| 45 | tmode |= FILEIO_S_IRGRP; |
| 46 | #endif |
| 47 | #ifdef S_IWRGRP |
| 48 | if (mode & S_IWGRP) |
| 49 | tmode |= FILEIO_S_IWGRP; |
| 50 | #endif |
| 51 | #ifdef S_IXGRP |
| 52 | if (mode & S_IXGRP) |
| 53 | tmode |= FILEIO_S_IXGRP; |
| 54 | #endif |
| 55 | if (mode & S_IROTH) |
| 56 | tmode |= FILEIO_S_IROTH; |
| 57 | #ifdef S_IWOTH |
| 58 | if (mode & S_IWOTH) |
| 59 | tmode |= FILEIO_S_IWOTH; |
| 60 | #endif |
| 61 | #ifdef S_IXOTH |
| 62 | if (mode & S_IXOTH) |
| 63 | tmode |= FILEIO_S_IXOTH; |
| 64 | #endif |
| 65 | return tmode; |
| 66 | } |
| 67 | |
| 68 | /* Pack a host-format mode_t into an fio_mode_t. */ |
| 69 | |
| 70 | static void |
| 71 | remote_fileio_to_fio_mode (mode_t num, fio_mode_t fnum) |
| 72 | { |
| 73 | remote_fileio_to_be (remote_fileio_mode_to_target (num), |
| 74 | (char *) fnum, 4); |
| 75 | } |
| 76 | |
| 77 | /* Pack a host-format integer into an fio_ulong_t. */ |
| 78 | |
| 79 | static void |
| 80 | remote_fileio_to_fio_ulong (LONGEST num, fio_ulong_t fnum) |
| 81 | { |
| 82 | remote_fileio_to_be (num, (char *) fnum, 8); |
| 83 | } |
| 84 | |
| 85 | /* See common-remote-fileio.h. */ |
| 86 | |
| 87 | void |
| 88 | remote_fileio_to_fio_stat (struct stat *st, struct fio_stat *fst) |
| 89 | { |
| 90 | LONGEST blksize; |
| 91 | |
| 92 | remote_fileio_to_fio_uint ((long) st->st_dev, fst->fst_dev); |
| 93 | remote_fileio_to_fio_uint ((long) st->st_ino, fst->fst_ino); |
| 94 | remote_fileio_to_fio_mode (st->st_mode, fst->fst_mode); |
| 95 | remote_fileio_to_fio_uint ((long) st->st_nlink, fst->fst_nlink); |
| 96 | remote_fileio_to_fio_uint ((long) st->st_uid, fst->fst_uid); |
| 97 | remote_fileio_to_fio_uint ((long) st->st_gid, fst->fst_gid); |
| 98 | remote_fileio_to_fio_uint ((long) st->st_rdev, fst->fst_rdev); |
| 99 | remote_fileio_to_fio_ulong ((LONGEST) st->st_size, fst->fst_size); |
| 100 | #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE |
| 101 | blksize = st->st_blksize; |
| 102 | #else |
| 103 | blksize = 512; |
| 104 | #endif |
| 105 | remote_fileio_to_fio_ulong (blksize, fst->fst_blksize); |
| 106 | #if HAVE_STRUCT_STAT_ST_BLOCKS |
| 107 | remote_fileio_to_fio_ulong ((LONGEST) st->st_blocks, fst->fst_blocks); |
| 108 | #else |
| 109 | /* FIXME: This is correct for DJGPP, but other systems that don't |
| 110 | have st_blocks, if any, might prefer 512 instead of st_blksize. |
| 111 | (eliz, 30-12-2003) */ |
| 112 | remote_fileio_to_fio_ulong (((LONGEST) st->st_size + blksize - 1) |
| 113 | / blksize, |
| 114 | fst->fst_blocks); |
| 115 | #endif |
| 116 | remote_fileio_to_fio_time (st->st_atime, fst->fst_atime); |
| 117 | remote_fileio_to_fio_time (st->st_mtime, fst->fst_mtime); |
| 118 | remote_fileio_to_fio_time (st->st_ctime, fst->fst_ctime); |
| 119 | } |