Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/os/inc/osapi-file.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ int32 OS_OpenCreate(osal_id_t *filedes, const char *path, int32 flags, int32 acc
* This closes regular file handles and any other file-like resource, such as
* network streams or pipes.
*
* Once the native close is attempted, the OSAL handle is released even if
* an error is reported. Do not retry the close using the same handle.
*
* @param[in] filedes The handle ID to operate on
*
* @return Execution status, see @ref OSReturnCodes
Expand Down
7 changes: 3 additions & 4 deletions src/os/portable/os-impl-posix-io.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ int32 OS_GenericClose_Impl(const OS_object_token_t *token)
if (result < 0)
{
/*
* close() can technically fail for various reasons, but
* there isn't much recourse if this call fails. Just log
* the failure for debugging.
* close() can report delayed I/O errors. Report the failure
* to the caller, but do not retry the native close.
*
* POSIX also does not specify the state of the filehandle
* after a close() with an error.
Expand All @@ -87,7 +86,7 @@ int32 OS_GenericClose_Impl(const OS_object_token_t *token)
OS_DEBUG("close: %s\n", strerror(errno));
}
impl->fd = -1;
return OS_SUCCESS;
return (result < 0) ? OS_ERROR : OS_SUCCESS;
}

/*----------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions src/os/shared/inc/os-shared-file.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ int32 OS_GenericWrite_Impl(const OS_object_token_t *token, const void *buffer, s

Purpose: Close a file descriptor
This may be a normal file or a socket/pipe
The implementation must invalidate the native handle even on error.

Returns: OS_SUCCESS on success, or relevant error code
------------------------------------------------------------------*/
Expand Down
17 changes: 13 additions & 4 deletions src/os/shared/src/osapi-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ int32 OS_close(osal_id_t filedes)
{
return_code = OS_GenericClose_Impl(&token);

/* Complete the operation via the common routine */
return_code = OS_ObjectIdFinalizeDelete(return_code, &token);
/* The native handle is no longer usable even when close reports an I/O error. */
OS_ObjectIdFinalizeDelete(OS_SUCCESS, &token);
}

return return_code;
Expand Down Expand Up @@ -462,6 +462,7 @@ int32 OS_rename(const char *old_filename, const char *new_filename)
int32 OS_cp(const char *src, const char *dest)
{
int32 return_code;
int32 close_code;
int32 rd_size;
int32 wr_size;
int32 wr_total;
Expand Down Expand Up @@ -508,11 +509,19 @@ int32 OS_cp(const char *src, const char *dest)

if (OS_ObjectIdDefined(file1))
{
OS_close(file1);
close_code = OS_close(file1);
if (return_code == OS_SUCCESS)
{
return_code = close_code;
}
}
if (OS_ObjectIdDefined(file2))
{
OS_close(file2);
close_code = OS_close(file2);
if (return_code == OS_SUCCESS)
{
return_code = close_code;
}
}

return return_code;
Expand Down
5 changes: 3 additions & 2 deletions src/unit-test-coverage/portable/src/coveragetest-posix-io.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ void Test_OS_GenericClose_Impl(void)

/*
* Test path where underlying close() fails.
* Should still return success.
* Report the error without retrying close().
*/
UT_SetDefaultReturnValue(UT_KEY(OCS_close), -1);
OSAPI_TEST_FUNCTION_RC(OS_GenericClose_Impl, (&token), OS_SUCCESS);
OSAPI_TEST_FUNCTION_RC(OS_GenericClose_Impl, (&token), OS_ERROR);
UtAssert_STUB_COUNT(OCS_close, 2);
}

void Test_OS_GenericSeek_Impl(void)
Expand Down
35 changes: 35 additions & 0 deletions src/unit-test-coverage/shared/src/coveragetest-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@

#include "OCS_string.h"

static int32 UT_Hook_FinalizeClose(void *UserObj, int32 StubRetcode, uint32 CallCount, const UT_StubContext_t *Context)
{
int32 operation_status = UT_Hook_GetArgValueByName(Context, "operation_status", int32);

UtAssert_INT32_EQ(operation_status, OS_SUCCESS);
return operation_status;
}

/*
**********************************************************************************
** PUBLIC API FUNCTIONS
Expand Down Expand Up @@ -89,8 +97,14 @@ void Test_OS_close(void)
* Test Case For:
* int32 OS_close (uint32 filedes)
*/
UT_SetHookFunction(UT_KEY(OS_ObjectIdFinalizeDelete), UT_Hook_FinalizeClose, NULL);
OSAPI_TEST_FUNCTION_RC(OS_close(UT_OBJID_1), OS_SUCCESS);

/* A native close error must reach the caller while still releasing the OSAL ID. */
UT_SetDefaultReturnValue(UT_KEY(OS_GenericClose_Impl), OS_ERROR);
OSAPI_TEST_FUNCTION_RC(OS_close(UT_OBJID_1), OS_ERROR);
UtAssert_STUB_COUNT(OS_ObjectIdFinalizeDelete, 2);

UT_SetDefaultReturnValue(UT_KEY(OS_ObjectIdGetById), OS_ERR_INVALID_ID);
OSAPI_TEST_FUNCTION_RC(OS_close(UT_OBJID_1), OS_ERR_INVALID_ID);
}
Expand Down Expand Up @@ -268,6 +282,21 @@ void Test_OS_cp(void)
UT_SetDataBuffer(UT_KEY(OS_GenericWrite_Impl), WriteBuf, sizeof(WriteBuf), false);
OSAPI_TEST_FUNCTION_RC(OS_cp("/cf/file1", "/cf/file2"), OS_SUCCESS);

/* Close errors must fail the copy, with both handles closed exactly once. */
UT_ResetState(UT_KEY(OS_GenericClose_Impl));
UT_SetDeferredRetcode(UT_KEY(OS_GenericClose_Impl), 1, -777);
OSAPI_TEST_FUNCTION_RC(OS_cp("/cf/file1", "/cf/file2"), -777);
UtAssert_STUB_COUNT(OS_GenericClose_Impl, 2);

UT_ResetState(UT_KEY(OS_GenericClose_Impl));
UT_SetDeferredRetcode(UT_KEY(OS_GenericClose_Impl), 2, -888);
OSAPI_TEST_FUNCTION_RC(OS_cp("/cf/file1", "/cf/file2"), -888);
UtAssert_STUB_COUNT(OS_GenericClose_Impl, 2);

/* Preserve the first failure, including an earlier read/write error. */
UT_SetDefaultReturnValue(UT_KEY(OS_GenericClose_Impl), -777);
OSAPI_TEST_FUNCTION_RC(OS_cp("/cf/file1", "/cf/file2"), -777);

UT_SetDeferredRetcode(UT_KEY(OS_GenericRead_Impl), 1, -444);
OSAPI_TEST_FUNCTION_RC(OS_cp("/cf/file1", "/cf/file2"), -444);

Expand All @@ -292,6 +321,12 @@ void Test_OS_mv(void)
UT_SetDefaultReturnValue(UT_KEY(OS_FileRename_Impl), OS_ERROR);
OSAPI_TEST_FUNCTION_RC(OS_mv("/cf/file1", "/cf/file2"), OS_SUCCESS);

/* A failed destination close must not delete the source in the fallback path. */
UT_ResetState(UT_KEY(OS_FileRemove_Impl));
UT_SetDeferredRetcode(UT_KEY(OS_GenericClose_Impl), 2, OS_ERROR);
OSAPI_TEST_FUNCTION_RC(OS_mv("/cf/file1", "/cf/file2"), OS_ERROR);
UtAssert_STUB_COUNT(OS_FileRemove_Impl, 0);

/* Fail the OS_cp/OS_OpenCreate */
UT_SetDefaultReturnValue(UT_KEY(OS_TranslatePathWithSize), OS_ERROR);
OSAPI_TEST_FUNCTION_RC(OS_mv("/cf/file1", "/cf/file2"), OS_ERROR);
Expand Down