Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[backport] Add EXIV2_ENABLE_FILESYSTEM_ACCESS option #2994

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ option( EXIV2_ENABLE_BMFF "Build with BMFF support"
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 @@ -95,17 +96,21 @@ add_subdirectory( src )

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

if( EXIV2_BUILD_FUZZ_TESTS )
add_subdirectory ( fuzz )
set(EXIV2_ENABLE_FILESYSTEM_ACCESS ON)
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 (Python3_Interpreter_FOUND)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,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 @@ -35,7 +35,9 @@ if (NOT Python3_Interpreter_FOUND)
message(WARNING "Python3 was not found. Python tests under the 'tests' folder will not be executed")
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
9 changes: 5 additions & 4 deletions cmake/generateConfigFile.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ include(CheckCXXSymbolExists)
if (${EXIV2_ENABLE_WEBREADY})
set(EXV_USE_CURL ${EXIV2_ENABLE_CURL})
endif()
set(EXV_ENABLE_BMFF ${EXIV2_ENABLE_BMFF})
set(EXV_ENABLE_WEBREADY ${EXIV2_ENABLE_WEBREADY})
set(EXV_HAVE_LENSDATA ${EXIV2_ENABLE_LENSDATA})
set(EXV_ENABLE_INIH ${EXIV2_ENABLE_INIH})
set(EXV_ENABLE_BMFF ${EXIV2_ENABLE_BMFF})
set(EXV_ENABLE_WEBREADY ${EXIV2_ENABLE_WEBREADY})
set(EXV_HAVE_LENSDATA ${EXIV2_ENABLE_LENSDATA})
set(EXV_ENABLE_INIH ${EXIV2_ENABLE_INIH})
set(EXV_ENABLE_FILESYSTEM ${EXIV2_ENABLE_FILESYSTEM_ACCESS})

set(EXV_PACKAGE_NAME ${PROJECT_NAME})
set(EXV_PACKAGE_VERSION ${PROJECT_VERSION})
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
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 @@ -120,6 +120,7 @@ cdata.set('EXV_HAVE_LIBZ', zlib_dep.found())
cdata.set('EXV_ENABLE_WEBREADY', web_dep.found())
cdata.set('EXV_USE_CURL', curl_dep.found())
cdata.set('EXV_ENABLE_NLS', intl_dep.found())
cdata.set('EXV_ENABLE_FILESYSTEM', true)

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

#ifdef EXV_ENABLE_FILESYSTEM
#ifdef _WIN32
using mode_t = unsigned short;
#include <io.h>
Expand All @@ -49,19 +50,7 @@ namespace fs = std::filesystem;
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#endif

// *****************************************************************************
// class member definitions
namespace {
/// @brief replace each substring of the subject that matches the given search string with the given replacement.
void ReplaceStringInPlace(std::string& subject, std::string_view search, std::string_view replace) {
auto pos = subject.find(search);
while (pos != std::string::npos) {
subject.replace(pos, search.length(), replace);
pos += subject.find(search, pos + replace.length());
}
}
} // namespace
#endif

namespace Exiv2 {
void BasicIo::readOrThrow(byte* buf, size_t rcount, ErrorCode err) {
Expand All @@ -75,6 +64,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 @@ -567,6 +557,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 @@ -915,7 +906,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 All @@ -930,6 +921,16 @@ void XPathIo::transfer(BasicIo& src) {
if (isTemp_) {
// replace temp path to gent path.
auto currentPath = path();

// replace each substring of the subject that matches the given search string with the given replacement.
auto ReplaceStringInPlace = [](std::string& subject, std::string_view search, std::string_view replace) {
auto pos = subject.find(search);
while (pos != std::string::npos) {
subject.replace(pos, search.length(), replace);
pos += subject.find(search, pos + replace.length());
}
};

ReplaceStringInPlace(currentPath, XPathIo::TEMP_FILE_EXT, XPathIo::GEN_FILE_EXT);
setPath(currentPath);

Expand Down Expand Up @@ -1725,7 +1726,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 @@ -1749,6 +1750,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
10 changes: 10 additions & 0 deletions src/futils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,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,11 @@ bool fileExists(const std::string& path) {
if (fileProtocol(path) != pFile) {
return true;
}
#ifdef EXV_ENABLE_FILESYSTEM
return fs::exists(path);
#else
return false;
#endif
}

std::string strError() {
Expand Down Expand Up @@ -341,6 +347,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 +389,8 @@ std::string getProcessPath() {
return "unknown";
}
#endif
#else
return "unknown";
#endif
}
} // namespace Exiv2
Loading
Loading