From a2b156fc7607ecd2de8d389767924ef9f66588cd Mon Sep 17 00:00:00 2001 From: Naushir Patuck Date: Fri, 19 Jul 2024 11:14:41 +0100 Subject: [PATCH] rpicam_still: Use std::filesystem to create latest symlink This is a more robust way of creating the latest file symlink, and allows the symlink to be created in a directory different to the output files. Signed-off-by: Naushir Patuck --- apps/rpicam_still.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/apps/rpicam_still.cpp b/apps/rpicam_still.cpp index 11a186d0..5e97aa79 100644 --- a/apps/rpicam_still.cpp +++ b/apps/rpicam_still.cpp @@ -5,6 +5,7 @@ * rpicam_still.cpp - libcamera stills capture app. */ #include +#include #include #include #include @@ -24,6 +25,8 @@ using namespace std::chrono_literals; using namespace std::placeholders; using libcamera::Stream; +namespace fs = std::filesystem; + class RPiCamStillApp : public RPiCamApp { public: @@ -61,15 +64,15 @@ static void update_latest_link(std::string const &filename, StillOptions const * // Create a fixed-name link to the most recent output file, if requested. if (!options->latest.empty()) { - struct stat buf; - if (stat(options->latest.c_str(), &buf) == 0 && unlink(options->latest.c_str())) + fs::path link { options->latest }; + fs::path target { filename }; + + if (fs::exists(link) && !fs::remove(link)) LOG_ERROR("WARNING: could not delete latest link " << options->latest); else { - if (symlink(filename.c_str(), options->latest.c_str())) - LOG_ERROR("WARNING: failed to create latest link " << options->latest); - else - LOG(2, "Link " << options->latest << " created"); + fs::create_symlink(target, link); + LOG(2, "Link " << options->latest << " created"); } } }