Skip to content

Commit

Permalink
revised lockfile implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerZhongAWS committed Apr 7, 2022
1 parent ba27223 commit ba5b580
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
25 changes: 25 additions & 0 deletions source/util/LockFile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

#include <stdexcept>
#include "LockFile.h"

using namespace Aws::Iot::DeviceClient::Util;

LockFile::LockFile(const std::string& filename)
: filename(filename)
, file(fopen(filename.c_str(), "wx"))
{
if (!file)
{
throw std::runtime_error{"unable to open lockfile"};
}
flockfile(file);
}

LockFile::~LockFile()
{
funlockfile(file);
fclose(file);
remove(filename.c_str());
}
37 changes: 37 additions & 0 deletions source/util/LockFile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

#ifndef AWS_IOT_DEVICE_CLIENT_LOCKFILE_H
#define AWS_IOT_DEVICE_CLIENT_LOCKFILE_H

#include <string>

namespace Aws
{
namespace Iot
{
namespace DeviceClient
{
namespace Util
{
struct LockFile
{
private:
static constexpr char TAG[] = "LockFile.cpp";
std::string filename;
FILE *file{nullptr};

public:
LockFile(const std::string& filename);
~LockFile();

// Non-copyable.
LockFile(const LockFile &) = delete;
LockFile &operator=(const LockFile &) = delete;
};
} // namespace Util
} // namespace DeviceClient
} // namespace Iot
} // namespace Aws

#endif // AWS_IOT_DEVICE_CLIENT_LOCKFILE_H

0 comments on commit ba5b580

Please sign in to comment.