Re-format new 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 #define BT_COMP_LOG_SELF_COMP (file->self_comp)
8 #define BT_LOG_OUTPUT_LEVEL (file->log_level)
9 #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/FILE"
10 #include "logging/comp-logging.h"
11
12 #include <stdio.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <unistd.h>
16 #include <glib.h>
17 #include "file.hpp"
18
19 BT_HIDDEN
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 BT_HIDDEN
43 struct ctf_fs_file *ctf_fs_file_create(bt_logging_level log_level, bt_self_component *self_comp)
44 {
45 struct ctf_fs_file *file = g_new0(struct ctf_fs_file, 1);
46
47 if (!file) {
48 goto error;
49 }
50
51 file->log_level = log_level;
52 file->self_comp = self_comp;
53 file->path = g_string_new(NULL);
54 if (!file->path) {
55 goto error;
56 }
57
58 goto end;
59
60 error:
61 ctf_fs_file_destroy(file);
62 file = NULL;
63
64 end:
65 return file;
66 }
67
68 BT_HIDDEN
69 int ctf_fs_file_open(struct ctf_fs_file *file, const char *mode)
70 {
71 int ret = 0;
72 struct stat stat;
73
74 BT_COMP_LOGI("Opening file \"%s\" with mode \"%s\"", file->path->str, mode);
75 file->fp = fopen(file->path->str, mode);
76 if (!file->fp) {
77 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(file->self_comp, "Cannot open file", ": path=%s, mode=%s",
78 file->path->str, mode);
79 goto error;
80 }
81
82 BT_COMP_LOGI("Opened file: %p", file->fp);
83
84 if (fstat(fileno(file->fp), &stat)) {
85 BT_COMP_LOGE_APPEND_CAUSE_ERRNO(file->self_comp, "Cannot get file information", ": path=%s",
86 file->path->str);
87 goto error;
88 }
89
90 file->size = stat.st_size;
91 BT_COMP_LOGI("File is %jd bytes", (intmax_t) file->size);
92 goto end;
93
94 error:
95 ret = -1;
96
97 if (file->fp) {
98 if (fclose(file->fp)) {
99 BT_COMP_LOGE("Cannot close file \"%s\": %s", file->path->str, strerror(errno));
100 }
101 }
102
103 end:
104 return ret;
105 }
This page took 0.031911 seconds and 4 git commands to generate.