src.ctf.fs: make ctf_fs_file::fp a FileUP
[babeltrace.git] / src / plugins / ctf / fs-src / file.cpp
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
5 */
6
7 #include <glib.h>
8 #include <stdio.h>
9 #include <sys/stat.h>
10
11 #include "cpp-common/vendor/fmt/format.h"
12
13 #include "file.hpp"
14
15 void ctf_fs_file_destroy(struct ctf_fs_file *file)
16 {
17 if (!file) {
18 return;
19 }
20
21 if (file->path) {
22 g_string_free(file->path, TRUE);
23 }
24
25 delete file;
26 }
27
28 void ctf_fs_file_deleter::operator()(ctf_fs_file * const file) noexcept
29 {
30 ctf_fs_file_destroy(file);
31 }
32
33 ctf_fs_file::UP ctf_fs_file_create(const bt2c::Logger& parentLogger)
34 {
35 ctf_fs_file::UP file {new ctf_fs_file {parentLogger}};
36
37 file->path = g_string_new(NULL);
38 if (!file->path) {
39 goto error;
40 }
41
42 goto end;
43
44 error:
45 file.reset();
46
47 end:
48 return file;
49 }
50
51 int ctf_fs_file_open(struct ctf_fs_file *file, const char *mode)
52 {
53 int ret = 0;
54 struct stat stat;
55
56 BT_CPPLOGI_SPEC(file->logger, "Opening file \"{}\" with mode \"{}\"", file->path->str, mode);
57 file->fp.reset(fopen(file->path->str, mode));
58 if (!file->fp) {
59 BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(file->logger, "Cannot open file", ": path={}, mode={}",
60 file->path->str, mode);
61 goto error;
62 }
63
64 BT_CPPLOGI_SPEC(file->logger, "Opened file: {}", fmt::ptr(file->fp));
65
66 if (fstat(fileno(file->fp.get()), &stat)) {
67 BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(file->logger, "Cannot get file information", ": path={}",
68 file->path->str);
69 goto error;
70 }
71
72 file->size = stat.st_size;
73 BT_CPPLOGI_SPEC(file->logger, "File is {} bytes", (intmax_t) file->size);
74 goto end;
75
76 error:
77 ret = -1;
78
79 end:
80 return ret;
81 }
This page took 0.031153 seconds and 4 git commands to generate.