e3f7d7783c1c872ff5b52b30c40df97ad9a85c43
[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 delete file;
22 }
23
24 void ctf_fs_file_deleter::operator()(ctf_fs_file * const file) noexcept
25 {
26 ctf_fs_file_destroy(file);
27 }
28
29 ctf_fs_file::UP ctf_fs_file_create(const bt2c::Logger& parentLogger)
30 {
31 return ctf_fs_file::UP {new ctf_fs_file {parentLogger}};
32 }
33
34 int ctf_fs_file_open(struct ctf_fs_file *file, const char *mode)
35 {
36 int ret = 0;
37 struct stat stat;
38
39 BT_CPPLOGI_SPEC(file->logger, "Opening file \"{}\" with mode \"{}\"", file->path, mode);
40 file->fp.reset(fopen(file->path.c_str(), mode));
41 if (!file->fp) {
42 BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(file->logger, "Cannot open file", ": path={}, mode={}",
43 file->path, mode);
44 goto error;
45 }
46
47 BT_CPPLOGI_SPEC(file->logger, "Opened file: {}", fmt::ptr(file->fp));
48
49 if (fstat(fileno(file->fp.get()), &stat)) {
50 BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(file->logger, "Cannot get file information", ": path={}",
51 file->path);
52 goto error;
53 }
54
55 file->size = stat.st_size;
56 BT_CPPLOGI_SPEC(file->logger, "File is {} bytes", (intmax_t) file->size);
57 goto end;
58
59 error:
60 ret = -1;
61
62 end:
63 return ret;
64 }
This page took 0.030316 seconds and 3 git commands to generate.