14a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project/*---------------------------------------------------------------------------*
24a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project *  PFileSystemUNIXImpl.c  *
34a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project *                                                                           *
44a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project *  Copyright 2007, 2008 Nuance Communciations, Inc.                               *
54a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project *                                                                           *
64a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project *  Licensed under the Apache License, Version 2.0 (the 'License');          *
74a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project *  you may not use this file except in compliance with the License.         *
84a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project *                                                                           *
94a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project *  You may obtain a copy of the License at                                  *
104a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0                           *
114a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project *                                                                           *
124a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project *  Unless required by applicable law or agreed to in writing, software      *
134a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project *  distributed under the License is distributed on an 'AS IS' BASIS,        *
144a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
154a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project *  See the License for the specific language governing permissions and      *
164a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project *  limitations under the License.                                           *
174a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project *                                                                           *
184a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project *---------------------------------------------------------------------------*/
194a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project
204a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project#include "PANSIFileImpl.h"
214a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project#include "PANSIFileSystemImpl.h"
224a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project#include "PFileSystem.h"
234a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project#include "PFileSystemImpl.h"
244a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project#include "PANSIFileSystem.h"
254a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project#include "phashtable.h"
264a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project#include "plog.h"
274a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project#include "pmemory.h"
284a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project
294a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project
304a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project#ifdef USE_THREAD
314a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	/* Prototype of private function */
324a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	PORTABLE_API ESR_ReturnCode PtrdFlush();
334a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project#endif
344a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project
354a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project
364a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project/**
374a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project * Initializes STDIN, STDOUT, STDERR.
384a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project */
394a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source ProjectESR_ReturnCode PFileSystemInitializeStreamsImpl(void)
404a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project{
414a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	ESR_ReturnCode rc;
424a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	PANSIFileImpl* impl;
434a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project#ifdef USE_THREAD
444a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	ESR_BOOL threadingEnabled;
454a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project#endif
464a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	ESR_BOOL isLittleEndian;
474a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	PANSIFileSystemImpl* ANSIImpl = NULL;
484a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project
494a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project#if __BYTE_ORDER==__LITTLE_ENDIAN
504a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	isLittleEndian = ESR_TRUE;
514a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project#else
524a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	isLittleEndian = ESR_FALSE;
534a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project#endif
544a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	CHKLOG(rc, PANSIFileSystemCreate());
554a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	ANSIImpl = (PANSIFileSystemImpl*) PANSIFileSystemSingleton;
564a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	CHKLOG(rc, PMemSetLogEnabled(ESR_FALSE));
574a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	CHKLOG(rc, PHashTablePutValue(PFileSystemPathMap, L("/"), PANSIFileSystemSingleton, NULL));
584a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	CHKLOG(rc, PHashTablePutValue(ANSIImpl->directoryMap, L("/"), L("/"), NULL));
594a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	CHKLOG(rc, PANSIFileSystemSingleton->createPFile(PANSIFileSystemSingleton, L("/dev/stdin"), isLittleEndian, &PSTDIN));
604a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	impl = (PANSIFileImpl*) PSTDIN;
614a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	impl->value = stdin;
624a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project
634a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	CHKLOG(rc, PANSIFileSystemSingleton->createPFile(PANSIFileSystemSingleton, L("/dev/stdout"), isLittleEndian, &PSTDOUT));
644a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	impl = (PANSIFileImpl*) PSTDOUT;
654a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project		setvbuf(stdout, NULL, _IONBF, 0);
664a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	impl->value = stdout;
674a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project
684a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	CHKLOG(rc, PANSIFileSystemSingleton->createPFile(PANSIFileSystemSingleton, L("/dev/stderr"), isLittleEndian, &PSTDERR));
694a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	impl = (PANSIFileImpl*) PSTDERR;
704a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project		setvbuf(stderr, NULL, _IONBF, 0);
714a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	impl->value = stderr;
724a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project
734a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	#ifdef USE_THREAD
744a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	/* Have STDERR and STDOUT share the same lock */
754a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	CHKLOG(rc, PtrdIsEnabled(&threadingEnabled));
764a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	if (threadingEnabled)
774a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	{
784a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project		CHKLOG(rc, PtrdMonitorDestroy(impl->Interface.lock));
794a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project		impl->Interface.lock = ((PANSIFileImpl*) PSTDOUT)->Interface.lock;
804a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	}
814a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	#endif
824a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	CHKLOG(rc, PHashTableRemoveValue(PFileSystemPathMap, L("/"), NULL));
834a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	CHKLOG(rc, PHashTableRemoveValue(ANSIImpl->directoryMap, L("/"), NULL));
844a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	CHKLOG(rc, PMemSetLogEnabled(ESR_TRUE));
854a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	return ESR_SUCCESS;
864a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source ProjectCLEANUP:
874a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	PHashTableRemoveValue(PFileSystemPathMap, L("/"), NULL);
884a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	if (ANSIImpl!=NULL)
894a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project		PHashTableRemoveValue(ANSIImpl->directoryMap, L("/"), NULL);
904a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	PMemSetLogEnabled(ESR_TRUE);
914a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	return rc;
924a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project}
934a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project
944a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source ProjectESR_ReturnCode PFileSystemShutdownStreamsImpl(void)
954a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project{
964a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	ESR_ReturnCode rc;
974a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	PANSIFileImpl* impl;
984a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project
994a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	/* It is illegal to log to file after the file system has shutdown so we do it now */
1004a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project#ifdef USE_THREAD
1014a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	PtrdFlush();
1024a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project#endif
1034a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	PMemDumpLogFile();
1044a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project
1054a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	if (PSTDIN!=NULL)
1064a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	{
1074a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project		CHKLOG(rc, PFileFlush(PSTDIN));
1084a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project		impl = (PANSIFileImpl*) PSTDIN;
1094a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project		impl->value = NULL;
1104a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project		CHKLOG(rc, PFileDestroy(PSTDIN));
1114a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project		PSTDIN = NULL;
1124a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	}
1134a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	if (PSTDOUT!=NULL)
1144a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	{
1154a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project#ifdef USE_THREAD
1164a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project		if (PSTDERR!=NULL)
1174a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project		{
1184a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project			/* stdout, stderr share the same lock, only one of them should destroy it */
1194a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project			PFileImpl* impl = (PFileImpl*) PSTDOUT;
1204a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project
1214a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project			impl->lock = NULL;
1224a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project		}
1234a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project#endif
1244a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project		CHKLOG(rc, PFileFlush(PSTDOUT));
1254a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project		impl = (PANSIFileImpl*) PSTDOUT;
1264a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project		impl->value = NULL;
1274a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project		CHKLOG(rc, PFileDestroy(PSTDOUT));
1284a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project		PSTDOUT = NULL;
1294a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	}
1304a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	if (PSTDERR!=NULL)
1314a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	{
1324a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project		CHKLOG(rc, PFileFlush(PSTDERR));
1334a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project		impl = (PANSIFileImpl*) PSTDERR;
1344a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project		impl->value = NULL;
1354a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project		CHKLOG(rc, PFileDestroy(PSTDERR));
1364a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project		PSTDERR = NULL;
1374a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	}
1384a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	CHKLOG(rc, PANSIFileSystemDestroy());
1394a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	return ESR_SUCCESS;
1404a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source ProjectCLEANUP:
1414a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project	return rc;
1424a68b3365c8c50aa93505e99ead2565ab73dcdb0The Android Open Source Project}
143