From 60edadf71dbbe6c82c5a83bcebee36a16b42ed88 Mon Sep 17 00:00:00 2001 From: David Steele Date: Mon, 29 Apr 2019 14:54:49 -0400 Subject: [PATCH] Expose handle (file descriptor) from IoWrite when applicable. This is a followup to dee90d3e which exposed file handles for IoRead. Also expose handle for StorageDriverPosixFileRead missed in dee90d3e. --- doc/xml/release.xml | 4 ++++ src/common/io/handleWrite.c | 18 +++++++++++++++++- src/common/io/handleWrite.h | 1 + src/common/io/write.c | 17 +++++++++++++++++ src/common/io/write.h | 1 + src/common/io/write.intern.h | 2 ++ src/storage/driver/posix/fileRead.c | 16 ++++++++++++++++ src/storage/driver/posix/fileRead.h | 1 + src/storage/driver/posix/fileWrite.c | 16 ++++++++++++++++ src/storage/driver/posix/fileWrite.h | 1 + test/src/module/common/ioTest.c | 1 + test/src/module/storage/posixTest.c | 4 ++++ 12 files changed, 81 insertions(+), 1 deletion(-) diff --git a/doc/xml/release.xml b/doc/xml/release.xml index 721aa7d33..f829a9999 100644 --- a/doc/xml/release.xml +++ b/doc/xml/release.xml @@ -49,6 +49,10 @@

Add unsigned int Variant type and update code to use it.

+ +

Expose handle (file descriptor) from IoWrite when applicable.

+
+

Add iniSave() and iniMove() to Ini object.

diff --git a/src/common/io/handleWrite.c b/src/common/io/handleWrite.c index 344ae572c..57c69ee69 100644 --- a/src/common/io/handleWrite.c +++ b/src/common/io/handleWrite.c @@ -38,7 +38,8 @@ ioHandleWriteNew(const String *name, int handle) { this = memNew(sizeof(IoHandleWrite)); this->memContext = memContextCurrent(); - this->io = ioWriteNewP(this, .write = (IoWriteInterfaceWrite)ioHandleWrite); + this->io = ioWriteNewP( + this, .handle = (IoWriteInterfaceHandle)ioHandleWriteHandle, .write = (IoWriteInterfaceWrite)ioHandleWrite); this->name = strDup(name); this->handle = handle; } @@ -86,6 +87,21 @@ ioHandleWriteMove(IoHandleWrite *this, MemContext *parentNew) FUNCTION_TEST_RETURN(this); } +/*********************************************************************************************************************************** +Get handle (file descriptor) +***********************************************************************************************************************************/ +int +ioHandleWriteHandle(const IoHandleWrite *this) +{ + FUNCTION_TEST_BEGIN(); + FUNCTION_TEST_PARAM(IO_HANDLE_WRITE, this); + FUNCTION_TEST_END(); + + ASSERT(this != NULL); + + FUNCTION_TEST_RETURN(this->handle); +} + /*********************************************************************************************************************************** Get io interface ***********************************************************************************************************************************/ diff --git a/src/common/io/handleWrite.h b/src/common/io/handleWrite.h index 3461daf91..0cf3f81b4 100644 --- a/src/common/io/handleWrite.h +++ b/src/common/io/handleWrite.h @@ -27,6 +27,7 @@ IoHandleWrite *ioHandleWriteMove(IoHandleWrite *this, MemContext *parentNew); /*********************************************************************************************************************************** Getters ***********************************************************************************************************************************/ +int ioHandleWriteHandle(const IoHandleWrite *this); IoWrite *ioHandleWriteIo(const IoHandleWrite *this); /*********************************************************************************************************************************** diff --git a/src/common/io/write.c b/src/common/io/write.c index 465811e43..98af6c351 100644 --- a/src/common/io/write.c +++ b/src/common/io/write.c @@ -287,6 +287,23 @@ ioWriteFilterGroupSet(IoWrite *this, IoFilterGroup *filterGroup) FUNCTION_LOG_RETURN_VOID(); } +/*********************************************************************************************************************************** +Handle (file descriptor) for the write object + +No all write objects have a handle and -1 will be returned in that case. +***********************************************************************************************************************************/ +int +ioWriteHandle(const IoWrite *this) +{ + FUNCTION_LOG_BEGIN(logLevelTrace); + FUNCTION_LOG_PARAM(IO_WRITE, this); + FUNCTION_LOG_END(); + + ASSERT(this != NULL); + + FUNCTION_LOG_RETURN(INT, this->interface.handle == NULL ? -1 : this->interface.handle(this->driver)); +} + /*********************************************************************************************************************************** Free the object ***********************************************************************************************************************************/ diff --git a/src/common/io/write.h b/src/common/io/write.h index f91f970b7..200e8d01f 100644 --- a/src/common/io/write.h +++ b/src/common/io/write.h @@ -32,6 +32,7 @@ Getters/Setters ***********************************************************************************************************************************/ const IoFilterGroup *ioWriteFilterGroup(const IoWrite *this); void ioWriteFilterGroupSet(IoWrite *this, IoFilterGroup *filterGroup); +int ioWriteHandle(const IoWrite *this); /*********************************************************************************************************************************** Destructor diff --git a/src/common/io/write.intern.h b/src/common/io/write.intern.h index b4ff79799..7113a7adc 100644 --- a/src/common/io/write.intern.h +++ b/src/common/io/write.intern.h @@ -10,12 +10,14 @@ IO Write Interface Internal Constructor ***********************************************************************************************************************************/ typedef void (*IoWriteInterfaceClose)(void *driver); +typedef int (*IoWriteInterfaceHandle)(void *driver); typedef void (*IoWriteInterfaceOpen)(void *driver); typedef void (*IoWriteInterfaceWrite)(void *driver, const Buffer *buffer); typedef struct IoWriteInterface { IoWriteInterfaceClose close; + IoWriteInterfaceHandle handle; IoWriteInterfaceOpen open; IoWriteInterfaceWrite write; } IoWriteInterface; diff --git a/src/storage/driver/posix/fileRead.c b/src/storage/driver/posix/fileRead.c index 40d7ff00a..ac9cc5fb8 100644 --- a/src/storage/driver/posix/fileRead.c +++ b/src/storage/driver/posix/fileRead.c @@ -65,6 +65,7 @@ storageDriverPosixFileReadNew(StorageDriverPosix *storage, const String *name, b this->io = ioReadNewP( this, .eof = (IoReadInterfaceEof)storageDriverPosixFileReadEof, .close = (IoReadInterfaceClose)storageDriverPosixFileReadClose, + .handle = (IoReadInterfaceHandle)storageDriverPosixFileReadHandle, .open = (IoReadInterfaceOpen)storageDriverPosixFileReadOpen, .read = (IoReadInterfaceRead)storageDriverPosixFileRead); } MEM_CONTEXT_NEW_END(); @@ -179,6 +180,21 @@ storageDriverPosixFileReadEof(const StorageDriverPosixFileRead *this) FUNCTION_TEST_RETURN(this->eof); } +/*********************************************************************************************************************************** +Get handle (file descriptor) +***********************************************************************************************************************************/ +int +storageDriverPosixFileReadHandle(const StorageDriverPosixFileRead *this) +{ + FUNCTION_TEST_BEGIN(); + FUNCTION_TEST_PARAM(STORAGE_DRIVER_POSIX_FILE_READ, this); + FUNCTION_TEST_END(); + + ASSERT(this != NULL); + + FUNCTION_TEST_RETURN(this->handle); +} + /*********************************************************************************************************************************** Should a missing file be ignored? ***********************************************************************************************************************************/ diff --git a/src/storage/driver/posix/fileRead.h b/src/storage/driver/posix/fileRead.h index 9ef1e6861..925159c98 100644 --- a/src/storage/driver/posix/fileRead.h +++ b/src/storage/driver/posix/fileRead.h @@ -30,6 +30,7 @@ void storageDriverPosixFileReadClose(StorageDriverPosixFileRead *this); Getters ***********************************************************************************************************************************/ bool storageDriverPosixFileReadEof(const StorageDriverPosixFileRead *this); +int storageDriverPosixFileReadHandle(const StorageDriverPosixFileRead *this); bool storageDriverPosixFileReadIgnoreMissing(const StorageDriverPosixFileRead *this); StorageFileRead *storageDriverPosixFileReadInterface(const StorageDriverPosixFileRead *this); IoRead *storageDriverPosixFileReadIo(const StorageDriverPosixFileRead *this); diff --git a/src/storage/driver/posix/fileWrite.c b/src/storage/driver/posix/fileWrite.c index 93cf9c591..1fc4dc4ec 100644 --- a/src/storage/driver/posix/fileWrite.c +++ b/src/storage/driver/posix/fileWrite.c @@ -91,6 +91,7 @@ storageDriverPosixFileWriteNew( this->io = ioWriteNewP( this, .close = (IoWriteInterfaceClose)storageDriverPosixFileWriteClose, + .handle = (IoWriteInterfaceHandle)storageDriverPosixFileWriteHandle, .open = (IoWriteInterfaceOpen)storageDriverPosixFileWriteOpen, .write = (IoWriteInterfaceWrite)storageDriverPosixFileWrite); @@ -239,6 +240,21 @@ storageDriverPosixFileWriteCreatePath(const StorageDriverPosixFileWrite *this) FUNCTION_TEST_RETURN(this->createPath); } +/*********************************************************************************************************************************** +Get handle (file descriptor) +***********************************************************************************************************************************/ +int +storageDriverPosixFileWriteHandle(const StorageDriverPosixFileWrite *this) +{ + FUNCTION_TEST_BEGIN(); + FUNCTION_TEST_PARAM(STORAGE_DRIVER_POSIX_FILE_WRITE, this); + FUNCTION_TEST_END(); + + ASSERT(this != NULL); + + FUNCTION_TEST_RETURN(this->handle); +} + /*********************************************************************************************************************************** Get interface ***********************************************************************************************************************************/ diff --git a/src/storage/driver/posix/fileWrite.h b/src/storage/driver/posix/fileWrite.h index a1dda5e48..d217474f8 100644 --- a/src/storage/driver/posix/fileWrite.h +++ b/src/storage/driver/posix/fileWrite.h @@ -35,6 +35,7 @@ Getters bool storageDriverPosixFileWriteAtomic(const StorageDriverPosixFileWrite *this); bool storageDriverPosixFileWriteCreatePath(const StorageDriverPosixFileWrite *this); mode_t storageDriverPosixFileWriteModeFile(const StorageDriverPosixFileWrite *this); +int storageDriverPosixFileWriteHandle(const StorageDriverPosixFileWrite *this); StorageFileWrite* storageDriverPosixFileWriteInterface(const StorageDriverPosixFileWrite *this); IoWrite *storageDriverPosixFileWriteIo(const StorageDriverPosixFileWrite *this); mode_t storageDriverPosixFileWriteModePath(const StorageDriverPosixFileWrite *this); diff --git a/test/src/module/common/ioTest.c b/test/src/module/common/ioTest.c index b5c04c462..39bad0f48 100644 --- a/test/src/module/common/ioTest.c +++ b/test/src/module/common/ioTest.c @@ -497,6 +497,7 @@ testRun(void) MEM_CONTEXT_TEMP_END(); ioWriteOpen(ioHandleWriteIo(write)); + TEST_RESULT_INT(ioWriteHandle(ioHandleWriteIo(write)), write->handle, "check write handle"); // Write a line to be read TEST_RESULT_VOID(ioWriteStrLine(ioHandleWriteIo(write), strNew("test string 1")), "write test string"); diff --git a/test/src/module/storage/posixTest.c b/test/src/module/storage/posixTest.c index 41fcd045d..9e03e9bb4 100644 --- a/test/src/module/storage/posixTest.c +++ b/test/src/module/storage/posixTest.c @@ -631,6 +631,8 @@ testRun(void) TEST_RESULT_INT(system(strPtr(strNewFmt("touch %s", strPtr(fileName)))), 0, "create read file"); TEST_RESULT_BOOL(ioReadOpen(storageFileReadIo(file)), true, " open file"); + TEST_RESULT_INT( + ioReadHandle(storageFileReadIo(file)), ((StorageDriverPosixFileRead *)file->driver)->handle, "check read handle"); TEST_RESULT_VOID(ioReadClose(storageFileReadIo(file)), " close file"); // ------------------------------------------------------------------------------------------------------------------------- @@ -655,6 +657,8 @@ testRun(void) TEST_ASSIGN(file, storageNewWriteNP(storageTest, fileName), "new write file (defaults)"); TEST_RESULT_VOID(ioWriteOpen(storageFileWriteIo(file)), " open file"); + TEST_RESULT_INT( + ioWriteHandle(storageFileWriteIo(file)), ((StorageDriverPosixFileWrite *)file->driver)->handle, "check write handle"); TEST_RESULT_VOID(ioWriteClose(storageFileWriteIo(file)), " close file"); TEST_RESULT_INT(storageInfoNP(storageTest, strPath(fileName)).mode, 0750, " check path mode"); TEST_RESULT_INT(storageInfoNP(storageTest, fileName).mode, 0640, " check file mode");