diff --git a/src/include/switch_types.h b/src/include/switch_types.h index 9302ad5c3d..9d8445a1e7 100644 --- a/src/include/switch_types.h +++ b/src/include/switch_types.h @@ -1937,7 +1937,8 @@ typedef enum { SWITCH_FILE_NOMUX = (1 << 17), SWITCH_FILE_BREAK_ON_CHANGE = (1 << 18), SWITCH_FILE_FLAG_VIDEO = (1 << 19), - SWITCH_FILE_FLAG_VIDEO_EOF = (1 << 20) + SWITCH_FILE_FLAG_VIDEO_EOF = (1 << 20), + SWITCH_FILE_PRE_CLOSED = (1 << 21) } switch_file_flag_enum_t; typedef uint32_t switch_file_flag_t; diff --git a/src/switch_core_file.c b/src/switch_core_file.c index c49b151b01..717233ce55 100644 --- a/src/switch_core_file.c +++ b/src/switch_core_file.c @@ -915,6 +915,7 @@ SWITCH_DECLARE(switch_status_t) switch_core_file_pre_close(switch_file_handle_t } switch_clear_flag_locked(fh, SWITCH_FILE_OPEN); + switch_set_flag_locked(fh, SWITCH_FILE_PRE_CLOSED); if (fh->file_interface->file_pre_close) { status = fh->file_interface->file_pre_close(fh); @@ -929,10 +930,12 @@ SWITCH_DECLARE(switch_status_t) switch_core_file_close(switch_file_handle_t *fh) if (switch_test_flag(fh, SWITCH_FILE_OPEN)) { status = switch_core_file_pre_close(fh); - } else { + } else if (!switch_test_flag(fh, SWITCH_FILE_PRE_CLOSED)) { return SWITCH_STATUS_FALSE; } + switch_clear_flag_locked(fh, SWITCH_FILE_PRE_CLOSED); + fh->file_interface->file_close(fh); if (fh->params) { diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am index 79df35188f..f3e9a7d234 100644 --- a/tests/unit/Makefile.am +++ b/tests/unit/Makefile.am @@ -1,6 +1,6 @@ include $(top_srcdir)/build/modmake.rulesam -noinst_PROGRAMS = switch_event switch_hash switch_ivr_originate switch_utils switch_core switch_console switch_vpx \ +noinst_PROGRAMS = switch_event switch_hash switch_ivr_originate switch_utils switch_core switch_console switch_vpx switch_core_file \ switch_ivr_play_say switch_core_codec noinst_PROGRAMS+= switch_core_video AM_LDFLAGS = -avoid-version -no-undefined $(SWITCH_AM_LDFLAGS) $(openssl_LIBS) diff --git a/tests/unit/switch_core_file.c b/tests/unit/switch_core_file.c new file mode 100644 index 0000000000..34edf7d7c0 --- /dev/null +++ b/tests/unit/switch_core_file.c @@ -0,0 +1,133 @@ +/* + * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * Copyright (C) 2005-2018, Anthony Minessale II + * + * Version: MPL 1.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application + * + * The Initial Developer of the Original Code is + * Anthony Minessale II + * Portions created by the Initial Developer are Copyright (C) + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Chris Rienzo + * Seven Du + * Dragos Oancea + * + * switch_core_file.c -- tests file core functions + * + */ +#include +#include + +#include + +FST_CORE_BEGIN("./conf") +{ + FST_SUITE_BEGIN(switch_core_file) + { + FST_SETUP_BEGIN() + { + } + FST_SETUP_END() + + FST_TEARDOWN_BEGIN() + { + } + FST_TEARDOWN_END() + + FST_TEST_BEGIN(test_switch_core_file_close) + { + switch_file_handle_t fh = { 0 }; + switch_status_t status = SWITCH_STATUS_FALSE; + static char filename[] = "/tmp/fs_unit_test.wav"; + static char hdr[] = /*simplest wav file*/ + "\x52\x49\x46\x46" + "\x24\x00\x00\x00" + "\x57\x41\x56\x45" + "\x66\x6d\x74\x20" + "\x10\x00\x00\x00" + "\x01\x00\x01\x00" + "\x44\xac\x00\x00" + "\x88\x58\x01\x00" + "\x02\x00\x10\x00" + "\x64\x61\x74\x61" + "\x00\x00"; + FILE *f = NULL; + + f = fopen(filename, "w"); + fst_check(f != NULL); + fwrite(hdr, 1, sizeof(hdr) ,f); + fclose(f); + + status = switch_core_file_open(&fh, filename, 1, 8000, SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL); + fst_check(status == SWITCH_STATUS_SUCCESS); + fst_check(switch_test_flag(&fh, SWITCH_FILE_OPEN)); + status = switch_core_file_pre_close(&fh); + fst_check(!switch_test_flag(&fh, SWITCH_FILE_OPEN)); + fst_check(switch_test_flag(&fh, SWITCH_FILE_PRE_CLOSED)); + fst_check(status == SWITCH_STATUS_SUCCESS); + status = switch_core_file_close(&fh); + fst_check(status == SWITCH_STATUS_SUCCESS); + fst_check(!switch_test_flag(&fh, SWITCH_FILE_PRE_CLOSED)); + + status = switch_core_file_close(&fh); + fst_check(status == SWITCH_STATUS_FALSE); + + unlink(filename); + } + FST_TEST_END() + FST_TEST_BEGIN(test_switch_core_file_no_pre_close) + { + switch_file_handle_t fh = { 0 }; + switch_status_t status = SWITCH_STATUS_FALSE; + static char filename[] = "/tmp/fs_unit_test.wav"; + static char hdr[] = /*simplest wav file*/ + "\x52\x49\x46\x46" + "\x24\x00\x00\x00" + "\x57\x41\x56\x45" + "\x66\x6d\x74\x20" + "\x10\x00\x00\x00" + "\x01\x00\x01\x00" + "\x44\xac\x00\x00" + "\x88\x58\x01\x00" + "\x02\x00\x10\x00" + "\x64\x61\x74\x61" + "\x00\x00"; + FILE *f = NULL; + + f = fopen(filename, "w"); + fst_check(f != NULL); + fwrite(hdr, 1, sizeof(hdr) ,f); + fclose(f); + + status = switch_core_file_open(&fh, filename, 1, 8000, SWITCH_FILE_FLAG_READ | SWITCH_FILE_DATA_SHORT, NULL); + fst_check(status == SWITCH_STATUS_SUCCESS); + fst_check(switch_test_flag(&fh, SWITCH_FILE_OPEN)); + status = switch_core_file_close(&fh); + fst_check(status == SWITCH_STATUS_SUCCESS); + fst_check(!switch_test_flag(&fh, SWITCH_FILE_PRE_CLOSED)); + + status = switch_core_file_close(&fh); + fst_check(status == SWITCH_STATUS_FALSE); + + unlink(filename); + } + FST_TEST_END() + + } + FST_SUITE_END() +} +FST_CORE_END()