Loading...
Searching...
No Matches
file.h
Go to the documentation of this file.
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2020, Google Inc.
*
* File I/O operations
*/
#pragma once
#include <map>
#include <stdint.h>
#include <string>
#include <sys/types.h>
#include <libcamera/base/private.h>
#include <libcamera/base/class.h>
#include <libcamera/base/flags.h>
#include <libcamera/base/span.h>
#include <libcamera/base/unique_fd.h>
namespace libcamera {
class File
{
public:
enum class MapFlag {
NoOption = 0,
Private = (1 << 0),
};
using MapFlags = Flags<MapFlag>;
enum class OpenModeFlag {
NotOpen = 0,
ReadOnly = (1 << 0),
WriteOnly = (1 << 1),
ReadWrite = ReadOnly | WriteOnly,
};
using OpenMode = Flags<OpenModeFlag>;
File(const std::string &name);
File();
~File();
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_.isValid(); }
OpenMode openMode() const { return mode_; }
void close();
int error() const { return error_; }
ssize_t size() const;
off_t pos() const;
off_t seek(off_t pos);
ssize_t read(const Span<uint8_t> &data);
ssize_t write(const Span<const uint8_t> &data);
Span<uint8_t> map(off_t offset = 0, ssize_t size = -1,
MapFlags flags = MapFlag::NoOption);
bool unmap(uint8_t *addr);
static bool exists(const std::string &name);
private:
LIBCAMERA_DISABLE_COPY(File)
void unmapAll();
std::string name_;
UniqueFD fd_;
OpenMode mode_;
int error_;
std::map<void *, size_t> maps_;
};
LIBCAMERA_FLAGS_ENABLE_OPERATORS(File::MapFlag)
LIBCAMERA_FLAGS_ENABLE_OPERATORS(File::OpenModeFlag)
} /* namespace libcamera */
class.h
Utilities to help constructing class interfaces.
LIBCAMERA_DISABLE_COPY
#define LIBCAMERA_DISABLE_COPY(klass)
Disable copy construction and assignment of the klass.
Definition class.h:27
libcamera::File
Interface for I/O operations on files.
Definition file.h:25
libcamera::File::MapFlag
MapFlag
Flags for the File::map() function.
Definition file.h:27
libcamera::File::MapFlag::NoOption
@ NoOption
No option (used as default value).
Definition file.h:28
libcamera::File::MapFlag::Private
@ Private
The memory region is mapped as private, changes are not reflected in the file constents.
Definition file.h:29
libcamera::File::error
int error() const
Retrieve the file error status.
Definition file.h:56
libcamera::File::fileName
const std::string & fileName() const
Retrieve the file name.
Definition file.h:47
libcamera::File::exists
bool exists() const
Check if the file specified by fileName() exists.
Definition file.cpp:152
libcamera::File::open
bool open(OpenMode mode)
Open the file in the given mode.
Definition file.cpp:173
libcamera::File::write
ssize_t write(const Span< const uint8_t > &data)
Write data to the file.
Definition file.cpp:342
libcamera::File::close
void close()
Close the file.
Definition file.cpp:214
libcamera::File::OpenModeFlag
OpenModeFlag
Mode in which a file is opened.
Definition file.h:34
libcamera::File::OpenModeFlag::ReadOnly
@ ReadOnly
The file is open for reading.
Definition file.h:36
libcamera::File::OpenModeFlag::WriteOnly
@ WriteOnly
The file is open for writing.
Definition file.h:37
libcamera::File::OpenModeFlag::ReadWrite
@ ReadWrite
The file is open for reading and writing.
Definition file.h:38
libcamera::File::OpenModeFlag::NotOpen
@ NotOpen
The file is not open.
Definition file.h:35
libcamera::File::isOpen
bool isOpen() const
Check if the file is open.
Definition file.h:52
libcamera::File::~File
~File()
Destroy a File instance.
Definition file.cpp:108
libcamera::File::File
File()
Construct a File without an associated name.
Definition file.cpp:97
libcamera::File::openMode
OpenMode openMode() const
Retrieve the file open mode.
Definition file.h:53
libcamera::File::OpenMode
Flags< OpenModeFlag > OpenMode
A bitwise combination of File::OpenModeFlag values.
Definition file.h:41
libcamera::File::MapFlags
Flags< MapFlag > MapFlags
A bitwise combination of File::MapFlag values.
Definition file.h:32
libcamera::File::File
File(const std::string &name)
Construct a File to represent the file name.
Definition file.cpp:86
libcamera::File::read
ssize_t read(const Span< uint8_t > &data)
Read data from the file.
Definition file.cpp:304
libcamera::File::seek
off_t seek(off_t pos)
Set the read or write position.
Definition file.cpp:277
libcamera::File::map
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
libcamera::File::unmap
bool unmap(uint8_t *addr)
Unmap a region mapped with map().
Definition file.cpp:434
libcamera::File::size
ssize_t size() const
Retrieve the file size.
Definition file.cpp:243
libcamera::File::pos
off_t pos() const
Return current read or write position.
Definition file.cpp:263
libcamera::File::setFileName
void setFileName(const std::string &name)
Set the name of the file.
Definition file.cpp:129
libcamera::Flags
Type-safe container for enum-based bitfields.
Definition flags.h:16
libcamera::UniqueFD
unique_ptr-like wrapper for a file descriptor
Definition unique_fd.h:17
flags.h
Enum-based bit fields.
LIBCAMERA_FLAGS_ENABLE_OPERATORS
#define LIBCAMERA_FLAGS_ENABLE_OPERATORS(_enum)
Enable bitwise operations on the enum enumeration.
Definition flags.h:189
libcamera
Top-level libcamera namespace.
Definition backtrace.h:17
unique_fd.h
File descriptor wrapper that owns a file descriptor.