Skip to content
Permalink
Browse files

Allow storageInfo() to follow links.

This allows info to be retrieved from linked files/paths in a single step.
  • Loading branch information...
dwsteele committed Apr 29, 2019
1 parent 60edadf commit 62b48d8a5a9bb7aa962e96566073dd58cfdb4436
@@ -33,6 +33,10 @@
<p>Add <code>storageInfoList()</code> to get detailed info about all entries in a path.</p>
</release-item>

<release-item>
<p>Allow <code>storageInfo()</code> to follow links.</p>
</release-item>

<release-item>
<p>Add <code>ioWriteStr()</code> and <code>ioWriteStrLine()</code>.</p>
</release-item>
@@ -120,12 +120,13 @@ storageDriverPosixExists(StorageDriverPosix *this, const String *path)
File/path info
***********************************************************************************************************************************/
StorageInfo
storageDriverPosixInfo(StorageDriverPosix *this, const String *file, bool ignoreMissing)
storageDriverPosixInfo(StorageDriverPosix *this, const String *file, bool ignoreMissing, bool followLink)
{
FUNCTION_LOG_BEGIN(logLevelTrace);
FUNCTION_LOG_PARAM(STORAGE_DRIVER_POSIX, this);
FUNCTION_LOG_PARAM(STRING, file);
FUNCTION_LOG_PARAM(BOOL, ignoreMissing);
FUNCTION_LOG_PARAM(BOOL, followLink);
FUNCTION_LOG_END();

ASSERT(this != NULL);
@@ -136,7 +137,7 @@ storageDriverPosixInfo(StorageDriverPosix *this, const String *file, bool ignore
// Attempt to stat the file
struct stat statFile;

if (lstat(strPtr(file), &statFile) == -1)
if ((followLink ? stat(strPtr(file), &statFile) : lstat(strPtr(file), &statFile)) == -1)
{
if (errno != ENOENT || !ignoreMissing)
THROW_SYS_ERROR_FMT(FileOpenError, "unable to get info for '%s'", strPtr(file));
@@ -212,7 +213,7 @@ storageDriverPosixInfoListEntry(
{
String *pathInfo = strEqZ(name, ".") ? strDup(path) : strNewFmt("%s/%s", strPtr(path), strPtr(name));

StorageInfo storageInfo = storageDriverPosixInfo(this, pathInfo, true);
StorageInfo storageInfo = storageDriverPosixInfo(this, pathInfo, true, false);

if (storageInfo.exists)
{
@@ -34,7 +34,7 @@ StorageDriverPosix *storageDriverPosixNew(
Functions
***********************************************************************************************************************************/
bool storageDriverPosixExists(StorageDriverPosix *this, const String *path);
StorageInfo storageDriverPosixInfo(StorageDriverPosix *this, const String *file, bool ignoreMissing);
StorageInfo storageDriverPosixInfo(StorageDriverPosix *this, const String *file, bool ignoreMissing, bool followLink);
bool storageDriverPosixInfoList(
StorageDriverPosix *this, const String *path, bool errorOnMissing, StorageInfoListCallback callback, void *callbackData);
StringList *storageDriverPosixList(StorageDriverPosix *this, const String *path, bool errorOnMissing, const String *expression);
@@ -104,12 +104,13 @@ storageDriverRemoteExists(StorageDriverRemote *this, const String *path)
File/path info
***********************************************************************************************************************************/
StorageInfo
storageDriverRemoteInfo(StorageDriverRemote *this, const String *file, bool ignoreMissing)
storageDriverRemoteInfo(StorageDriverRemote *this, const String *file, bool ignoreMissing, bool followLink)
{
FUNCTION_LOG_BEGIN(logLevelDebug);
FUNCTION_LOG_PARAM(STORAGE_DRIVER_REMOTE, this);
FUNCTION_LOG_PARAM(STRING, file);
FUNCTION_LOG_PARAM(BOOL, ignoreMissing);
FUNCTION_LOG_PARAM(BOOL, followLink);
FUNCTION_LOG_END();

ASSERT(this != NULL);
@@ -29,7 +29,7 @@ StorageDriverRemote *storageDriverRemoteNew(
Functions
***********************************************************************************************************************************/
bool storageDriverRemoteExists(StorageDriverRemote *this, const String *path);
StorageInfo storageDriverRemoteInfo(StorageDriverRemote *this, const String *file, bool ignoreMissing);
StorageInfo storageDriverRemoteInfo(StorageDriverRemote *this, const String *file, bool ignoreMissing, bool followLink);
StringList *storageDriverRemoteList(StorageDriverRemote *this, const String *path, bool errorOnMissing, const String *expression);
StorageFileRead *storageDriverRemoteNewRead(StorageDriverRemote *this, const String *file, bool ignoreMissing);
StorageFileWrite *storageDriverRemoteNewWrite(
@@ -460,16 +460,18 @@ storageDriverS3Exists(StorageDriverS3 *this, const String *path)
File/path info
***********************************************************************************************************************************/
StorageInfo
storageDriverS3Info(StorageDriverS3 *this, const String *file, bool ignoreMissing)
storageDriverS3Info(StorageDriverS3 *this, const String *file, bool ignoreMissing, bool followLink)
{
FUNCTION_LOG_BEGIN(logLevelDebug);
FUNCTION_LOG_PARAM(STORAGE_DRIVER_S3, this);
FUNCTION_LOG_PARAM(STRING, file);
FUNCTION_LOG_PARAM(BOOL, ignoreMissing);
FUNCTION_LOG_PARAM(BOOL, followLink);
FUNCTION_LOG_END();

ASSERT(this != NULL);
ASSERT(file != NULL);
ASSERT(followLink == false);

THROW(AssertError, "NOT YET IMPLEMENTED");

@@ -38,7 +38,7 @@ StorageDriverS3 *storageDriverS3New(
Functions
***********************************************************************************************************************************/
bool storageDriverS3Exists(StorageDriverS3 *this, const String *path);
StorageInfo storageDriverS3Info(StorageDriverS3 *this, const String *file, bool ignoreMissing);
StorageInfo storageDriverS3Info(StorageDriverS3 *this, const String *file, bool ignoreMissing, bool followLink);
StringList *storageDriverS3List(StorageDriverS3 *this, const String *path, bool errorOnMissing, const String *expression);
StorageFileRead *storageDriverS3NewRead(StorageDriverS3 *this, const String *file, bool ignoreMissing);
StorageFileWrite *storageDriverS3NewWrite(
@@ -236,6 +236,7 @@ storageInfo(const Storage *this, const String *fileExp, StorageInfoParam param)
FUNCTION_LOG_PARAM(STORAGE, this);
FUNCTION_LOG_PARAM(STRING, fileExp);
FUNCTION_LOG_PARAM(BOOL, param.ignoreMissing);
FUNCTION_LOG_PARAM(BOOL, param.followLink);
FUNCTION_LOG_END();

ASSERT(this != NULL);
@@ -248,7 +249,7 @@ storageInfo(const Storage *this, const String *fileExp, StorageInfoParam param)
String *file = storagePathNP(this, fileExp);

// Call driver function
result = this->interface.info(this->driver, file, param.ignoreMissing);
result = this->interface.info(this->driver, file, param.ignoreMissing, param.followLink);

// Dup the strings into the calling context
memContextSwitch(MEM_CONTEXT_OLD());
@@ -63,6 +63,7 @@ storageInfo
typedef struct StorageInfoParam
{
bool ignoreMissing;
bool followLink;
} StorageInfoParam;

#define storageInfoP(this, fileExp, ...) \
@@ -21,7 +21,7 @@ typedef String *(*StoragePathExpressionCallback)(const String *expression, const
Constructor
***********************************************************************************************************************************/
typedef bool (*StorageInterfaceExists)(void *driver, const String *path);
typedef StorageInfo (*StorageInterfaceInfo)(void *driver, const String *path, bool ignoreMissing);
typedef StorageInfo (*StorageInterfaceInfo)(void *driver, const String *path, bool ignoreMissing, bool followLink);
typedef bool (*StorageInterfaceInfoList)(
void *driver, const String *file, bool ignoreMissing, StorageInfoListCallback callback, void *callbackData);
typedef StringList *(*StorageInterfaceList)(void *driver, const String *path, bool errorOnMissing, const String *expression);
@@ -267,6 +267,16 @@ testRun(void)
TEST_RESULT_STR(strPtr(info.user), getpwuid(getuid())->pw_name, " check user");
TEST_RESULT_STR(strPtr(info.group), getgrgid(getgid())->gr_name, " check group");

TEST_ASSIGN(info, storageInfoP(storageTest, linkName, .followLink = true), "get info from path pointed to by link");
TEST_RESULT_PTR(info.name, NULL, " name is not set");
TEST_RESULT_BOOL(info.exists, true, " check exists");
TEST_RESULT_INT(info.type, storageTypePath, " check type");
TEST_RESULT_INT(info.size, 0, " check size");
TEST_RESULT_INT(info.mode, 0777, " check mode");
TEST_RESULT_STR(strPtr(info.linkDestination), NULL, " check link destination");
TEST_RESULT_STR(strPtr(info.user), "root", " check user");
TEST_RESULT_STR(strPtr(info.group), "root", " check group");

storageRemoveP(storageTest, linkName, .errorOnMissing = true);

// -------------------------------------------------------------------------------------------------------------------------

0 comments on commit 62b48d8

Please sign in to comment.
You can’t perform that action at this time.