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 | ||
4c65a157 | 7 | #define BT_COMP_LOG_SELF_COMP (file->self_comp) |
4164020e SM |
8 | #define BT_LOG_OUTPUT_LEVEL (file->log_level) |
9 | #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/FILE" | |
d9c39b0a | 10 | #include "logging/comp-logging.h" |
98903a3e | 11 | |
e98a2d6e PP |
12 | #include <stdio.h> |
13 | #include <sys/types.h> | |
14 | #include <sys/stat.h> | |
15 | #include <unistd.h> | |
16 | #include <glib.h> | |
087cd0f5 | 17 | #include "file.hpp" |
e98a2d6e PP |
18 | |
19 | void ctf_fs_file_destroy(struct ctf_fs_file *file) | |
20 | { | |
4164020e SM |
21 | if (!file) { |
22 | return; | |
23 | } | |
24 | ||
25 | if (file->fp) { | |
26 | BT_COMP_LOGD("Closing file \"%s\" (%p)", file->path ? file->path->str : NULL, file->fp); | |
27 | ||
28 | if (fclose(file->fp)) { | |
29 | BT_COMP_LOGE("Cannot close file \"%s\": %s", file->path ? file->path->str : "NULL", | |
30 | strerror(errno)); | |
31 | } | |
32 | } | |
33 | ||
34 | if (file->path) { | |
35 | g_string_free(file->path, TRUE); | |
36 | } | |
37 | ||
38 | g_free(file); | |
e98a2d6e PP |
39 | } |
40 | ||
4164020e | 41 | struct ctf_fs_file *ctf_fs_file_create(bt_logging_level log_level, bt_self_component *self_comp) |
e98a2d6e | 42 | { |
4164020e | 43 | struct ctf_fs_file *file = g_new0(struct ctf_fs_file, 1); |
e98a2d6e | 44 | |
4164020e SM |
45 | if (!file) { |
46 | goto error; | |
47 | } | |
e98a2d6e | 48 | |
4164020e SM |
49 | file->log_level = log_level; |
50 | file->self_comp = self_comp; | |
51 | file->path = g_string_new(NULL); | |
52 | if (!file->path) { | |
53 | goto error; | |
54 | } | |
e98a2d6e | 55 | |
4164020e | 56 | goto end; |
e98a2d6e PP |
57 | |
58 | error: | |
4164020e SM |
59 | ctf_fs_file_destroy(file); |
60 | file = NULL; | |
e98a2d6e PP |
61 | |
62 | end: | |
4164020e | 63 | return file; |
e98a2d6e PP |
64 | } |
65 | ||
55314f2a | 66 | int ctf_fs_file_open(struct ctf_fs_file *file, const char *mode) |
e98a2d6e | 67 | { |
4164020e SM |
68 | int ret = 0; |
69 | struct stat stat; | |
70 | ||
71 | BT_COMP_LOGI("Opening file \"%s\" with mode \"%s\"", file->path->str, mode); | |
72 | file->fp = fopen(file->path->str, mode); | |
73 | if (!file->fp) { | |
74 | BT_COMP_LOGE_APPEND_CAUSE_ERRNO(file->self_comp, "Cannot open file", ": path=%s, mode=%s", | |
75 | file->path->str, mode); | |
76 | goto error; | |
77 | } | |
78 | ||
79 | BT_COMP_LOGI("Opened file: %p", file->fp); | |
80 | ||
81 | if (fstat(fileno(file->fp), &stat)) { | |
82 | BT_COMP_LOGE_APPEND_CAUSE_ERRNO(file->self_comp, "Cannot get file information", ": path=%s", | |
83 | file->path->str); | |
84 | goto error; | |
85 | } | |
86 | ||
87 | file->size = stat.st_size; | |
88 | BT_COMP_LOGI("File is %jd bytes", (intmax_t) file->size); | |
89 | goto end; | |
e98a2d6e PP |
90 | |
91 | error: | |
4164020e | 92 | ret = -1; |
e98a2d6e | 93 | |
4164020e SM |
94 | if (file->fp) { |
95 | if (fclose(file->fp)) { | |
96 | BT_COMP_LOGE("Cannot close file \"%s\": %s", file->path->str, strerror(errno)); | |
97 | } | |
98 | } | |
e98a2d6e PP |
99 | |
100 | end: | |
4164020e | 101 | return ret; |
e98a2d6e | 102 | } |