| 1784 | } |
| 1785 | |
| 1786 | int buffer::list::read_file(const char *fn, std::string *error) |
| 1787 | { |
| 1788 | int fd = TEMP_FAILURE_RETRY(::open(fn, O_RDONLY|O_CLOEXEC|O_BINARY)); |
| 1789 | if (fd < 0) { |
| 1790 | int err = errno; |
| 1791 | std::ostringstream oss; |
| 1792 | oss << "can't open " << fn << ": " << cpp_strerror(err); |
| 1793 | *error = oss.str(); |
| 1794 | return -err; |
| 1795 | } |
| 1796 | |
| 1797 | struct stat st; |
| 1798 | // FIPS zeroization audit 20191115: this memset is not security related. |
| 1799 | memset(&st, 0, sizeof(st)); |
| 1800 | if (::fstat(fd, &st) < 0) { |
| 1801 | int err = errno; |
| 1802 | std::ostringstream oss; |
| 1803 | oss << "bufferlist::read_file(" << fn << "): stat error: " |
| 1804 | << cpp_strerror(err); |
| 1805 | *error = oss.str(); |
| 1806 | VOID_TEMP_FAILURE_RETRY(::close(fd)); |
| 1807 | return -err; |
| 1808 | } |
| 1809 | |
| 1810 | ssize_t ret = read_fd(fd, st.st_size); |
| 1811 | if (ret < 0) { |
| 1812 | std::ostringstream oss; |
| 1813 | oss << "bufferlist::read_file(" << fn << "): read error:" |
| 1814 | << cpp_strerror(ret); |
| 1815 | *error = oss.str(); |
| 1816 | VOID_TEMP_FAILURE_RETRY(::close(fd)); |
| 1817 | return ret; |
| 1818 | } |
| 1819 | else if (ret != st.st_size) { |
| 1820 | // Premature EOF. |
| 1821 | // Perhaps the file changed between stat() and read()? |
| 1822 | std::ostringstream oss; |
| 1823 | oss << "bufferlist::read_file(" << fn << "): warning: got premature EOF."; |
| 1824 | *error = oss.str(); |
| 1825 | // not actually an error, but weird |
| 1826 | } |
| 1827 | VOID_TEMP_FAILURE_RETRY(::close(fd)); |
| 1828 | return 0; |
| 1829 | } |
| 1830 | |
| 1831 | ssize_t buffer::list::read_fd(int fd, size_t len) |
| 1832 | { |