Skip to content

Commit

Permalink
Add EXIV2_ENABLE_FILESYSTEM_ACCESS option
Browse files Browse the repository at this point in the history
  • Loading branch information
enen92 committed Nov 18, 2023
1 parent e8326ba commit 71e9227
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 6 deletions.
9 changes: 9 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ option(EXIV2_ENABLE_BMFF "Build with BMFF support" ON)
option(EXIV2_ENABLE_BROTLI "Use Brotli for JPEG XL compressed boxes (BMFF)" ON)
option(EXIV2_ENABLE_VIDEO "Build with video support" ON)
option(EXIV2_ENABLE_INIH "Use inih library" ON)
option(EXIV2_ENABLE_FILESYSTEM_ACCESS "Build with filesystem access" ON)

option(EXIV2_BUILD_SAMPLES "Build sample applications" OFF)
option(EXIV2_BUILD_EXIV2_COMMAND "Build exiv2 command-line executable" ON)
Expand Down Expand Up @@ -100,18 +101,22 @@ include(cmake/compilerFlagsExiv2.cmake REQUIRED)
add_subdirectory(src)

if(BUILD_TESTING AND EXIV2_BUILD_UNIT_TESTS)
set(EXIV2_ENABLE_FILESYSTEM_ACCESS ON)
add_subdirectory(unitTests)
endif()

if(EXIV2_BUILD_FUZZ_TESTS)
set(EXIV2_ENABLE_FILESYSTEM_ACCESS ON)
add_subdirectory(fuzz)
endif()

if(EXIV2_BUILD_EXIV2_COMMAND)
add_subdirectory(app)
set(EXIV2_ENABLE_FILESYSTEM_ACCESS ON)

if(EXIV2_BUILD_SAMPLES)
add_subdirectory(samples)
set(EXIV2_ENABLE_FILESYSTEM_ACCESS ON)
get_directory_property(SAMPLES DIRECTORY samples DEFINITION APPLICATIONS)

if(BUILD_TESTING AND Python3_Interpreter_FOUND)
Expand Down Expand Up @@ -160,6 +165,10 @@ if(EXIV2_TEAM_PACKAGING)
include(cmake/packaging.cmake)
endif()

if(EXIV2_ENABLE_FILESYSTEM_ACCESS)
add_definitions(-DEXV_ENABLE_FILESYSTEM)
endif()

# Handle both relative and absolute paths (e.g. NixOS) for a relocatable package
if(IS_ABSOLUTE "${CMAKE_INSTALL_LIBDIR}")
set(EXIV2_LIBDIR "${CMAKE_INSTALL_LIBDIR}")
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ option( EXIV2_ENABLE_PNG "Build with png support (requires libz)"
...
option( EXIV2_ENABLE_BMFF "Build with BMFF support (brotli recommended)" ON )
option( EXIV2_ENABLE_BROTLI "Use Brotli for JPEG XL compressed boxes (BMFF)" ON )
option( EXIV2_ENABLE_FILESYSTEM_ACCESS "Build with filesystem access" ON )
577 rmills@rmillsmm:~/gnu/github/exiv2/exiv2 $
```

Expand Down
3 changes: 3 additions & 0 deletions cmake/config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
// Define to 1 if you want to use libcurl in httpIO.
#cmakedefine EXV_USE_CURL

// Define to 1 if you want to enable filesystem access
#cmakedefine EXV_ENABLE_FILESYSTEM

// Define if you require webready support.
#cmakedefine EXV_ENABLE_WEBREADY

Expand Down
4 changes: 3 additions & 1 deletion cmake/findDependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ if(BUILD_TESTING)
endif()
endif()

find_package(Filesystem COMPONENTS Experimental Final REQUIRED)
if(EXIV2_ENABLE_FILESYSTEM_ACCESS)
find_package(Filesystem COMPONENTS Experimental Final REQUIRED)
endif()

# don't use Frameworks on the Mac (#966)
if (APPLE)
Expand Down
1 change: 1 addition & 0 deletions cmake/printSummary.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ OptionOutput( "Building unit tests: " EXIV2_BUILD_UNIT_TESTS AND
OptionOutput( "Building fuzz tests: " EXIV2_BUILD_FUZZ_TESTS )
OptionOutput( "Building doc: " EXIV2_BUILD_DOC )
OptionOutput( "Building with coverage flags: " BUILD_WITH_COVERAGE )
OptionOutput( "Building with filesystem access " EXIV2_ENABLE_FILESYSTEM_ACCESS )
OptionOutput( "Using ccache: " BUILD_WITH_CCACHE )
4 changes: 3 additions & 1 deletion include/exiv2/basicio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ class EXIV2API IoCloser {
IoCloser& operator=(const IoCloser&) = delete;
}; // class IoCloser

#ifdef EXV_ENABLE_FILESYSTEM
/*!
@brief Provides binary file IO by implementing the BasicIo
interface.
Expand Down Expand Up @@ -479,6 +480,7 @@ class EXIV2API FileIo : public BasicIo {
std::unique_ptr<Impl> p_;

}; // class FileIo
#endif

/*!
@brief Provides binary IO on blocks of memory by implementing the BasicIo
Expand Down Expand Up @@ -686,7 +688,7 @@ class EXIV2API XPathIo : public MemIo {
*/
void ReadDataUri(const std::string& path);
}; // class XPathIo
#else
#elif defined(EXV_ENABLE_FILESYSTEM)
class EXIV2API XPathIo : public FileIo {
public:
/*!
Expand Down
6 changes: 6 additions & 0 deletions include/exiv2/exif.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ class EXIV2API ExifThumbC {
data buffer and %DataBuf ensures that it will be deleted.
*/
[[nodiscard]] DataBuf copy() const;
#ifdef EXV_ENABLE_FILESYSTEM
/*!
@brief Write the thumbnail image to a file.
Expand All @@ -240,6 +241,7 @@ class EXIV2API ExifThumbC {
@return The number of bytes written.
*/
[[nodiscard]] size_t writeFile(const std::string& path) const;
#endif
/*!
@brief Return the MIME type of the thumbnail, either \c "image/tiff"
or \c "image/jpeg".
Expand Down Expand Up @@ -279,6 +281,7 @@ class EXIV2API ExifThumb : public ExifThumbC {

//! @name Manipulators
//@{
#ifdef EXV_ENABLE_FILESYSTEM
/*!
@brief Set the Exif thumbnail to the JPEG image \em path. Set
XResolution, YResolution and ResolutionUnit to \em xres,
Expand All @@ -297,6 +300,7 @@ class EXIV2API ExifThumb : public ExifThumbC {
application that comes with OS X for one.) - David Harvey.
*/
void setJpegThumbnail(const std::string& path, URational xres, URational yres, uint16_t unit);
#endif
/*!
@brief Set the Exif thumbnail to the JPEG image pointed to by \em buf,
and size \em size. Set XResolution, YResolution and
Expand All @@ -315,6 +319,7 @@ class EXIV2API ExifThumb : public ExifThumbC {
application that comes with OS X for one.) - David Harvey.
*/
void setJpegThumbnail(const byte* buf, size_t size, URational xres, URational yres, uint16_t unit);
#ifdef EXV_ENABLE_FILESYSTEM
/*!
@brief Set the Exif thumbnail to the JPEG image \em path.
Expand All @@ -329,6 +334,7 @@ class EXIV2API ExifThumb : public ExifThumbC {
@note Additional existing Exif thumbnail tags are not modified.
*/
void setJpegThumbnail(const std::string& path);
#endif
/*!
@brief Set the Exif thumbnail to the JPEG image pointed to by \em buf,
and size \em size.
Expand Down
2 changes: 2 additions & 0 deletions include/exiv2/preview.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class EXIV2API PreviewImage {
@brief Return the size of the preview image in bytes.
*/
[[nodiscard]] uint32_t size() const;
#ifdef EXV_ENABLE_FILESYSTEM
/*!
@brief Write the thumbnail image to a file.
Expand All @@ -79,6 +80,7 @@ class EXIV2API PreviewImage {
@return The number of bytes written.
*/
[[nodiscard]] size_t writeFile(const std::string& path) const;
#endif
/*!
@brief Return the MIME type of the preview image, usually either
\c "image/tiff" or \c "image/jpeg".
Expand Down
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ cdata.set('EXV_HAVE_LIBZ', zlib_dep.found())
cdata.set('EXV_USE_CURL', curl_dep.found())
cdata.set('EXV_ENABLE_NLS', intl_dep.found())
cdata.set('EXV_ENABLE_WEBREADY', curl_dep.found())
cdata.set('EXV_ENABLE_FILESYSTEM', true)

cfile = configure_file(
input: 'cmake/config.h.cmake',
Expand Down
9 changes: 7 additions & 2 deletions src/basicio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <curl/curl.h>
#endif

#ifdef EXV_ENABLE_FILESYSTEM
#ifdef _WIN32
#include <io.h>
#include <windows.h>
Expand All @@ -47,6 +48,7 @@ namespace fs = std::filesystem;
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#endif
#endif

// *****************************************************************************
// class member definitions
Expand All @@ -73,6 +75,7 @@ void BasicIo::seekOrThrow(int64_t offset, Position pos, ErrorCode err) {
Internal::enforce(r == 0, err);
}

#ifdef EXV_ENABLE_FILESYSTEM
//! Internal Pimpl structure of class FileIo.
class FileIo::Impl {
public:
Expand Down Expand Up @@ -550,6 +553,7 @@ const std::string& FileIo::path() const noexcept {

void FileIo::populateFakeData() {
}
#endif

//! Internal Pimpl structure of class MemIo.
class MemIo::Impl final {
Expand Down Expand Up @@ -898,7 +902,7 @@ void XPathIo::ReadDataUri(const std::string& path) {
delete[] decodeData;
}

#else
#elif defined(EXV_ENABLE_FILESYSTEM)
XPathIo::XPathIo(const std::string& orgPath) : FileIo(XPathIo::writeDataToFile(orgPath)), tempFilePath_(path()) {
}

Expand Down Expand Up @@ -1706,7 +1710,7 @@ CurlIo::CurlIo(const std::string& url, size_t blockSize) {

// *************************************************************************
// free functions

#ifdef EXV_ENABLE_FILESYSTEM
DataBuf readFile(const std::string& path) {
FileIo file(path);
if (file.open("rb") != 0) {
Expand All @@ -1726,6 +1730,7 @@ size_t writeFile(const DataBuf& buf, const std::string& path) {
}
return file.write(buf.c_data(), buf.size());
}
#endif

#ifdef EXV_USE_CURL
size_t curlWriter(char* data, size_t size, size_t nmemb, std::string* writerData) {
Expand Down
6 changes: 6 additions & 0 deletions src/exif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ DataBuf ExifThumbC::copy() const {
return thumbnail->copy(exifData_);
}

#ifdef EXV_ENABLE_FILESYSTEM
size_t ExifThumbC::writeFile(const std::string& path) const {
auto thumbnail = Thumbnail::create(exifData_);
if (!thumbnail)
Expand All @@ -387,6 +388,7 @@ size_t ExifThumbC::writeFile(const std::string& path) const {

return Exiv2::writeFile(buf, name);
}
#endif

const char* ExifThumbC::mimeType() const {
auto thumbnail = Thumbnail::create(exifData_);
Expand All @@ -405,10 +407,12 @@ const char* ExifThumbC::extension() const {
ExifThumb::ExifThumb(ExifData& exifData) : ExifThumbC(exifData), exifData_(exifData) {
}

#ifdef EXV_ENABLE_FILESYSTEM
void ExifThumb::setJpegThumbnail(const std::string& path, URational xres, URational yres, uint16_t unit) {
DataBuf thumb = readFile(path); // may throw
setJpegThumbnail(thumb.c_data(), thumb.size(), xres, yres, unit);
}
#endif

void ExifThumb::setJpegThumbnail(const byte* buf, size_t size, URational xres, URational yres, uint16_t unit) {
setJpegThumbnail(buf, size);
Expand All @@ -417,10 +421,12 @@ void ExifThumb::setJpegThumbnail(const byte* buf, size_t size, URational xres, U
exifData_["Exif.Thumbnail.ResolutionUnit"] = unit;
}

#ifdef EXV_ENABLE_FILESYSTEM
void ExifThumb::setJpegThumbnail(const std::string& path) {
DataBuf thumb = readFile(path); // may throw
setJpegThumbnail(thumb.c_data(), thumb.size());
}
#endif

void ExifThumb::setJpegThumbnail(const byte* buf, size_t size) {
exifData_["Exif.Thumbnail.Compression"] = static_cast<uint16_t>(6);
Expand Down
8 changes: 8 additions & 0 deletions src/futils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
#include <sstream>
#include <stdexcept>

#ifdef EXV_ENABLE_FILESYSTEM
#if __has_include(<filesystem>)
#include <filesystem>
namespace fs = std::filesystem;
#else
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#endif
#endif

#if defined(_WIN32)
// clang-format off
Expand Down Expand Up @@ -231,7 +233,10 @@ bool fileExists(const std::string& path) {
if (fileProtocol(path) != pFile) {
return true;
}
#ifdef EXV_ENABLE_FILESYSTEM
return fs::exists(path);
#endif
return false;
}

std::string strError() {
Expand Down Expand Up @@ -341,6 +346,7 @@ Uri Uri::Parse(const std::string& uri) {
}

std::string getProcessPath() {
#ifdef EXV_ENABLE_FILESYSTEM
#if defined(__FreeBSD__)
std::string ret("unknown");
unsigned int n;
Expand Down Expand Up @@ -382,5 +388,7 @@ std::string getProcessPath() {
return "unknown";
}
#endif
#endif
return "unknown";
}
} // namespace Exiv2
8 changes: 8 additions & 0 deletions src/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -785,8 +785,11 @@ bool ImageFactory::checkType(ImageType type, BasicIo& io, bool advance) {
}

ImageType ImageFactory::getType(const std::string& path) {
#ifdef EXV_ENABLE_FILESYSTEM
FileIo fileIo(path);
return getType(fileIo);
#endif
return ImageType::none;
}

ImageType ImageFactory::getType(const byte* data, size_t size) {
Expand Down Expand Up @@ -817,12 +820,15 @@ BasicIo::UniquePtr ImageFactory::createIo(const std::string& path, [[maybe_unuse

if (fProt == pHttp)
return std::make_unique<HttpIo>(path); // may throw
#ifdef EXV_ENABLE_FILESYSTEM
if (fProt == pFileUri)
return std::make_unique<FileIo>(pathOfFileUrl(path));
if (fProt == pStdin || fProt == pDataUri)
return std::make_unique<XPathIo>(path); // may throw

return std::make_unique<FileIo>(path);
#endif
return nullptr;
} // ImageFactory::createIo

Image::UniquePtr ImageFactory::open(const std::string& path, bool useCurl) {
Expand Down Expand Up @@ -851,6 +857,7 @@ Image::UniquePtr ImageFactory::open(BasicIo::UniquePtr io) {
return nullptr;
}

#ifdef EXV_ENABLE_FILESYSTEM
Image::UniquePtr ImageFactory::create(ImageType type, const std::string& path) {
auto fileIo = std::make_unique<FileIo>(path);
// Create or overwrite the file, then close it
Expand All @@ -865,6 +872,7 @@ Image::UniquePtr ImageFactory::create(ImageType type, const std::string& path) {
throw Error(ErrorCode::kerUnsupportedImageType, static_cast<int>(type));
return image;
}
#endif

Image::UniquePtr ImageFactory::create(ImageType type) {
auto image = create(type, std::make_unique<MemIo>());
Expand Down
Loading

0 comments on commit 71e9227

Please sign in to comment.