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