Commit | Line | Data |
---|---|---|
e98a2d6e | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
e98a2d6e | 3 | * |
0235b0db | 4 | * Copyright 2016 Philippe Proulx <pproulx@efficios.com> |
e98a2d6e PP |
5 | */ |
6 | ||
27a14e13 SM |
7 | #define BT_CLOG_CFG (logCfg) |
8 | #define BT_LOG_TAG "PLUGIN/SRC.CTF.FS/FILE" | |
98903a3e | 9 | |
e98a2d6e PP |
10 | #include <stdio.h> |
11 | #include <sys/types.h> | |
12 | #include <sys/stat.h> | |
13 | #include <unistd.h> | |
14 | #include <glib.h> | |
087cd0f5 | 15 | #include "file.hpp" |
27a14e13 SM |
16 | #include "cpp-common/cfg-logging.hpp" |
17 | #include "cpp-common/cfg-logging-error-reporting.hpp" | |
e98a2d6e | 18 | |
e7a4393b | 19 | BT_HIDDEN |
55314f2a | 20 | int ctf_fs_file_open(struct ctf_fs_file *file, const char *mode) |
e98a2d6e | 21 | { |
4164020e SM |
22 | int ret = 0; |
23 | struct stat stat; | |
27a14e13 | 24 | const bt2_common::LogCfg& logCfg = file->logCfg; |
4164020e | 25 | |
31f55dc1 SM |
26 | BT_CLOGI("Opening file \"%s\" with mode \"%s\"", file->path.c_str(), mode); |
27 | file->fp.reset(fopen(file->path.c_str(), mode)); | |
4164020e | 28 | if (!file->fp) { |
31f55dc1 | 29 | BT_CLOGE_ERRNO_APPEND_CAUSE("Cannot open file", ": path=%s, mode=%s", file->path.c_str(), |
27a14e13 | 30 | mode); |
4164020e SM |
31 | goto error; |
32 | } | |
33 | ||
d42345a4 | 34 | BT_CLOGI("Opened file: %p", file->fp.get()); |
4164020e | 35 | |
d42345a4 | 36 | if (fstat(fileno(file->fp.get()), &stat)) { |
31f55dc1 | 37 | BT_CLOGE_ERRNO_APPEND_CAUSE("Cannot get file information", ": path=%s", file->path.c_str()); |
4164020e SM |
38 | goto error; |
39 | } | |
40 | ||
41 | file->size = stat.st_size; | |
27a14e13 | 42 | BT_CLOGI("File is %jd bytes", (intmax_t) file->size); |
4164020e | 43 | goto end; |
e98a2d6e PP |
44 | |
45 | error: | |
4164020e | 46 | ret = -1; |
e98a2d6e | 47 | |
e98a2d6e | 48 | end: |
4164020e | 49 | return ret; |
e98a2d6e | 50 | } |