libcamera: file: Turn MapFlag and OpenModeFlag into enum class

Add type safety by turning the MapFlag and OpenModeFlag enum into enum
class.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>
This commit is contained in:
Laurent Pinchart 2020-08-03 02:10:28 +03:00
parent 91d06ae2fc
commit 6a8582dc20
6 changed files with 46 additions and 43 deletions

View file

@ -23,14 +23,14 @@ namespace libcamera {
class File class File
{ {
public: public:
enum MapFlag { enum class MapFlag {
MapNoOption = 0, NoOption = 0,
MapPrivate = (1 << 0), Private = (1 << 0),
}; };
using MapFlags = Flags<MapFlag>; using MapFlags = Flags<MapFlag>;
enum OpenModeFlag { enum class OpenModeFlag {
NotOpen = 0, NotOpen = 0,
ReadOnly = (1 << 0), ReadOnly = (1 << 0),
WriteOnly = (1 << 1), WriteOnly = (1 << 1),
@ -62,7 +62,7 @@ public:
ssize_t write(const Span<const uint8_t> &data); ssize_t write(const Span<const uint8_t> &data);
Span<uint8_t> map(off_t offset = 0, ssize_t size = -1, Span<uint8_t> map(off_t offset = 0, ssize_t size = -1,
MapFlags flags = MapNoOption); MapFlags flags = MapFlag::NoOption);
bool unmap(uint8_t *addr); bool unmap(uint8_t *addr);
static bool exists(const std::string &name); static bool exists(const std::string &name);
@ -80,6 +80,9 @@ private:
std::map<void *, size_t> maps_; std::map<void *, size_t> maps_;
}; };
LIBCAMERA_FLAGS_ENABLE_OPERATORS(File::MapFlag)
LIBCAMERA_FLAGS_ENABLE_OPERATORS(File::OpenModeFlag)
} /* namespace libcamera */ } /* namespace libcamera */
#endif /* __LIBCAMERA_BASE_FILE_H__ */ #endif /* __LIBCAMERA_BASE_FILE_H__ */

View file

@ -62,7 +62,7 @@ int IPAVimc::init(const IPASettings &settings)
<< settings.configurationFile; << settings.configurationFile;
File conf(settings.configurationFile); File conf(settings.configurationFile);
if (!conf.open(File::ReadOnly)) { if (!conf.open(File::OpenModeFlag::ReadOnly)) {
LOG(IPAVimc, Error) << "Failed to open configuration file"; LOG(IPAVimc, Error) << "Failed to open configuration file";
return -EINVAL; return -EINVAL;
} }

View file

@ -45,9 +45,9 @@ LOG_DEFINE_CATEGORY(File)
/** /**
* \enum File::MapFlag * \enum File::MapFlag
* \brief Flags for the File::map() function * \brief Flags for the File::map() function
* \var File::MapNoOption * \var File::MapFlag::NoOption
* \brief No option (used as default value) * \brief No option (used as default value)
* \var File::MapPrivate * \var File::MapFlag::Private
* \brief The memory region is mapped as private, changes are not reflected in * \brief The memory region is mapped as private, changes are not reflected in
* the file constents * the file constents
*/ */
@ -60,13 +60,13 @@ LOG_DEFINE_CATEGORY(File)
/** /**
* \enum File::OpenModeFlag * \enum File::OpenModeFlag
* \brief Mode in which a file is opened * \brief Mode in which a file is opened
* \var File::NotOpen * \var File::OpenModeFlag::NotOpen
* \brief The file is not open * \brief The file is not open
* \var File::ReadOnly * \var File::OpenModeFlag::ReadOnly
* \brief The file is open for reading * \brief The file is open for reading
* \var File::WriteOnly * \var File::OpenModeFlag::WriteOnly
* \brief The file is open for writing * \brief The file is open for writing
* \var File::ReadWrite * \var File::OpenModeFlag::ReadWrite
* \brief The file is open for reading and writing * \brief The file is open for reading and writing
*/ */
@ -83,7 +83,7 @@ LOG_DEFINE_CATEGORY(File)
* before performing I/O operations. * before performing I/O operations.
*/ */
File::File(const std::string &name) File::File(const std::string &name)
: name_(name), fd_(-1), mode_(NotOpen), error_(0) : name_(name), fd_(-1), mode_(OpenModeFlag::NotOpen), error_(0)
{ {
} }
@ -94,7 +94,7 @@ File::File(const std::string &name)
* setFileName(). * setFileName().
*/ */
File::File() File::File()
: fd_(-1), mode_(NotOpen), error_(0) : fd_(-1), mode_(OpenModeFlag::NotOpen), error_(0)
{ {
} }
@ -173,8 +173,8 @@ bool File::open(File::OpenMode mode)
return false; return false;
} }
int flags = static_cast<OpenMode::Type>(mode & ReadWrite) - 1; int flags = static_cast<OpenMode::Type>(mode & OpenModeFlag::ReadWrite) - 1;
if (mode & WriteOnly) if (mode & OpenModeFlag::WriteOnly)
flags |= O_CREAT; flags |= O_CREAT;
fd_ = ::open(name_.c_str(), flags, 0666); fd_ = ::open(name_.c_str(), flags, 0666);
@ -214,7 +214,7 @@ void File::close()
::close(fd_); ::close(fd_);
fd_ = -1; fd_ = -1;
mode_ = NotOpen; mode_ = OpenModeFlag::NotOpen;
} }
/** /**
@ -374,8 +374,8 @@ ssize_t File::write(const Span<const uint8_t> &data)
* offset until the end of the file. * offset until the end of the file.
* *
* The mapping memory protection is controlled by the file open mode, unless \a * The mapping memory protection is controlled by the file open mode, unless \a
* flags contains MapPrivate in which case the region is mapped in read/write * flags contains MapFlag::Private in which case the region is mapped in
* mode. * read/write mode.
* *
* The error() status is updated. * The error() status is updated.
* *
@ -398,14 +398,14 @@ Span<uint8_t> File::map(off_t offset, ssize_t size, File::MapFlags flags)
size -= offset; size -= offset;
} }
int mmapFlags = flags & MapPrivate ? MAP_PRIVATE : MAP_SHARED; int mmapFlags = flags & MapFlag::Private ? MAP_PRIVATE : MAP_SHARED;
int prot = 0; int prot = 0;
if (mode_ & ReadOnly) if (mode_ & OpenModeFlag::ReadOnly)
prot |= PROT_READ; prot |= PROT_READ;
if (mode_ & WriteOnly) if (mode_ & OpenModeFlag::WriteOnly)
prot |= PROT_WRITE; prot |= PROT_WRITE;
if (flags & MapPrivate) if (flags & MapFlag::Private)
prot |= PROT_WRITE; prot |= PROT_WRITE;
void *map = mmap(NULL, size, prot, mmapFlags, fd_, offset); void *map = mmap(NULL, size, prot, mmapFlags, fd_, offset);

View file

@ -285,7 +285,7 @@ bool IPAManager::isSignatureValid([[maybe_unused]] IPAModule *ipa) const
} }
File file{ ipa->path() }; File file{ ipa->path() };
if (!file.open(File::ReadOnly)) if (!file.open(File::OpenModeFlag::ReadOnly))
return false; return false;
Span<uint8_t> data = file.map(); Span<uint8_t> data = file.map();

View file

@ -275,7 +275,7 @@ IPAModule::~IPAModule()
int IPAModule::loadIPAModuleInfo() int IPAModule::loadIPAModuleInfo()
{ {
File file{ libPath_ }; File file{ libPath_ };
if (!file.open(File::ReadOnly)) { if (!file.open(File::OpenModeFlag::ReadOnly)) {
LOG(IPAModule, Error) << "Failed to open IPA library: " LOG(IPAModule, Error) << "Failed to open IPA library: "
<< strerror(-file.error()); << strerror(-file.error());
return file.error(); return file.error();
@ -317,13 +317,13 @@ int IPAModule::loadIPAModuleInfo()
/* Load the signature. Failures are not fatal. */ /* Load the signature. Failures are not fatal. */
File sign{ libPath_ + ".sign" }; File sign{ libPath_ + ".sign" };
if (!sign.open(File::ReadOnly)) { if (!sign.open(File::OpenModeFlag::ReadOnly)) {
LOG(IPAModule, Debug) LOG(IPAModule, Debug)
<< "IPA module " << libPath_ << " is not signed"; << "IPA module " << libPath_ << " is not signed";
return 0; return 0;
} }
data = sign.map(0, -1, File::MapPrivate); data = sign.map(0, -1, File::MapFlag::Private);
signature_.resize(data.size()); signature_.resize(data.size());
memcpy(signature_.data(), data.data(), data.size()); memcpy(signature_.data(), data.data(), data.size());

View file

@ -72,7 +72,7 @@ protected:
return TestFail; return TestFail;
} }
if (file.openMode() != File::NotOpen) { if (file.openMode() != File::OpenModeFlag::NotOpen) {
cerr << "File has invalid open mode after construction" cerr << "File has invalid open mode after construction"
<< endl; << endl;
return TestFail; return TestFail;
@ -83,7 +83,7 @@ protected:
return TestFail; return TestFail;
} }
if (file.open(File::ReadWrite)) { if (file.open(File::OpenModeFlag::ReadWrite)) {
cerr << "Opening unnamed file succeeded" << endl; cerr << "Opening unnamed file succeeded" << endl;
return TestFail; return TestFail;
} }
@ -111,7 +111,7 @@ protected:
return TestFail; return TestFail;
} }
if (file.openMode() != File::NotOpen) { if (file.openMode() != File::OpenModeFlag::NotOpen) {
cerr << "Invalid file has invalid open mode after construction" cerr << "Invalid file has invalid open mode after construction"
<< endl; << endl;
return TestFail; return TestFail;
@ -122,7 +122,7 @@ protected:
return TestFail; return TestFail;
} }
if (file.open(File::ReadWrite)) { if (file.open(File::OpenModeFlag::ReadWrite)) {
cerr << "Opening invalid file succeeded" << endl; cerr << "Opening invalid file succeeded" << endl;
return TestFail; return TestFail;
} }
@ -140,7 +140,7 @@ protected:
return TestFail; return TestFail;
} }
if (file.openMode() != File::NotOpen) { if (file.openMode() != File::OpenModeFlag::NotOpen) {
cerr << "Valid file has invalid open mode after construction" cerr << "Valid file has invalid open mode after construction"
<< endl; << endl;
return TestFail; return TestFail;
@ -152,7 +152,7 @@ protected:
} }
/* Test open and close. */ /* Test open and close. */
if (!file.open(File::ReadWrite)) { if (!file.open(File::OpenModeFlag::ReadWrite)) {
cerr << "Opening file failed" << endl; cerr << "Opening file failed" << endl;
return TestFail; return TestFail;
} }
@ -162,7 +162,7 @@ protected:
return TestFail; return TestFail;
} }
if (file.openMode() != File::ReadWrite) { if (file.openMode() != File::OpenModeFlag::ReadWrite) {
cerr << "Open file has invalid open mode" << endl; cerr << "Open file has invalid open mode" << endl;
return TestFail; return TestFail;
} }
@ -174,7 +174,7 @@ protected:
return TestFail; return TestFail;
} }
if (file.openMode() != File::NotOpen) { if (file.openMode() != File::OpenModeFlag::NotOpen) {
cerr << "Closed file has invalid open mode" << endl; cerr << "Closed file has invalid open mode" << endl;
return TestFail; return TestFail;
} }
@ -187,7 +187,7 @@ protected:
return TestFail; return TestFail;
} }
file.open(File::ReadOnly); file.open(File::OpenModeFlag::ReadOnly);
ssize_t size = file.size(); ssize_t size = file.size();
if (size <= 0) { if (size <= 0) {
@ -205,12 +205,12 @@ protected:
return TestFail; return TestFail;
} }
if (file.open(File::ReadOnly)) { if (file.open(File::OpenModeFlag::ReadOnly)) {
cerr << "Read-only open succeeded on nonexistent file" << endl; cerr << "Read-only open succeeded on nonexistent file" << endl;
return TestFail; return TestFail;
} }
if (!file.open(File::WriteOnly)) { if (!file.open(File::OpenModeFlag::WriteOnly)) {
cerr << "Write-only open failed on nonexistent file" << endl; cerr << "Write-only open failed on nonexistent file" << endl;
return TestFail; return TestFail;
} }
@ -238,7 +238,7 @@ protected:
return TestFail; return TestFail;
} }
file.open(File::ReadOnly); file.open(File::OpenModeFlag::ReadOnly);
if (file.write(buffer) >= 0) { if (file.write(buffer) >= 0) {
cerr << "Write succeeded on read-only file" << endl; cerr << "Write succeeded on read-only file" << endl;
@ -247,7 +247,7 @@ protected:
file.close(); file.close();
file.open(File::ReadWrite); file.open(File::OpenModeFlag::ReadWrite);
if (file.write({ buffer.data(), 9 }) != 9) { if (file.write({ buffer.data(), 9 }) != 9) {
cerr << "Write test failed" << endl; cerr << "Write test failed" << endl;
@ -278,7 +278,7 @@ protected:
/* Test mapping and unmapping. */ /* Test mapping and unmapping. */
file.setFileName("/proc/self/exe"); file.setFileName("/proc/self/exe");
file.open(File::ReadOnly); file.open(File::OpenModeFlag::ReadOnly);
Span<uint8_t> data = file.map(); Span<uint8_t> data = file.map();
if (data.empty()) { if (data.empty()) {
@ -316,9 +316,9 @@ protected:
/* Test private mapping. */ /* Test private mapping. */
file.setFileName(fileName_); file.setFileName(fileName_);
file.open(File::ReadWrite); file.open(File::OpenModeFlag::ReadWrite);
data = file.map(0, -1, File::MapPrivate); data = file.map(0, -1, File::MapFlag::Private);
if (data.empty()) { if (data.empty()) {
cerr << "Private mapping failed" << endl; cerr << "Private mapping failed" << endl;
return TestFail; return TestFail;