61a724b01fb18a8953b3d5e73692812fe84b76e8
[babeltrace.git] / plugins / ctf / fs-src / file.c
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>
28
29 #define PRINT_ERR_STREAM ctf_fs->error_fp
30 #define PRINT_PREFIX "ctf-fs-file"
31 #define PRINT_DBG_CHECK ctf_fs_debug
32 #include "../print.h"
33
34 #include "file.h"
35
36 BT_HIDDEN
37 void ctf_fs_file_destroy(struct ctf_fs_file *file)
38 {
39 struct ctf_fs_component *ctf_fs;;
40
41 if (!file) {
42 return;
43 }
44
45 ctf_fs = file->ctf_fs;
46
47 if (file->fp) {
48 PDBG("Closing file \"%s\" (%p)\n",
49 file->path ? file->path->str : NULL, file->fp);
50
51 if (fclose(file->fp)) {
52 PERR("Cannot close file \"%s\": %s\n",
53 file->path ? file->path->str : "NULL",
54 strerror(errno));
55 }
56 }
57
58 if (file->path) {
59 g_string_free(file->path, TRUE);
60 }
61
62 g_free(file);
63 }
64
65 BT_HIDDEN
66 struct ctf_fs_file *ctf_fs_file_create(struct ctf_fs_component *ctf_fs)
67 {
68 struct ctf_fs_file *file = g_new0(struct ctf_fs_file, 1);
69
70 if (!file) {
71 goto error;
72 }
73
74 file->ctf_fs = ctf_fs;
75 file->path = g_string_new(NULL);
76 if (!file->path) {
77 goto error;
78 }
79
80 goto end;
81
82 error:
83 ctf_fs_file_destroy(file);
84 file = NULL;
85
86 end:
87 return file;
88 }
89
90 BT_HIDDEN
91 int ctf_fs_file_open(struct ctf_fs_component *ctf_fs, struct ctf_fs_file *file,
92 const char *mode)
93 {
94 int ret = 0;
95 struct stat stat;
96
97 PDBG("Opening file \"%s\" with mode \"%s\"\n", file->path->str, mode);
98 file->fp = fopen(file->path->str, mode);
99 if (!file->fp) {
100 PERR("Cannot open file \"%s\" with mode \"%s\": %s\n",
101 file->path->str, mode, strerror(errno));
102 goto error;
103 }
104
105 PDBG("Opened file: %p\n", file->fp);
106
107 if (fstat(fileno(file->fp), &stat)) {
108 PERR("Cannot get file informations: %s\n", strerror(errno));
109 goto error;
110 }
111
112 file->size = stat.st_size;
113 PDBG(" File is %zu bytes\n", file->size);
114 goto end;
115
116 error:
117 ret = -1;
118
119 if (file->fp) {
120 if (fclose(file->fp)) {
121 PERR("Cannot close file \"%s\": %s\n", file->path->str,
122 strerror(errno));
123 }
124 }
125
126 end:
127 return ret;
128 }
This page took 0.038041 seconds and 3 git commands to generate.