| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2016 Philippe Proulx <pproulx@efficios.com> |
| 5 | */ |
| 6 | |
| 7 | #include <glib.h> |
| 8 | #include <stdio.h> |
| 9 | #include <sys/stat.h> |
| 10 | |
| 11 | #include "cpp-common/vendor/fmt/format.h" |
| 12 | |
| 13 | #include "file.hpp" |
| 14 | |
| 15 | int ctf_fs_file_open(struct ctf_fs_file *file, const char *mode) |
| 16 | { |
| 17 | int ret = 0; |
| 18 | struct stat stat; |
| 19 | |
| 20 | BT_CPPLOGI_SPEC(file->logger, "Opening file \"{}\" with mode \"{}\"", file->path, mode); |
| 21 | file->fp.reset(fopen(file->path.c_str(), mode)); |
| 22 | if (!file->fp) { |
| 23 | BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(file->logger, "Cannot open file", ": path={}, mode={}", |
| 24 | file->path, mode); |
| 25 | goto error; |
| 26 | } |
| 27 | |
| 28 | BT_CPPLOGI_SPEC(file->logger, "Opened file: {}", fmt::ptr(file->fp)); |
| 29 | |
| 30 | if (fstat(fileno(file->fp.get()), &stat)) { |
| 31 | BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(file->logger, "Cannot get file information", ": path={}", |
| 32 | file->path); |
| 33 | goto error; |
| 34 | } |
| 35 | |
| 36 | file->size = stat.st_size; |
| 37 | BT_CPPLOGI_SPEC(file->logger, "File is {} bytes", (intmax_t) file->size); |
| 38 | goto end; |
| 39 | |
| 40 | error: |
| 41 | ret = -1; |
| 42 | |
| 43 | end: |
| 44 | return ret; |
| 45 | } |