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 | ||
23 | #include <stdio.h> | |
24 | #include <sys/types.h> | |
25 | #include <sys/stat.h> | |
26 | #include <unistd.h> | |
27 | #include <glib.h> | |
56a1cced | 28 | #include "file.h" |
e98a2d6e | 29 | |
55314f2a JG |
30 | #define BT_LOG_TAG "PLUGIN-CTF-FS-FILE-SRC" |
31 | #include "logging.h" | |
32 | ||
e7a4393b | 33 | BT_HIDDEN |
e98a2d6e PP |
34 | void ctf_fs_file_destroy(struct ctf_fs_file *file) |
35 | { | |
e98a2d6e PP |
36 | if (!file) { |
37 | return; | |
38 | } | |
39 | ||
40 | if (file->fp) { | |
55314f2a | 41 | BT_LOGD("Closing file \"%s\" (%p)", |
dcbaa1ca | 42 | file->path ? file->path->str : NULL, file->fp); |
e98a2d6e PP |
43 | |
44 | if (fclose(file->fp)) { | |
55314f2a | 45 | BT_LOGE("Cannot close file \"%s\": %s", |
dcbaa1ca JG |
46 | file->path ? file->path->str : "NULL", |
47 | strerror(errno)); | |
e98a2d6e PP |
48 | } |
49 | } | |
50 | ||
51 | if (file->path) { | |
52 | g_string_free(file->path, TRUE); | |
53 | } | |
54 | ||
55 | g_free(file); | |
56 | } | |
57 | ||
e7a4393b | 58 | BT_HIDDEN |
55314f2a | 59 | struct ctf_fs_file *ctf_fs_file_create(void) |
e98a2d6e PP |
60 | { |
61 | struct ctf_fs_file *file = g_new0(struct ctf_fs_file, 1); | |
62 | ||
63 | if (!file) { | |
64 | goto error; | |
65 | } | |
66 | ||
e98a2d6e PP |
67 | file->path = g_string_new(NULL); |
68 | if (!file->path) { | |
69 | goto error; | |
70 | } | |
71 | ||
72 | goto end; | |
73 | ||
74 | error: | |
75 | ctf_fs_file_destroy(file); | |
76 | file = NULL; | |
77 | ||
78 | end: | |
79 | return file; | |
80 | } | |
81 | ||
e7a4393b | 82 | BT_HIDDEN |
55314f2a | 83 | int ctf_fs_file_open(struct ctf_fs_file *file, const char *mode) |
e98a2d6e PP |
84 | { |
85 | int ret = 0; | |
86 | struct stat stat; | |
87 | ||
55314f2a | 88 | BT_LOGD("Opening file \"%s\" with mode \"%s\"", file->path->str, mode); |
e98a2d6e PP |
89 | file->fp = fopen(file->path->str, mode); |
90 | if (!file->fp) { | |
55314f2a | 91 | BT_LOGE("Cannot open file \"%s\" with mode \"%s\": %s", |
e98a2d6e PP |
92 | file->path->str, mode, strerror(errno)); |
93 | goto error; | |
94 | } | |
95 | ||
55314f2a | 96 | BT_LOGD("Opened file: %p", file->fp); |
e98a2d6e PP |
97 | |
98 | if (fstat(fileno(file->fp), &stat)) { | |
e07ab7c8 | 99 | BT_LOGE("Cannot get file information: %s", strerror(errno)); |
e98a2d6e PP |
100 | goto error; |
101 | } | |
102 | ||
103 | file->size = stat.st_size; | |
1974687e | 104 | BT_LOGD("File is %jd bytes", (intmax_t) file->size); |
e98a2d6e PP |
105 | goto end; |
106 | ||
107 | error: | |
108 | ret = -1; | |
109 | ||
110 | if (file->fp) { | |
111 | if (fclose(file->fp)) { | |
55314f2a | 112 | BT_LOGE("Cannot close file \"%s\": %s", file->path->str, |
e98a2d6e PP |
113 | strerror(errno)); |
114 | } | |
115 | } | |
116 | ||
117 | end: | |
118 | return ret; | |
119 | } |