Sort includes in C++ files
[babeltrace.git] / src / plugins / ctf / fs-src / file.cpp
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
5 */
6
7 #include <glib.h>
8 #include <stdio.h>
9 #include <sys/stat.h>
10 #include <sys/types.h>
11 #include <unistd.h>
12
13 #define BT_COMP_LOG_SELF_COMP (file->self_comp)
14 #define BT_LOG_OUTPUT_LEVEL (file->log_level)
15 #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/FILE"
16 #include "logging/comp-logging.h"
17
18 #include "file.hpp"
19
20 void ctf_fs_file_destroy(struct ctf_fs_file *file)
21 {
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);
40 }
41
42 struct ctf_fs_file *ctf_fs_file_create(bt_logging_level log_level, bt_self_component *self_comp)
43 {
44 struct ctf_fs_file *file = g_new0(struct ctf_fs_file, 1);
45
46 if (!file) {
47 goto error;
48 }
49
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 }
56
57 goto end;
58
59 error:
60 ctf_fs_file_destroy(file);
61 file = NULL;
62
63 end:
64 return file;
65 }
66
67 int ctf_fs_file_open(struct ctf_fs_file *file, const char *mode)
68 {
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;
91
92 error:
93 ret = -1;
94
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 }
100
101 end:
102 return ret;
103 }
This page took 0.033848 seconds and 4 git commands to generate.