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