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 | ||
4164020e SM |
21 | if (file->path) { |
22 | g_string_free(file->path, TRUE); | |
23 | } | |
24 | ||
afb0f12b | 25 | delete file; |
e98a2d6e PP |
26 | } |
27 | ||
5ff12b25 | 28 | void ctf_fs_file_deleter::operator()(ctf_fs_file * const file) noexcept |
e98a2d6e | 29 | { |
5ff12b25 SM |
30 | ctf_fs_file_destroy(file); |
31 | } | |
32 | ||
33 | ctf_fs_file::UP ctf_fs_file_create(const bt2c::Logger& parentLogger) | |
34 | { | |
35 | ctf_fs_file::UP file {new ctf_fs_file {parentLogger}}; | |
36 | ||
4164020e SM |
37 | file->path = g_string_new(NULL); |
38 | if (!file->path) { | |
39 | goto error; | |
40 | } | |
e98a2d6e | 41 | |
4164020e | 42 | goto end; |
e98a2d6e PP |
43 | |
44 | error: | |
5ff12b25 | 45 | file.reset(); |
e98a2d6e PP |
46 | |
47 | end: | |
4164020e | 48 | return file; |
e98a2d6e PP |
49 | } |
50 | ||
55314f2a | 51 | int ctf_fs_file_open(struct ctf_fs_file *file, const char *mode) |
e98a2d6e | 52 | { |
4164020e SM |
53 | int ret = 0; |
54 | struct stat stat; | |
55 | ||
0f5c5d5c | 56 | BT_CPPLOGI_SPEC(file->logger, "Opening file \"{}\" with mode \"{}\"", file->path->str, mode); |
85a25425 | 57 | file->fp.reset(fopen(file->path->str, mode)); |
4164020e | 58 | if (!file->fp) { |
0f5c5d5c SM |
59 | BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(file->logger, "Cannot open file", ": path={}, mode={}", |
60 | file->path->str, mode); | |
4164020e SM |
61 | goto error; |
62 | } | |
63 | ||
0f5c5d5c | 64 | BT_CPPLOGI_SPEC(file->logger, "Opened file: {}", fmt::ptr(file->fp)); |
4164020e | 65 | |
85a25425 | 66 | if (fstat(fileno(file->fp.get()), &stat)) { |
0f5c5d5c SM |
67 | BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(file->logger, "Cannot get file information", ": path={}", |
68 | file->path->str); | |
4164020e SM |
69 | goto error; |
70 | } | |
71 | ||
72 | file->size = stat.st_size; | |
0f5c5d5c | 73 | BT_CPPLOGI_SPEC(file->logger, "File is {} bytes", (intmax_t) file->size); |
4164020e | 74 | goto end; |
e98a2d6e PP |
75 | |
76 | error: | |
4164020e | 77 | ret = -1; |
e98a2d6e | 78 | |
e98a2d6e | 79 | end: |
4164020e | 80 | return ret; |
e98a2d6e | 81 | } |