2 * SPDX-License-Identifier: MIT
4 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
11 #define BT_COMP_LOG_SELF_COMP (file->self_comp)
12 #define BT_LOG_OUTPUT_LEVEL (file->log_level)
13 #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/FILE"
14 #include "logging/comp-logging.h"
19 void ctf_fs_file_destroy(struct ctf_fs_file
*file
)
26 BT_COMP_LOGD("Closing file \"%s\" (%p)", file
->path
? file
->path
->str
: NULL
, file
->fp
);
28 if (fclose(file
->fp
)) {
29 BT_COMP_LOGE("Cannot close file \"%s\": %s", file
->path
? file
->path
->str
: "NULL",
35 g_string_free(file
->path
, TRUE
);
41 struct ctf_fs_file
*ctf_fs_file_create(bt_logging_level log_level
, bt_self_component
*self_comp
)
43 struct ctf_fs_file
*file
= g_new0(struct ctf_fs_file
, 1);
49 file
->log_level
= log_level
;
50 file
->self_comp
= self_comp
;
51 file
->path
= g_string_new(NULL
);
59 ctf_fs_file_destroy(file
);
66 int ctf_fs_file_open(struct ctf_fs_file
*file
, const char *mode
)
71 BT_COMP_LOGI("Opening file \"%s\" with mode \"%s\"", file
->path
->str
, mode
);
72 file
->fp
= fopen(file
->path
->str
, mode
);
74 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(file
->self_comp
, "Cannot open file", ": path=%s, mode=%s",
75 file
->path
->str
, mode
);
79 BT_COMP_LOGI("Opened file: %p", file
->fp
);
81 if (fstat(fileno(file
->fp
), &stat
)) {
82 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(file
->self_comp
, "Cannot get file information", ": path=%s",
87 file
->size
= stat
.st_size
;
88 BT_COMP_LOGI("File is %jd bytes", (intmax_t) file
->size
);
95 if (fclose(file
->fp
)) {
96 BT_COMP_LOGE("Cannot close file \"%s\": %s", file
->path
->str
, strerror(errno
));