Skip to content
Permalink
Browse files

Expose handle (file descriptor) from IoWrite when applicable.

This is a followup to dee90d3 which exposed file handles for IoRead.

Also expose handle for StorageDriverPosixFileRead missed in dee90d3.
  • Loading branch information...
dwsteele committed Apr 29, 2019
1 parent e2141cc commit 60edadf71dbbe6c82c5a83bcebee36a16b42ed88
@@ -49,6 +49,10 @@
<p>Add <code>unsigned int</code> <code>Variant</code> type and update code to use it.</p>
</release-item>

<release-item>
<p>Expose handle (file descriptor) from <code>IoWrite</code> when applicable.</p>
</release-item>

<release-item>
<p>Add <code>iniSave()</code> and <code>iniMove()</code> to <code>Ini</code> object.</p>
</release-item>
@@ -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
***********************************************************************************************************************************/
@@ -27,6 +27,7 @@ IoHandleWrite *ioHandleWriteMove(IoHandleWrite *this, MemContext *parentNew);
/***********************************************************************************************************************************
Getters
***********************************************************************************************************************************/
int ioHandleWriteHandle(const IoHandleWrite *this);
IoWrite *ioHandleWriteIo(const IoHandleWrite *this);

/***********************************************************************************************************************************
@@ -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
***********************************************************************************************************************************/
@@ -32,6 +32,7 @@ Getters/Setters
***********************************************************************************************************************************/
const IoFilterGroup *ioWriteFilterGroup(const IoWrite *this);
void ioWriteFilterGroupSet(IoWrite *this, IoFilterGroup *filterGroup);
int ioWriteHandle(const IoWrite *this);

/***********************************************************************************************************************************
Destructor
@@ -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;
@@ -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?
***********************************************************************************************************************************/
@@ -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);
@@ -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
***********************************************************************************************************************************/
@@ -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);
@@ -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");
@@ -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");

0 comments on commit 60edadf

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