Commit | Line | Data |
---|---|---|
e98a2d6e PP |
1 | /* |
2 | * Copyright 2016 - Philippe Proulx <pproulx@efficios.com> | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 | * of this software and associated documentation files (the "Software"), to deal | |
6 | * in the Software without restriction, including without limitation the rights | |
7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 | * copies of the Software, and to permit persons to whom the Software is | |
9 | * furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice shall be included in | |
12 | * all copies or substantial portions of the Software. | |
13 | * | |
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
20 | * SOFTWARE. | |
21 | */ | |
22 | ||
4c65a157 | 23 | #define BT_COMP_LOG_SELF_COMP (file->self_comp) |
98903a3e PP |
24 | #define BT_LOG_OUTPUT_LEVEL (file->log_level) |
25 | #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/FILE" | |
d9c39b0a | 26 | #include "logging/comp-logging.h" |
98903a3e | 27 | |
e98a2d6e PP |
28 | #include <stdio.h> |
29 | #include <sys/types.h> | |
30 | #include <sys/stat.h> | |
31 | #include <unistd.h> | |
32 | #include <glib.h> | |
56a1cced | 33 | #include "file.h" |
e98a2d6e | 34 | |
e7a4393b | 35 | BT_HIDDEN |
e98a2d6e PP |
36 | void ctf_fs_file_destroy(struct ctf_fs_file *file) |
37 | { | |
e98a2d6e PP |
38 | if (!file) { |
39 | return; | |
40 | } | |
41 | ||
42 | if (file->fp) { | |
4c65a157 | 43 | BT_COMP_LOGD("Closing file \"%s\" (%p)", |
dcbaa1ca | 44 | file->path ? file->path->str : NULL, file->fp); |
e98a2d6e PP |
45 | |
46 | if (fclose(file->fp)) { | |
4c65a157 | 47 | BT_COMP_LOGE("Cannot close file \"%s\": %s", |
dcbaa1ca JG |
48 | file->path ? file->path->str : "NULL", |
49 | strerror(errno)); | |
e98a2d6e PP |
50 | } |
51 | } | |
52 | ||
53 | if (file->path) { | |
54 | g_string_free(file->path, TRUE); | |
55 | } | |
56 | ||
57 | g_free(file); | |
58 | } | |
59 | ||
e7a4393b | 60 | BT_HIDDEN |
4c65a157 PP |
61 | struct ctf_fs_file *ctf_fs_file_create(bt_logging_level log_level, |
62 | bt_self_component *self_comp) | |
e98a2d6e PP |
63 | { |
64 | struct ctf_fs_file *file = g_new0(struct ctf_fs_file, 1); | |
65 | ||
66 | if (!file) { | |
67 | goto error; | |
68 | } | |
69 | ||
98903a3e | 70 | file->log_level = log_level; |
4c65a157 | 71 | file->self_comp = self_comp; |
e98a2d6e PP |
72 | file->path = g_string_new(NULL); |
73 | if (!file->path) { | |
74 | goto error; | |
75 | } | |
76 | ||
77 | goto end; | |
78 | ||
79 | error: | |
80 | ctf_fs_file_destroy(file); | |
81 | file = NULL; | |
82 | ||
83 | end: | |
84 | return file; | |
85 | } | |
86 | ||
e7a4393b | 87 | BT_HIDDEN |
55314f2a | 88 | int ctf_fs_file_open(struct ctf_fs_file *file, const char *mode) |
e98a2d6e PP |
89 | { |
90 | int ret = 0; | |
91 | struct stat stat; | |
92 | ||
4c65a157 | 93 | BT_COMP_LOGI("Opening file \"%s\" with mode \"%s\"", file->path->str, mode); |
e98a2d6e PP |
94 | file->fp = fopen(file->path->str, mode); |
95 | if (!file->fp) { | |
b30284d1 SM |
96 | BT_COMP_LOGE_APPEND_CAUSE_ERRNO(file->self_comp, |
97 | "Cannot open file", ": path=%s, mode=%s", | |
98 | file->path->str, mode); | |
e98a2d6e PP |
99 | goto error; |
100 | } | |
101 | ||
4c65a157 | 102 | BT_COMP_LOGI("Opened file: %p", file->fp); |
e98a2d6e PP |
103 | |
104 | if (fstat(fileno(file->fp), &stat)) { | |
b30284d1 SM |
105 | BT_COMP_LOGE_APPEND_CAUSE_ERRNO(file->self_comp, |
106 | "Cannot get file information", | |
107 | ": path=%s", file->path->str); | |
e98a2d6e PP |
108 | goto error; |
109 | } | |
110 | ||
111 | file->size = stat.st_size; | |
4c65a157 | 112 | BT_COMP_LOGI("File is %jd bytes", (intmax_t) file->size); |
e98a2d6e PP |
113 | goto end; |
114 | ||
115 | error: | |
116 | ret = -1; | |
117 | ||
118 | if (file->fp) { | |
119 | if (fclose(file->fp)) { | |
4c65a157 | 120 | BT_COMP_LOGE("Cannot close file \"%s\": %s", file->path->str, |
e98a2d6e PP |
121 | strerror(errno)); |
122 | } | |
123 | } | |
124 | ||
125 | end: | |
126 | return ret; | |
127 | } |