libcamera v0.1.0+127-8e215127-dirty (2023-12-02T01:06:12+00:00)
Supporting cameras in Linux since 2019
file.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2/*
3 * Copyright (C) 2020, Google Inc.
4 *
5 * file.h - File I/O operations
6 */
7
8#pragma once
9
10#include <sys/types.h>
11
12#include <map>
13#include <string>
14
15#include <libcamera/base/private.h>
16
19#include <libcamera/base/span.h>
21
22namespace libcamera {
23
24class File
25{
26public:
27 enum class MapFlag {
28 NoOption = 0,
29 Private = (1 << 0),
30 };
31
33
34 enum class OpenModeFlag {
35 NotOpen = 0,
36 ReadOnly = (1 << 0),
37 WriteOnly = (1 << 1),
39 };
40
42
43 File(const std::string &name);
44 File();
45 ~File();
46
47 const std::string &fileName() const { return name_; }
48 void setFileName(const std::string &name);
49 bool exists() const;
50
51 bool open(OpenMode mode);
52 bool isOpen() const { return fd_.isValid(); }
53 OpenMode openMode() const { return mode_; }
54 void close();
55
56 int error() const { return error_; }
57 ssize_t size() const;
58
59 off_t pos() const;
60 off_t seek(off_t pos);
61
62 ssize_t read(const Span<uint8_t> &data);
63 ssize_t write(const Span<const uint8_t> &data);
64
65 Span<uint8_t> map(off_t offset = 0, ssize_t size = -1,
67 bool unmap(uint8_t *addr);
68
69 static bool exists(const std::string &name);
70
71private:
73
74 void unmapAll();
75
76 std::string name_;
77 UniqueFD fd_;
78 OpenMode mode_;
79
80 int error_;
81 std::map<void *, size_t> maps_;
82};
83
86
87} /* namespace libcamera */
Utilities to help constructing class interfaces.
#define LIBCAMERA_DISABLE_COPY(klass)
Disable copy construction and assignment of the klass.
Interface for I/O operations on files.
Definition: file.h:25
MapFlag
Flags for the File::map() function.
Definition: file.h:27
@ NoOption
No option (used as default value)
@ Private
The memory region is mapped as private, changes are not reflected in the file constents.
int error() const
Retrieve the file error status.
Definition: file.h:56
const std::string & fileName() const
Retrieve the file name.
Definition: file.h:47
bool exists() const
Check if the file specified by fileName() exists.
Definition: file.cpp:152
bool open(OpenMode mode)
Open the file in the given mode.
Definition: file.cpp:173
ssize_t write(const Span< const uint8_t > &data)
Write data to the file.
Definition: file.cpp:342
void close()
Close the file.
Definition: file.cpp:214
OpenModeFlag
Mode in which a file is opened.
Definition: file.h:34
@ ReadOnly
The file is open for reading.
@ WriteOnly
The file is open for writing.
@ ReadWrite
The file is open for reading and writing.
@ NotOpen
The file is not open.
bool isOpen() const
Check if the file is open.
Definition: file.h:52
~File()
Destroy a File instance.
Definition: file.cpp:108
File()
Construct a File without an associated name.
Definition: file.cpp:97
Flags< OpenModeFlag > OpenMode
A bitwise combination of File::OpenModeFlag values.
Definition: file.h:41
OpenMode openMode() const
Retrieve the file open mode.
Definition: file.h:53
Flags< MapFlag > MapFlags
A bitwise combination of File::MapFlag values.
Definition: file.h:32
ssize_t read(const Span< uint8_t > &data)
Read data from the file.
Definition: file.cpp:304
off_t seek(off_t pos)
Set the read or write position.
Definition: file.cpp:277
Span< uint8_t > map(off_t offset=0, ssize_t size=-1, MapFlags flags=MapFlag::NoOption)
Map a region of the file in the process memory.
Definition: file.cpp:387
bool unmap(uint8_t *addr)
Unmap a region mapped with map()
Definition: file.cpp:434
ssize_t size() const
Retrieve the file size.
Definition: file.cpp:243
off_t pos() const
Return current read or write position.
Definition: file.cpp:263
void setFileName(const std::string &name)
Set the name of the file.
Definition: file.cpp:129
Type-safe container for enum-based bitfields.
Definition: flags.h:16
unique_ptr-like wrapper for a file descriptor
Definition: unique_fd.h:18
bool isValid() const
Check if the UniqueFD owns a valid file descriptor.
Definition: unique_fd.h:61
Enum-based bit fields.
#define LIBCAMERA_FLAGS_ENABLE_OPERATORS(_enum)
Enable bitwise operations on the enum enumeration.
Top-level libcamera namespace.
Definition: backtrace.h:17
File descriptor wrapper that owns a file descriptor.