src.ctf.fs: remove ctf_fs_file_destroy
[babeltrace.git] / src / plugins / ctf / fs-src / file.cpp
CommitLineData
e98a2d6e 1/*
0235b0db 2 * SPDX-License-Identifier: MIT
e98a2d6e 3 *
0235b0db 4 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
e98a2d6e
PP
5 */
6
c802cacb
SM
7#include <glib.h>
8#include <stdio.h>
9#include <sys/stat.h>
c802cacb 10
bf3ab6f6 11#include "cpp-common/bt2s/make-unique.hpp"
0f5c5d5c 12#include "cpp-common/vendor/fmt/format.h"
98903a3e 13
087cd0f5 14#include "file.hpp"
e98a2d6e 15
5ff12b25
SM
16ctf_fs_file::UP ctf_fs_file_create(const bt2c::Logger& parentLogger)
17{
bf3ab6f6 18 return bt2s::make_unique<ctf_fs_file>(parentLogger);
e98a2d6e
PP
19}
20
55314f2a 21int ctf_fs_file_open(struct ctf_fs_file *file, const char *mode)
e98a2d6e 22{
4164020e
SM
23 int ret = 0;
24 struct stat stat;
25
a39d9817
SM
26 BT_CPPLOGI_SPEC(file->logger, "Opening file \"{}\" with mode \"{}\"", file->path, mode);
27 file->fp.reset(fopen(file->path.c_str(), mode));
4164020e 28 if (!file->fp) {
0f5c5d5c 29 BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(file->logger, "Cannot open file", ": path={}, mode={}",
a39d9817 30 file->path, mode);
4164020e
SM
31 goto error;
32 }
33
0f5c5d5c 34 BT_CPPLOGI_SPEC(file->logger, "Opened file: {}", fmt::ptr(file->fp));
4164020e 35
85a25425 36 if (fstat(fileno(file->fp.get()), &stat)) {
0f5c5d5c 37 BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(file->logger, "Cannot get file information", ": path={}",
a39d9817 38 file->path);
4164020e
SM
39 goto error;
40 }
41
42 file->size = stat.st_size;
0f5c5d5c 43 BT_CPPLOGI_SPEC(file->logger, "File is {} bytes", (intmax_t) file->size);
4164020e 44 goto end;
e98a2d6e
PP
45
46error:
4164020e 47 ret = -1;
e98a2d6e 48
e98a2d6e 49end:
4164020e 50 return ret;
e98a2d6e 51}
This page took 0.113761 seconds and 4 git commands to generate.