vfschain.cpp revision c7814cf6e1240a519cbec0441e033d0e2470ed00
/* $Id$ */
/** @file
* IPRT - Virtual File System, Chains.
*/
/*
* Copyright (C) 2010-2012 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/vfslowlevel.h>
#include <iprt/critsect.h>
#include <iprt/semaphore.h>
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** Init the critical section once. */
static RTONCE g_rtVfsChainElementInitOnce;
/** Critical section protecting g_rtVfsChainElementProviderList. */
/** List of VFS chain element providers (RTVFSCHAINELEMENTREG). */
/**
* Initializes the globals via RTOnce.
*
* @returns IPRT status code
* @param pvUser Unused, ignored.
*/
{
return RTCritSectInit(&g_rtVfsChainElementCritSect);
}
{
int rc;
/*
* Input validation.
*/
AssertMsgReturn(pRegRec->uVersion == RTVFSCHAINELEMENTREG_VERSION, ("%#x", pRegRec->uVersion), VERR_INVALID_POINTER);
AssertMsgReturn(pRegRec->uEndMarker == RTVFSCHAINELEMENTREG_VERSION, ("%#zx", pRegRec->uEndMarker), VERR_INVALID_POINTER);
/*
* Init and take the lock.
*/
if (!fFromCtor)
{
if (RT_FAILURE(rc))
return rc;
if (RT_FAILURE(rc))
return rc;
}
/*
* Duplicate name?
*/
rc = VINF_SUCCESS;
RTListForEachSafe(&g_rtVfsChainElementProviderList, pIterator, pIterNext, RTVFSCHAINELEMENTREG, ListEntry)
{
{
break;
}
}
/*
* If not, append the record to the list.
*/
if (RT_SUCCESS(rc))
/*
* Leave the lock and return.
*/
if (!fFromCtor)
return rc;
}
/**
* Allocates and initializes an empty spec
*
* @returns Pointer to the spec on success, NULL on failure.
*/
static PRTVFSCHAINSPEC rtVfsChainSpecAlloc(void)
{
if (pSpec)
{
}
return pSpec;
}
/**
* Duplicate a spec string.
*
* This differs from RTStrDupN in that it uses RTMemTmpAlloc instead of
* RTMemAlloc.
*
* @returns String copy on success, NULL on failure.
* @param psz The string to duplicate.
* @param cch The number of bytes to duplicate.
* @param prc The status code variable to set on failure. (Leeps the
* code shorter. -lazy bird)
*/
{
if (pszCopy)
{
{
/* Plain string, copy it raw. */
}
else
{
/* Has escape sequences, must unescape it. */
while (cch)
{
if (ch == '\\')
{
{
psz++;
}
}
}
*pszDst = '\0';
}
}
else
return pszCopy;
}
/**
* Adds an empty element to the chain specification.
*
* The caller is responsible for filling it the element attributes.
*
* @returns Pointer to the new element on success, NULL on failure. The
* pointer is only valid till the next call to this function.
* @param pSpec The chain specification.
* @param prc The status code variable to set on failure. (Leeps the
* code shorter. -lazy bird)
*/
{
/*
* Resize the element table if necessary.
*/
if ((iElement % 32) == 0)
{
if (!paNew)
{
return NULL;
}
}
/*
* Initialize and add the new element.
*/
return pElement;
}
/**
* Adds an argument to the element spec.
*
* @returns IPRT status code.
* @param pElement The element.
* @param psz The start of the argument string.
* @param cch The length of the argument string, escape
* sequences counted twice.
*/
{
if ((iArg % 32) == 0)
{
if (!papszNew)
return VERR_NO_TMP_MEMORY;
}
int rc = VINF_SUCCESS;
return rc;
}
{
if (!pSpec)
return;
while (i-- > 0)
{
while (iArg-- > 0)
}
}
/**
* Finds the end of the argument string.
*
* @returns The offset of the end character relative to @a psz.
* @param psz The argument string.
*/
{
char ch;
&& ch != ','
&& ch != ')'
&& ch != '(')
{
/* check for escape sequences. */
if ( ch == '\\'
off++;
off++;
}
return off;
}
/**
* Look for action.
*
* @returns Action.
* @param pszSpec The current spec position.
* @param pcchAction Where to return the length of the action
* string.
*/
{
switch (*pszSpec)
{
case '|':
*pcchAction = 1;
return RTVFSCHAINACTION_PASSIVE;
case '>':
*pcchAction = 1;
return RTVFSCHAINACTION_PUSH;
default:
*pcchAction = 0;
return RTVFSCHAINACTION_NONE;
}
}
RTDECL(int) RTVfsChainSpecParse(const char *pszSpec, uint32_t fFlags, RTVFSCHAINACTION enmLeadingAction,
{
if (ppszError)
AssertReturn(enmLeadingAction > RTVFSCHAINACTION_INVALID && enmLeadingAction < RTVFSCHAINACTION_END, VERR_INVALID_PARAMETER);
/*
* Check the start of the specification and allocate an empty return spec.
*/
return VERR_VFS_CHAIN_NO_PREFIX;
if (!*pszSpec)
return VERR_VFS_CHAIN_EMPTY;
if (!pSpec)
return VERR_NO_TMP_MEMORY;
/*
* Parse the spec one element at a time.
*/
int rc = VINF_SUCCESS;
{
/*
* Pipe or redirection action symbol, except maybe the first time.
* The pipe symbol may occur at the end of the spec.
*/
if (enmAction != RTVFSCHAINACTION_NONE)
{
if (!*pszSrc)
{
/* Fail if the caller does not approve of a trailing pipe (all
other actions non-trailing). */
if ( enmAction != enmTrailingAction
break;
}
/* There can only be one real action atm. */
if (enmAction != RTVFSCHAINACTION_PASSIVE)
{
{
break;
}
}
}
{
break;
}
/* Check the leading action. */
&& enmAction != enmLeadingAction
{
break;
}
/*
* Ok, there should be an element here so add one to the return struct.
*/
if (!pElement)
break;
/*
* First up is the VFS object type followed by a parentheses.
*/
else
{
break;
}
/* Check and skip the parentheses. */
if (*pszSrc != '(')
{
break;
}
/*
* The name of the element provider.
*/
if (!cch)
{
break;
}
if (!pElement->pszProvider)
break;
/*
* The arguments.
*/
while (*pszSrc == ',')
{
}
/* Must end with a right parentheses. */
if (*pszSrc != ')')
{
break;
}
}
/*
* Return the chain on success; Cleanup and set the error indicator on
* failure.
*/
if (RT_SUCCESS(rc))
else
{
if (ppszError)
}
return rc;
}
{
/*
* Fend off wildlife.
*/
return VINF_SUCCESS;
AssertMsgReturn(pRegRec->uVersion == RTVFSCHAINELEMENTREG_VERSION, ("%#x", pRegRec->uVersion), VERR_INVALID_POINTER);
AssertMsgReturn(pRegRec->uEndMarker == RTVFSCHAINELEMENTREG_VERSION, ("%#zx", pRegRec->uEndMarker), VERR_INVALID_POINTER);
/*
* Take the lock if that's safe.
*/
if (!fFromDtor)
/*
* Ok, remove it.
*/
int rc = VERR_NOT_FOUND;
RTListForEachSafe(&g_rtVfsChainElementProviderList, pIterator, pIterNext, RTVFSCHAINELEMENTREG, ListEntry)
{
{
rc = VINF_SUCCESS;
break;
}
}
/*
* Leave the lock and return.
*/
if (!fFromDtor)
return rc;
}
RTDECL(int) RTVfsChainOpenFile(const char *pszSpec, uint64_t fOpen, PRTVFSFILE phVfsFile, const char **ppszError)
{
if (ppszError)
/*
* If it's not a VFS chain spec, treat it as a file.
*/
int rc;
{
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
else
}
}
else
{
&pSpec,
if (RT_SUCCESS(rc))
{
/** @todo implement this when needed. */
}
}
return rc;
}
RTDECL(int) RTVfsChainOpenIoStream(const char *pszSpec, uint64_t fOpen, PRTVFSIOSTREAM phVfsIos, const char **ppszError)
{
if (ppszError)
/*
* If it's not a VFS chain spec, treat it as a file.
*/
int rc;
{
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
}
else
}
}
else
{
&pSpec,
if (RT_SUCCESS(rc))
{
}
}
return rc;
}
{
return pszSpec
}