Skip to content

Commit

Permalink
rpicam_still: Use std::filesystem to create latest symlink
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
naushir committed Jul 19, 2024
1 parent 18fc2d1 commit 1ae058d
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions apps/rpicam_still.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* rpicam_still.cpp - libcamera stills capture app.
*/
#include <chrono>
#include <filesystem>
#include <poll.h>
#include <signal.h>
#include <sys/signalfd.h>
Expand All @@ -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:
Expand Down Expand Up @@ -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");
}
}
}
Expand Down

0 comments on commit 1ae058d

Please sign in to comment.