libcamera  v0.0.0
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 #ifndef __LIBCAMERA_INTERNAL_FILE_H__
8 #define __LIBCAMERA_INTERNAL_FILE_H__
9 
10 #include <map>
11 #include <string>
12 #include <sys/types.h>
13 
14 #include <libcamera/span.h>
15 
16 namespace libcamera {
17 
18 class File
19 {
20 public:
21  enum MapFlag {
23  MapPrivate = (1 << 0),
24  };
25 
26  enum OpenMode {
27  NotOpen = 0,
28  ReadOnly = (1 << 0),
29  WriteOnly = (1 << 1),
31  };
32 
33  File(const std::string &name);
34  File();
35  ~File();
36 
37  File(const File &) = delete;
38  File &operator=(const File &) = delete;
39 
40  const std::string &fileName() const { return name_; }
41  void setFileName(const std::string &name);
42  bool exists() const;
43 
44  bool open(OpenMode mode);
45  bool isOpen() const { return fd_ != -1; }
46  OpenMode openMode() const { return mode_; }
47  void close();
48 
49  int error() const { return error_; }
50  ssize_t size() const;
51 
52  Span<uint8_t> map(off_t offset = 0, ssize_t size = -1,
53  MapFlag flags = MapNoOption);
54  bool unmap(uint8_t *addr);
55 
56  static bool exists(const std::string &name);
57 
58 private:
59  void unmapAll();
60 
61  std::string name_;
62  int fd_;
63  OpenMode mode_;
64 
65  int error_;
66  std::map<void *, size_t> maps_;
67 };
68 
69 } /* namespace libcamera */
70 
71 #endif /* __LIBCAMERA_INTERNAL_FILE_H__ */
Interface for I/O operations on files.
Definition: file.h:19
MapFlag
Flags for the File::map() function.
Definition: file.h:21
@ MapNoOption
No option (used as default value)
Definition: file.h:22
@ MapPrivate
The memory region is mapped as private, changes are not reflected in the file constents.
Definition: file.h:23
int error() const
Retrieve the file error status.
Definition: file.h:49
const std::string & fileName() const
Retrieve the file name.
Definition: file.h:40
bool exists() const
Check if the file specified by fileName() exists.
Definition: file.cpp:141
bool open(OpenMode mode)
Open the file in the given mode.
Definition: file.cpp:158
void close()
Close the file.
Definition: file.cpp:197
Span< uint8_t > map(off_t offset=0, ssize_t size=-1, MapFlag flags=MapNoOption)
Map a region of the file in the process memory.
Definition: file.cpp:262
bool isOpen() const
Check if the file is open.
Definition: file.h:45
~File()
Destroy a File instance.
Definition: file.cpp:97
File()
Construct a File without an associated name.
Definition: file.cpp:86
OpenMode
Mode in which a file is opened.
Definition: file.h:26
@ WriteOnly
The file is open for writing.
Definition: file.h:29
@ ReadWrite
The file is open for reading and writing.
Definition: file.h:30
@ NotOpen
The file is not open.
Definition: file.h:27
@ ReadOnly
The file is open for reading.
Definition: file.h:28
OpenMode openMode() const
Retrieve the file open mode.
Definition: file.h:46
bool unmap(uint8_t *addr)
Unmap a region mapped with map()
Definition: file.cpp:309
ssize_t size() const
Retrieve the file size.
Definition: file.cpp:227
void setFileName(const std::string &name)
Set the name of the file.
Definition: file.cpp:118