Beispiel #1
0
static oslFileError osl_setup_base_directory_impl_(
    rtl_uString*  pustrDirectoryURL,
    rtl_uString** ppustr_base_dir)
{
    rtl_uString* dir_url = 0;
    rtl_uString* dir     = 0;
    oslFileError error   = osl_File_E_None;

    if (pustrDirectoryURL)
        rtl_uString_assign(&dir_url, pustrDirectoryURL);
    else
        error = osl_getTempDirURL(&dir_url);

    if (osl_File_E_None == error)
    {
        error = osl_getSystemPathFromFileURL_Ex(dir_url, &dir);
        rtl_uString_release(dir_url);
    }

    if (osl_File_E_None == error)
    {
        rtl_uString_assign(ppustr_base_dir, dir);
        rtl_uString_release(dir);
    }

    return error;
}
Beispiel #2
0
oslFileError SAL_CALL osl_createTempFile(
    rtl_uString*   pustrDirectoryURL,
    oslFileHandle* pHandle,
    rtl_uString**  ppustrTempFileURL)
{
    rtl_uString*  base_directory     = 0;
    rtl_uString*  temp_file_name     = 0;
    oslFileHandle temp_file_handle;
    sal_Bool      b_delete_on_close;
    oslFileError  osl_error;

    osl_error = osl_setup_createTempFile_impl_(
        pustrDirectoryURL,
        pHandle,
        ppustrTempFileURL,
        &base_directory,
        &b_delete_on_close);

    if (osl_File_E_None != osl_error)
        return osl_error;

    osl_error = osl_create_temp_file_impl_(
        base_directory, &temp_file_handle, &temp_file_name);

    if (osl_File_E_None == osl_error)
    {
        rtl_uString* temp_file_url = 0;

        /* assuming this works */
        osl_getFileURLFromSystemPath(temp_file_name, &temp_file_url);

        if (b_delete_on_close)
        {
            osl_error = osl_removeFile(temp_file_url);

            if (osl_File_E_None == osl_error)
                *pHandle = temp_file_handle;
            else
                osl_closeFile(temp_file_handle);
        }
        else
        {
            if (pHandle)
                *pHandle = temp_file_handle;
            else
                osl_closeFile(temp_file_handle);

            rtl_uString_assign(ppustrTempFileURL, temp_file_url);
        }

        if (temp_file_url)
            rtl_uString_release(temp_file_url);

        if (temp_file_name)
            rtl_uString_release(temp_file_name);
    }

    if (base_directory)
        rtl_uString_release(base_directory);

    return osl_error;
}
Beispiel #3
0
static oslFileError osl_create_temp_file_impl_(
    const rtl_uString* pustr_base_directory,
    oslFileHandle* file_handle,
    rtl_uString** ppustr_temp_file_name)
{
    rtl_uString*        rand_name        = 0;
    sal_uInt32          len_base_dir     = 0;
    rtl_uString*        tmp_file_path    = 0;
    rtl_uString*        tmp_file_url     = 0;
    sal_Int32           capacity         = 0;
    oslFileError        osl_error        = osl_File_E_None;
    sal_Int32           offset_file_name;
    const sal_Unicode*  puchr;

    OSL_PRECOND(pustr_base_directory, "Invalid Parameter");
    OSL_PRECOND(file_handle, "Invalid Parameter");
    OSL_PRECOND(ppustr_temp_file_name, "Invalid Parameter");

    len_base_dir = rtl_uString_getLength(pustr_base_directory);

    rtl_uStringbuffer_newFromStr_WithLength(
        &tmp_file_path,
        rtl_uString_getStr((rtl_uString*)pustr_base_directory),
        len_base_dir);

    rtl_uStringbuffer_ensureCapacity(
        &tmp_file_path,
        &capacity,
        (len_base_dir + 1 + RAND_NAME_LENGTH));

    offset_file_name = len_base_dir;

    puchr = rtl_uString_getStr(tmp_file_path);

    /* ensure that the last character is a '/' */

    if ((sal_Unicode)'/' != puchr[len_base_dir - 1])
    {
        rtl_uStringbuffer_insert_ascii(
            &tmp_file_path,
            &capacity,
            len_base_dir,
            "/",
            1);

        offset_file_name++;
    }

    while(1) /* try until success */
    {
        osl_gen_random_name_impl_(&rand_name);

        rtl_uStringbuffer_insert(
            &tmp_file_path,
            &capacity,
            offset_file_name,
            rtl_uString_getStr(rand_name),
            rtl_uString_getLength(rand_name));

        osl_error = osl_getFileURLFromSystemPath(
            tmp_file_path, &tmp_file_url);

        if (osl_File_E_None == osl_error)
        {
            /* RW permission for the user only! */
            mode_t old_mode = umask(077);

            osl_error = osl_openFile(
                tmp_file_url,
                file_handle,
                osl_File_OpenFlag_Read |
                osl_File_OpenFlag_Write |
                osl_File_OpenFlag_Create);

            umask(old_mode);
        }

        /* in case of error osl_File_E_EXIST we simply try again else we give up */

        if ((osl_File_E_None == osl_error) || (osl_error != osl_File_E_EXIST))
        {
            if (rand_name)
                rtl_uString_release(rand_name);

            if (tmp_file_url)
                rtl_uString_release(tmp_file_url);

            break;
        }
    } /* while(1) */

    if (osl_File_E_None == osl_error)
        rtl_uString_assign(ppustr_temp_file_name, tmp_file_path);

    if (tmp_file_path)
        rtl_uString_release(tmp_file_path);

    return osl_error;
}
Beispiel #4
0
oslPipe SAL_CALL osl_acceptPipe(oslPipe pPipe)
{
	oslPipe  pAcceptedPipe = NULL;
	
	HANDLE		 Event;
    OVERLAPPED   os;

	OSL_ASSERT(pPipe);

	if (IS_NT)
	{
		DWORD nBytesTransfered;
		rtl_uString* path = NULL;
		rtl_uString* temp = NULL;

		OSL_ASSERT (pPipe->m_File != INVALID_HANDLE_VALUE);

		Event = pPipe->m_AcceptEvent;
		rtl_zeroMemory(&os, sizeof(OVERLAPPED));
		os.hEvent = pPipe->m_AcceptEvent;
		ResetEvent(pPipe->m_AcceptEvent);

		if ( !ConnectNamedPipe(pPipe->m_File, &os))
		{
			switch ( GetLastError() )
			{
			case ERROR_PIPE_CONNECTED:	// Client already connected to pipe
			case ERROR_NO_DATA:			// Client was connected but has already closed pipe end
										// should only appear in nonblocking mode but in fact does
										// in blocking asynchronous mode.
				break;
			case ERROR_PIPE_LISTENING:	// Only for nonblocking mode but see ERROR_NO_DATA
			case ERROR_IO_PENDING:		// This is normal if not client is connected yet
			case ERROR_MORE_DATA:		// Should not happen
				// blocking call to accept
				if( !GetOverlappedResult( pPipe->m_File, &os, &nBytesTransfered, TRUE ) )
				{
					// Possible error could be that between ConnectNamedPipe and GetOverlappedResult a connect
					// took place.

					switch ( GetLastError() )
					{
					case ERROR_PIPE_CONNECTED:	// Pipe was already connected
					case ERROR_NO_DATA:			// Pipe was connected but client has already closed -> ver fast client ;-)
						break;					// Everything's fine !!!
					default:
						// Something went wrong
						return 0;
					}
				}
				break;
			default:					// All other error say that somethings going wrong.
				return 0;
			}
		}

		
		pAcceptedPipe = __osl_createPipeImpl();
		OSL_ASSERT(pAcceptedPipe);

		osl_incrementInterlockedCount(&(pAcceptedPipe->m_Reference));
		rtl_uString_assign(&pAcceptedPipe->m_Name, pPipe->m_Name);
		pAcceptedPipe->m_File = pPipe->m_File;

		rtl_uString_newFromAscii(&temp, PIPESYSTEM);
		rtl_uString_newConcat(&path, temp, pPipe->m_Name);
		rtl_uString_release(temp);

		// prepare for next accept
		pPipe->m_File = 
        	CreateNamedPipeW(path->buffer, 
				PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
				PIPE_WAIT | PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE,
				PIPE_UNLIMITED_INSTANCES,
				4096, 4096,
				NMPWAIT_WAIT_FOREVER, 
				pAcceptedPipe->m_Security);
		rtl_uString_release( path );
	}
	else /* Win9x */
	{
		pAcceptedPipe = __osl_createPipeImpl();
		OSL_ASSERT(pAcceptedPipe);

		osl_incrementInterlockedCount(&(pAcceptedPipe->m_Reference));
		rtl_uString_assign(&pAcceptedPipe->m_Name, pPipe->m_Name);
		pAcceptedPipe->m_File = pPipe->m_File;

		pAcceptedPipe->m_File = AcceptSimplePipeConnection( pPipe->m_File );
	}

	return pAcceptedPipe;
}
Beispiel #5
0
oslPipe SAL_CALL osl_createPipe(rtl_uString *strPipeName, oslPipeOptions Options, 
					   oslSecurity Security)
{
	rtl_uString* name = NULL;
	rtl_uString* path = NULL;
	rtl_uString* temp = NULL;
	oslPipe pPipe;

   	PSECURITY_ATTRIBUTES  pSecAttr = NULL;

	rtl_uString_newFromAscii(&path, PIPESYSTEM);
	rtl_uString_newFromAscii(&name, PIPEPREFIX);

	if ( /*IS_NT &&*/ Security)
	{
		rtl_uString *Ident = NULL;
		rtl_uString *Delim = NULL;

		OSL_VERIFY(osl_getUserIdent(Security, &Ident));
		rtl_uString_newFromAscii(&Delim, "_");

		rtl_uString_newConcat(&temp, name, Ident);
		rtl_uString_newConcat(&name, temp, Delim);

		rtl_uString_release(Ident);
		rtl_uString_release(Delim);
	}
	else
	{
		if (Options & osl_Pipe_CREATE)
		{
	    	PSECURITY_DESCRIPTOR pSecDesc;

	    	pSecDesc = (PSECURITY_DESCRIPTOR) rtl_allocateMemory(SECURITY_DESCRIPTOR_MIN_LENGTH);

	    	/* add a NULL disc. ACL to the security descriptor */
		    OSL_VERIFY(InitializeSecurityDescriptor(pSecDesc, SECURITY_DESCRIPTOR_REVISION));
	    	OSL_VERIFY(SetSecurityDescriptorDacl(pSecDesc, TRUE, (PACL) NULL, FALSE));

			pSecAttr = rtl_allocateMemory(sizeof(SECURITY_ATTRIBUTES));
	    	pSecAttr->nLength = sizeof(SECURITY_ATTRIBUTES);
	    	pSecAttr->lpSecurityDescriptor = pSecDesc;
	    	pSecAttr->bInheritHandle = TRUE;
		}
	}

	rtl_uString_assign(&temp, name);
	rtl_uString_newConcat(&name, temp, strPipeName);

	/* alloc memory */
	pPipe= __osl_createPipeImpl();
	osl_incrementInterlockedCount(&(pPipe->m_Reference));

	/* build system pipe name */
	rtl_uString_assign(&temp, path);
	rtl_uString_newConcat(&path, temp, name);
	rtl_uString_release(temp);
	temp = NULL;

	if (Options & osl_Pipe_CREATE)
	{
		SetLastError( ERROR_SUCCESS );

		if ( IS_NT )
			pPipe->m_NamedObject = CreateMutexW( NULL, FALSE, name->buffer );
		else
		{
			LPSTR	pszTempBuffer = NULL;
			int		nCharsNeeded;

			nCharsNeeded = WideCharToMultiByte( CP_ACP, 0, name->buffer, name->length, NULL, 0, NULL, NULL );
			pszTempBuffer = alloca( nCharsNeeded * sizeof(CHAR) );
			nCharsNeeded = WideCharToMultiByte( CP_ACP, 0, name->buffer, name->length, pszTempBuffer, nCharsNeeded, NULL, NULL );
			pszTempBuffer[nCharsNeeded-1] = 0;

			pPipe->m_NamedObject = CreateMutexA( NULL, FALSE, pszTempBuffer );
		}

		if ( pPipe->m_NamedObject != INVALID_HANDLE_VALUE && pPipe->m_NamedObject != NULL )
		{
			if ( GetLastError() != ERROR_ALREADY_EXISTS )
			{
				pPipe->m_Security = pSecAttr;
				rtl_uString_assign(&pPipe->m_Name, name);

				if (IS_NT)
				{
					/* try to open system pipe */
					pPipe->m_File = CreateNamedPipeW(
						path->buffer, 
						PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
						PIPE_WAIT | PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE,
						PIPE_UNLIMITED_INSTANCES,
						4096, 4096,
						NMPWAIT_WAIT_FOREVER, 
						pPipe->m_Security);

					if (pPipe->m_File != INVALID_HANDLE_VALUE)
					{
						rtl_uString_release( name );
						rtl_uString_release( path );

						return pPipe;
					}
				}
				else /* Win 9x */
				{
					LPSTR	pszTempBuffer = NULL;
					int		nCharsNeeded;

					nCharsNeeded = WideCharToMultiByte( CP_ACP, 0, path->buffer, path->length, NULL, 0, NULL, NULL );
					pszTempBuffer = alloca( nCharsNeeded * sizeof(CHAR) );
					nCharsNeeded = WideCharToMultiByte( CP_ACP, 0, path->buffer, path->length, pszTempBuffer, nCharsNeeded, NULL, NULL );
					pszTempBuffer[nCharsNeeded-1] = 0;

					pPipe->m_File = CreateSimplePipe( pszTempBuffer );

					if ( IsValidHandle(pPipe->m_File) )
					{
						rtl_uString_release( name );
						rtl_uString_release( path );

						return pPipe;
					}
				}
			}
			else
			{
				CloseHandle( pPipe->m_NamedObject );
				pPipe->m_NamedObject = INVALID_HANDLE_VALUE;
			}
		}
	}
	else
	{
		if (IS_NT)
		{
			BOOL	fPipeAvailable;

			do
			{
				/* free instance should be available first */
				fPipeAvailable = WaitNamedPipeW(path->buffer, NMPWAIT_WAIT_FOREVER);

				/* first try to open system pipe */
				if ( fPipeAvailable )
				{
					pPipe->m_File = CreateFileW(
							path->buffer,
							GENERIC_READ|GENERIC_WRITE, 
							FILE_SHARE_READ | FILE_SHARE_WRITE,
							NULL,
							OPEN_EXISTING, 
							FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, 
							NULL);

					if ( pPipe->m_File != INVALID_HANDLE_VALUE )
					{
						// We got it !
						rtl_uString_release( name );
						rtl_uString_release( path );

						return (pPipe);
					}
					else
					{
						// Pipe instance maybe catched by another client -> try again
					}
				}
			} while ( fPipeAvailable );
		}
		else /* Win 9x */
		{
			LPSTR	pszTempBuffer = NULL;
			int		nCharsNeeded;

			nCharsNeeded = WideCharToMultiByte( CP_ACP, 0, path->buffer, path->length, NULL, 0, NULL, NULL );
			pszTempBuffer = alloca( nCharsNeeded * sizeof(CHAR) );
			nCharsNeeded = WideCharToMultiByte( CP_ACP, 0, path->buffer, path->length, pszTempBuffer, nCharsNeeded, NULL, NULL );
			pszTempBuffer[nCharsNeeded-1] = 0;

			pPipe->m_File = OpenSimplePipe( pszTempBuffer );

			if ( IsValidHandle(pPipe->m_File) )
			{
				// We got it !
				rtl_uString_release( name );
				rtl_uString_release( path );

				return (pPipe);
			}
		}
	}

	/* if we reach here something went wrong */
	__osl_destroyPipeImpl(pPipe);

	return NULL;
}