lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* Convert between the NaCl ABI's `struct stat' format, and libc's. |
| 2 | Copyright (C) 2015 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <string.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <xstatconv.h> |
| 22 | |
| 23 | internal_function |
| 24 | int |
| 25 | __xstat_conv (int vers, const struct nacl_abi_stat *kbuf, void *ubuf) |
| 26 | { |
| 27 | /* It's kosher enough just to crash here, but there are some |
| 28 | existing NaCl tests that like to see EFAULT, and that's what |
| 29 | making the IRT call with NULL would give. */ |
| 30 | if (__glibc_unlikely (ubuf == NULL)) |
| 31 | { |
| 32 | __set_errno (EFAULT); |
| 33 | return -1; |
| 34 | } |
| 35 | |
| 36 | switch (vers) |
| 37 | { |
| 38 | case _STAT_VER_NACL: |
| 39 | /* Nothing to do. The struct is in the form the NaCl ABI expects. */ |
| 40 | *(struct nacl_abi_stat *) ubuf = *kbuf; |
| 41 | break; |
| 42 | |
| 43 | case _STAT_VER_LINUX: |
| 44 | { |
| 45 | struct stat *buf = ubuf; |
| 46 | |
| 47 | /* Zero-fill the pad/unused fields. */ |
| 48 | memset (buf, 0, sizeof *buf); |
| 49 | |
| 50 | /* Convert from NaCl IRT ABI `struct stat'. */ |
| 51 | buf->st_dev = kbuf->nacl_abi_st_dev; |
| 52 | buf->st_ino = kbuf->nacl_abi_st_ino; |
| 53 | buf->st_mode = kbuf->nacl_abi_st_mode; |
| 54 | buf->st_nlink = kbuf->nacl_abi_st_nlink; |
| 55 | buf->st_uid = kbuf->nacl_abi_st_uid; |
| 56 | buf->st_gid = kbuf->nacl_abi_st_gid; |
| 57 | buf->st_rdev = kbuf->nacl_abi_st_rdev; |
| 58 | buf->st_size = kbuf->nacl_abi_st_size; |
| 59 | buf->st_blksize = kbuf->nacl_abi_st_blksize; |
| 60 | buf->st_blocks = kbuf->nacl_abi_st_blocks; |
| 61 | buf->st_atim.tv_sec = kbuf->nacl_abi_st_atime; |
| 62 | buf->st_atim.tv_nsec = kbuf->nacl_abi_st_atimensec; |
| 63 | buf->st_mtim.tv_sec = kbuf->nacl_abi_st_mtime; |
| 64 | buf->st_mtim.tv_nsec = kbuf->nacl_abi_st_mtimensec; |
| 65 | buf->st_ctim.tv_sec = kbuf->nacl_abi_st_ctime; |
| 66 | buf->st_ctim.tv_nsec = kbuf->nacl_abi_st_ctimensec; |
| 67 | } |
| 68 | break; |
| 69 | |
| 70 | default: |
| 71 | __set_errno (EINVAL); |
| 72 | return -1; |
| 73 | } |
| 74 | |
| 75 | return 0; |
| 76 | } |