Commit | Line | Data |
---|---|---|
e98a2d6e | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
e98a2d6e | 3 | * |
0235b0db | 4 | * Copyright 2016 Philippe Proulx <pproulx@efficios.com> |
e98a2d6e PP |
5 | */ |
6 | ||
c802cacb SM |
7 | #include <glib.h> |
8 | #include <stdio.h> | |
9 | #include <sys/stat.h> | |
c802cacb | 10 | |
0f5c5d5c | 11 | #include "cpp-common/vendor/fmt/format.h" |
98903a3e | 12 | |
087cd0f5 | 13 | #include "file.hpp" |
e98a2d6e PP |
14 | |
15 | void ctf_fs_file_destroy(struct ctf_fs_file *file) | |
16 | { | |
4164020e SM |
17 | if (!file) { |
18 | return; | |
19 | } | |
20 | ||
afb0f12b | 21 | delete file; |
e98a2d6e PP |
22 | } |
23 | ||
5ff12b25 | 24 | void ctf_fs_file_deleter::operator()(ctf_fs_file * const file) noexcept |
e98a2d6e | 25 | { |
5ff12b25 SM |
26 | ctf_fs_file_destroy(file); |
27 | } | |
28 | ||
29 | ctf_fs_file::UP ctf_fs_file_create(const bt2c::Logger& parentLogger) | |
30 | { | |
a39d9817 | 31 | return ctf_fs_file::UP {new ctf_fs_file {parentLogger}}; |
e98a2d6e PP |
32 | } |
33 | ||
55314f2a | 34 | int ctf_fs_file_open(struct ctf_fs_file *file, const char *mode) |
e98a2d6e | 35 | { |
4164020e SM |
36 | int ret = 0; |
37 | struct stat stat; | |
38 | ||
a39d9817 SM |
39 | BT_CPPLOGI_SPEC(file->logger, "Opening file \"{}\" with mode \"{}\"", file->path, mode); |
40 | file->fp.reset(fopen(file->path.c_str(), mode)); | |
4164020e | 41 | if (!file->fp) { |
0f5c5d5c | 42 | BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(file->logger, "Cannot open file", ": path={}, mode={}", |
a39d9817 | 43 | file->path, mode); |
4164020e SM |
44 | goto error; |
45 | } | |
46 | ||
0f5c5d5c | 47 | BT_CPPLOGI_SPEC(file->logger, "Opened file: {}", fmt::ptr(file->fp)); |
4164020e | 48 | |
85a25425 | 49 | if (fstat(fileno(file->fp.get()), &stat)) { |
0f5c5d5c | 50 | BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(file->logger, "Cannot get file information", ": path={}", |
a39d9817 | 51 | file->path); |
4164020e SM |
52 | goto error; |
53 | } | |
54 | ||
55 | file->size = stat.st_size; | |
0f5c5d5c | 56 | BT_CPPLOGI_SPEC(file->logger, "File is {} bytes", (intmax_t) file->size); |
4164020e | 57 | goto end; |
e98a2d6e PP |
58 | |
59 | error: | |
4164020e | 60 | ret = -1; |
e98a2d6e | 61 | |
e98a2d6e | 62 | end: |
4164020e | 63 | return ret; |
e98a2d6e | 64 | } |