file.h
Go to the documentation of this file.
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2020, Google Inc.
*
* file.h - File I/O operations
*/
#ifndef __LIBCAMERA_INTERNAL_FILE_H__
#define __LIBCAMERA_INTERNAL_FILE_H__
#include <map>
#include <string>
#include <sys/types.h>
#include <libcamera/span.h>
namespace libcamera {
class File
{
public:
enum MapFlag {
MapNoOption = 0,
MapPrivate = (1 << 0),
};
enum OpenMode {
NotOpen = 0,
ReadOnly = (1 << 0),
WriteOnly = (1 << 1),
ReadWrite = ReadOnly | WriteOnly,
};
File(const std::string &name);
File();
~File();
File(const File &) = delete;
File &operator=(const File &) = delete;
const std::string &fileName() const { return name_; }
void setFileName(const std::string &name);
bool exists() const;
bool open(OpenMode mode);
bool isOpen() const { return fd_ != -1; }
OpenMode openMode() const { return mode_; }
void close();
int error() const { return error_; }
ssize_t size() const;
Span<uint8_t> map(off_t offset = 0, ssize_t size = -1,
MapFlag flags = MapNoOption);
bool unmap(uint8_t *addr);
static bool exists(const std::string &name);
private:
void unmapAll();
std::string name_;
int fd_;
OpenMode mode_;
int error_;
std::map<void *, size_t> maps_;
};
} /* namespace libcamera */
#endif /* __LIBCAMERA_INTERNAL_FILE_H__ */
libcamera::File
Interface for I/O operations on files.
Definition: file.h:19
libcamera::File::MapFlag
MapFlag
Flags for the File::map() function.
Definition: file.h:21
libcamera::File::MapNoOption
@ MapNoOption
No option (used as default value)
Definition: file.h:22
libcamera::File::MapPrivate
@ MapPrivate
The memory region is mapped as private, changes are not reflected in the file constents.
Definition: file.h:23
libcamera::File::error
int error() const
Retrieve the file error status.
Definition: file.h:49
libcamera::File::fileName
const std::string & fileName() const
Retrieve the file name.
Definition: file.h:40
libcamera::File::exists
bool exists() const
Check if the file specified by fileName() exists.
Definition: file.cpp:141
libcamera::File::open
bool open(OpenMode mode)
Open the file in the given mode.
Definition: file.cpp:158
libcamera::File::close
void close()
Close the file.
Definition: file.cpp:197
libcamera::File::map
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
libcamera::File::isOpen
bool isOpen() const
Check if the file is open.
Definition: file.h:45
libcamera::File::~File
~File()
Destroy a File instance.
Definition: file.cpp:97
libcamera::File::File
File()
Construct a File without an associated name.
Definition: file.cpp:86
libcamera::File::OpenMode
OpenMode
Mode in which a file is opened.
Definition: file.h:26
libcamera::File::WriteOnly
@ WriteOnly
The file is open for writing.
Definition: file.h:29
libcamera::File::ReadWrite
@ ReadWrite
The file is open for reading and writing.
Definition: file.h:30
libcamera::File::NotOpen
@ NotOpen
The file is not open.
Definition: file.h:27
libcamera::File::ReadOnly
@ ReadOnly
The file is open for reading.
Definition: file.h:28
libcamera::File::openMode
OpenMode openMode() const
Retrieve the file open mode.
Definition: file.h:46
libcamera::File::unmap
bool unmap(uint8_t *addr)
Unmap a region mapped with map()
Definition: file.cpp:309
libcamera::File::size
ssize_t size() const
Retrieve the file size.
Definition: file.cpp:227
libcamera::File::setFileName
void setFileName(const std::string &name)
Set the name of the file.
Definition: file.cpp:118