Factor out platform inconsistency between Mac and Linux (stat.st_atim vs stat.st_atimespec)

This commit is contained in:
Sebastian Messmer 2016-06-02 20:08:51 -07:00
parent 61e56cfeab
commit 1402f54fec
6 changed files with 19 additions and 22 deletions

View File

@ -0,0 +1,14 @@
#pragma once
#ifndef MESSMER_CPPUTILS_SYSTEM_STAT_H
#define MESSMER_CPPUTILS_SYSTEM_STAT_H
/**
* For platform independence: Apple doesn't have stat.st_atim, but stat.st_atimespec
*/
#ifdef __APPLE__
# define st_atim st_atimespec
# define st_mtim st_mtimespec
# define st_ctim st_ctimespec
#endif
#endif

View File

@ -8,6 +8,7 @@
#include <fspp/fuse/FuseErrnoException.h>
#include <cpp-utils/pointer/cast.h>
#include <cpp-utils/system/clock_gettime.h>
#include <cpp-utils/system/stat.h>
namespace bf = boost::filesystem;
@ -123,15 +124,9 @@ void CryNode::stat(struct ::stat *result) const {
result->st_nlink = 1;
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
#ifdef __APPLE__
result->st_atimespec = now;
result->st_mtimespec = now;
result->st_ctimespec = now;
#else
result->st_atim = now;
result->st_mtim = now;
result->st_ctim = now;
#endif
} else {
(*_parent)->statChild(_key, result);
}

View File

@ -9,6 +9,7 @@
#include "../CryDevice.h"
#include "FileBlob.h"
#include "SymlinkBlob.h"
#include <cpp-utils/system/stat.h>
using std::vector;
using std::string;
@ -149,15 +150,9 @@ void DirBlob::statChildExceptSize(const Key &key, struct ::stat *result) const {
result->st_gid = child.gid();
//TODO If possible without performance loss, then for a directory, st_nlink should return number of dir entries (including "." and "..")
result->st_nlink = 1;
#ifdef __APPLE__
result->st_atimespec = child.lastAccessTime();
result->st_mtimespec = child.lastModificationTime();
result->st_ctimespec = child.lastMetadataChangeTime();
#else
result->st_atim = child.lastAccessTime();
result->st_mtim = child.lastModificationTime();
result->st_ctim = child.lastMetadataChangeTime();
#endif
//TODO Move ceilDivision to general utils which can be used by cryfs as well
result->st_blocks = blobstore::onblocks::utils::ceilDivision(result->st_size, (off_t)512);
result->st_blksize = _fsBlobStore->virtualBlocksizeBytes();

View File

@ -5,6 +5,7 @@
#include "testutils/FsppNodeTest.h"
#include "../fuse/FuseErrnoException.h"
#include "testutils/TimestampTestUtils.h"
#include <cpp-utils/system/stat.h>
using namespace cpputils::time;
using std::function;

View File

@ -5,6 +5,7 @@
#include "FileSystemTest.h"
#include <cpp-utils/data/Data.h>
#include <cpp-utils/pointer/unique_ref.h>
#include <cpp-utils/system/stat.h>
template<class ConcreteFileSystemTestFixture>
class FileTest: public FileSystemTest<ConcreteFileSystemTestFixture> {
@ -49,23 +50,13 @@ public:
}
void EXPECT_ATIME_EQ(struct timespec expected, struct stat st) {
#ifdef __APPLE__
EXPECT_EQ(expected.tv_sec, st.st_atimespec.tv_sec);
EXPECT_EQ(expected.tv_nsec, st.st_atimespec.tv_nsec);
#else
EXPECT_EQ(expected.tv_sec, st.st_atim.tv_sec);
EXPECT_EQ(expected.tv_nsec, st.st_atim.tv_nsec);
#endif
}
void EXPECT_MTIME_EQ(struct timespec expected, struct stat st) {
#ifdef __APPLE__
EXPECT_EQ(expected.tv_sec, st.st_mtimespec.tv_sec);
EXPECT_EQ(expected.tv_nsec, st.st_mtimespec.tv_nsec);
#else
EXPECT_EQ(expected.tv_sec, st.st_mtim.tv_sec);
EXPECT_EQ(expected.tv_nsec, st.st_mtim.tv_nsec);
#endif
}
};

View File

@ -3,6 +3,7 @@
#define MESSMER_FSPP_FSTEST_TESTUTILS_TIMESTAMPTESTUTILS_H_
#include <cpp-utils/system/time.h>
#include <cpp-utils/system/stat.h>
class TimestampTestUtils {
public: