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