scmsubversion.cpp revision 3b91cdad96393c90c3098e2805621136b20ef1e6
/* $Id$ */
/** @file
* IPRT Testcase / Tool - Source Code Massager, Subversion Access.
*/
/*
* 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.
*/
#define SCM_WITHOUT_LIBSVN
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/initterm.h>
#include "scm.h"
/*******************************************************************************
* Global Variables *
*******************************************************************************/
static char g_szSvnPath[RTPATH_MAX];
static enum
{
#ifdef SCM_WITHOUT_LIBSVN
/**
* Callback that is call for each path to search.
*/
static DECLCALLBACK(int) scmSvnFindSvnBinaryCallback(char const *pchPath, size_t cchPath, void *pvUser1, void *pvUser2)
{
{
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
#else
#endif
if ( RT_SUCCESS(rc)
&& RTFileExists(pszDst))
return VINF_SUCCESS;
}
return VERR_TRY_AGAIN;
}
/**
* Reads from a pipe.
*
* @returns @a rc or other status code.
* @param rc The current status of the operation. Error status
* are preserved and returned.
* @param phPipeR Pointer to the pipe handle.
* @param pcbAllocated Pointer to the buffer size variable.
* @param poffCur Pointer to the buffer offset variable.
* @param ppszBuffer Pointer to the buffer pointer variable.
*/
static int rtProcProcessOutput(int rc, PRTPIPE phPipeR, size_t *pcbAllocated, size_t *poffCur, char **ppszBuffer,
{
for (;;)
{
{
/* Resize the buffer. */
{
if (*pcbAllocated >= _1G)
{
*phPipeR = NIL_RTPIPE;
}
if (RT_FAILURE(rc2))
{
*phPipeR = NIL_RTPIPE;
}
*pcbAllocated = cbNew;
}
/* Append the new data, terminating it. */
/* Check for null terminators in the string. */
/* If we read a full buffer, try read some more. */
continue;
}
else if (rc2 != VINF_TRY_AGAIN)
{
*phPipeR = NIL_RTPIPE;
}
return rc;
}
}
/** @name RTPROCEXEC_FLAGS_XXX - flags for RTProcExec and RTProcExecToString.
* @{ */
#define RTPROCEXEC_FLAGS_STDIN_NULL RT_BIT_32(0)
* to standard input. */
#define RTPROCEXEC_FLAGS_STD_NULL ( RTPROCEXEC_FLAGS_STDIN_NULL \
/** Mask containing the valid flags. */
/** @} */
/**
*
*
* @returns IPRT status code
* @retval VERR_NO_TRANSLATION if the output of the program isn't valid UTF-8
* or contains a nul character.
* @retval VERR_TOO_MUCH_DATA if the process produced too much data.
*
* @param pszExec Executable image to use to create the child process.
* @param papszArgs Pointer to an array of arguments to the child. The
* array terminated by an entry containing NULL.
* @param hEnv Handle to the environment block for the child.
* @param fFlags A combination of RTPROCEXEC_FLAGS_XXX. The @a
* ppszStdOut and @a ppszStdErr parameters takes precedence
* over redirection flags.
* @param pStatus Where to return the status on success.
* @param ppszStdOut Where to return the text written to standard output. If
* NULL then standard output will not be collected and go
* to the standard output handle of the process.
* Free with RTStrFree, regardless of return status.
* @param ppszStdErr Where to return the text written to standard error. If
* NULL then standard output will not be collected and go
* to the standard error handle of the process.
* Free with RTStrFree, regardless of return status.
*/
int RTProcExecToString(const char *pszExec, const char * const *papszArgs, RTENV hEnv, uint32_t fFlags,
{
int rc2;
/*
* Clear output arguments (no returning failure here, simply crash!).
*/
if (ppszStdOut)
*ppszStdOut = NULL;
if (ppszStdErr)
*ppszStdErr = NULL;
/*
* Check input arguments.
*/
/*
* Do we need a standard input bitbucket?
*/
int rc = VINF_SUCCESS;
{
}
/*
* Create the output pipes / bitbuckets.
*/
{
}
{
}
{
}
{
}
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
if (hPipeStdErrR != NIL_RTPIPE)
}
if (RT_SUCCESS(rc))
{
/*
* Create the process.
*/
0 /*fFlags*/,
NULL /*phStdIn*/,
NULL /*pszAsUser*/,
NULL /*pszPassword*/,
&hProc);
if (RT_SUCCESS(rc))
{
/*
* Process output and wait for the process to finish.
*/
for (;;)
{
if (hPipeStdOutR != NIL_RTPIPE)
if (hPipeStdErrR != NIL_RTPIPE)
break;
if (hProc != NIL_RTPROCESS)
{
if (rc2 != VERR_PROCESS_RUNNING)
{
if (RT_FAILURE(rc2))
}
}
}
if (RT_SUCCESS(rc))
{
}
/*
* No more output, just wait for it to finish.
*/
if (hProc != NIL_RTPROCESS)
{
if (RT_FAILURE(rc2))
}
}
}
}
return rc;
}
/**
* Runs a process, waiting for it to complete.
*
* @returns IPRT status code
*
* @param pszExec Executable image to use to create the child process.
* @param papszArgs Pointer to an array of arguments to the child. The
* array terminated by an entry containing NULL.
* @param hEnv Handle to the environment block for the child.
* @param fFlags A combination of RTPROCEXEC_FLAGS_XXX.
* @param pStatus Where to return the status on success.
*/
{
int rc;
/*
* Clear output argument (no returning failure here, simply crash!).
*/
/*
* Check input arguments.
*/
/*
*/
for (uint32_t i = 0; i < 3; i++)
{
}
rc = VINF_SUCCESS;
{
}
{
}
{
}
/*
* Create the process.
*/
if (RT_SUCCESS(rc))
0 /*fFlags*/,
aph[0],
aph[1],
aph[2],
NULL /*pszAsUser*/,
NULL /*pszPassword*/,
&hProc);
for (uint32_t i = 0; i < 3; i++)
if (RT_SUCCESS(rc))
return rc;
}
/**
* Executes SVN and gets the output.
*
* Standard error is suppressed.
*
* @returns VINF_SUCCESS if the command executed successfully.
* @param pState The rewrite state to work on. Can be NULL.
* @param papszArgs The SVN argument.
* @param fNormalFailureOk Whether normal failure is ok.
* @param ppszStdOut Where to return the output on success.
*/
static int scmSvnRunAndGetOutput(PSCMRWSTATE pState, const char **papszArgs, bool fNormalFailureOk, char **ppszStdOut)
{
*ppszStdOut = NULL;
char *pszCmdLine = NULL;
if (RT_FAILURE(rc))
return rc;
if ( RT_SUCCESS(rc)
{
RTMsgError("%s: %s -> %s %u\n",
: "abducted by alien",
}
else if (RT_FAILURE(rc))
{
if (pState)
else
}
if (RT_FAILURE(rc))
{
*ppszStdOut = NULL;
}
return rc;
}
/**
* Executes SVN.
*
* Standard error and standard output is suppressed.
*
* @returns VINF_SUCCESS if the command executed successfully.
* @param pState The rewrite state to work on.
* @param papszArgs The SVN argument.
* @param fNormalFailureOk Whether normal failure is ok.
*/
{
char *pszCmdLine = NULL;
if (RT_FAILURE(rc))
return rc;
/* Lazy bird uses RTProcExecToString. */
if ( RT_SUCCESS(rc)
{
RTMsgError("%s: %s -> %s %u\n",
: "abducted by alien",
}
else if (RT_FAILURE(rc))
return rc;
}
/**
* Finds the svn binary, updating g_szSvnPath and g_enmSvnVersion.
*/
{
/* Already been called? */
if (g_szSvnPath[0] != '\0')
return;
/*
* Locate it.
*/
/** @todo code page fun... */
#ifdef RT_OS_WINDOWS
#else
#endif
if (pszEnvVar)
{
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
int rc = RTPathTraverseList(pszEnvVar, ';', scmSvnFindSvnBinaryCallback, g_szSvnPath, (void *)sizeof(g_szSvnPath));
#else
int rc = RTPathTraverseList(pszEnvVar, ':', scmSvnFindSvnBinaryCallback, g_szSvnPath, (void *)sizeof(g_szSvnPath));
#endif
if (RT_FAILURE(rc))
}
else
/*
* Check the version.
*/
char *pszVersion;
if (RT_SUCCESS(rc))
{
else
}
else
}
/**
* Construct a dot svn filename for the file being rewritten.
*
* @returns IPRT status code.
* @param pState The rewrite state (for the name).
* @param pszDir The directory, including ".svn/".
* @param pszSuff The filename suffix.
* @param pszDst The output buffer. RTPATH_MAX in size.
*/
static int scmSvnConstructName(PSCMRWSTATE pState, const char *pszDir, const char *pszSuff, char *pszDst)
{
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
{
return VINF_SUCCESS;
}
else
}
}
return rc;
}
/**
* Interprets the specified string as decimal numbers.
*
* @returns true if parsed successfully, false if not.
* @param pch The string (not terminated).
* @param cch The string length.
* @param pu Where to return the value.
*/
{
size_t u = 0;
while (cch-- > 0)
{
return false;
u *= 10;
u += ch - '0';
}
*pu = u;
return true;
}
#endif /* SCM_WITHOUT_LIBSVN */
/**
* Checks if the file we're operating on is part of a SVN working copy.
*
* @returns true if it is, false if it isn't or we cannot tell.
* @param pState The rewrite state to work on.
*/
{
#ifdef SCM_WITHOUT_LIBSVN
{
/*
*/
char szPath[RTPATH_MAX];
if (RT_SUCCESS(rc))
return RTFileExists(szPath);
}
else
{
const char *apszArgs[] = { g_szSvnPath, "propget", "svn:no-such-property", pState->pszFilename, NULL };
char *pszValue;
if (RT_SUCCESS(rc))
{
return true;
}
}
#else
#endif
return false;
}
/**
* Checks if the specified directory is part of a SVN working copy.
*
* @returns true if it is, false if it isn't or we cannot tell.
* @param pszDir The directory in question.
*/
bool ScmSvnIsDirInWorkingCopy(const char *pszDir)
{
#ifdef SCM_WITHOUT_LIBSVN
{
/*
* Hack: check if the .svn/ dir exists.
*/
char szPath[RTPATH_MAX];
if (RT_SUCCESS(rc))
return RTDirExists(szPath);
}
else
{
char *pszValue;
if (RT_SUCCESS(rc))
{
return true;
}
}
#else
#endif
return false;
}
/**
* Queries the value of an SVN property.
*
* This will automatically adjust for scheduled changes.
*
* @returns IPRT status code.
* @retval VERR_INVALID_STATE if not a SVN WC file.
* @retval VERR_NOT_FOUND if the property wasn't found.
* @param pState The rewrite state to work on.
* @param pszName The property name.
* @param ppszValue Where to return the property value. Free this
* using RTStrFree. Optional.
*/
{
/*
* Look it up in the scheduled changes.
*/
while (i-- > 0)
{
if (!pszValue)
return VERR_NOT_FOUND;
if (ppszValue)
return VINF_SUCCESS;
}
#ifdef SCM_WITHOUT_LIBSVN
int rc;
{
/*
*/
char szPath[RTPATH_MAX];
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
/*
* The current format is K len\n<name>\nV len\n<value>\n" ... END.
*/
rc = VERR_NOT_FOUND;
const char *pchLine;
{
/*
* Parse the 'K num' / 'END' line.
*/
if ( cchLine == 3
break;
if ( cchLine < 3
|| pchLine[0] != 'K'
|| cchKey == 0
|| cchKey > 4096)
{
RTMsgError("%s:%u: Unexpected data '%.*s'\n", szPath, ScmStreamTellLine(&Stream), cchLine, pchLine);
break;
}
/*
* Match the key and skip to the value line. Don't bother with
* names containing EOL markers.
*/
if (fMatch)
{
if (!pchLine)
break;
}
break;
break;
/*
* Read and Parse the 'V num' line.
*/
if (!pchLine)
break;
if ( cchLine < 3
|| pchLine[0] != 'V'
{
RTMsgError("%s:%u: Unexpected data '%.*s'\n", szPath, ScmStreamTellLine(&Stream), cchLine, pchLine);
break;
}
/*
* If we have a match, allocate a return buffer and read the
* value into it. Otherwise skip this value and continue
* searching.
*/
if (fMatch)
{
if (!ppszValue)
rc = VINF_SUCCESS;
else
{
char *pszValue;
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
else
}
}
break;
}
break;
break;
}
{
}
}
}
if (rc == VERR_FILE_NOT_FOUND)
rc = VERR_NOT_FOUND;
}
else
{
const char *apszArgs[] = { g_szSvnPath, "propget", "--strict", pszName, pState->pszFilename, NULL };
char *pszValue;
if (RT_SUCCESS(rc))
{
{
if (ppszValue)
{
}
}
else
rc = VERR_NOT_FOUND;
}
}
return rc;
#else
#endif
return VERR_NOT_FOUND;
}
/**
* Schedules the setting of a property.
*
* @returns IPRT status code.
* @retval VERR_INVALID_STATE if not a SVN WC file.
* @param pState The rewrite state to work on.
* @param pszName The name of the property to set.
* @param pszValue The value. NULL means deleting it.
*/
{
/*
* Update any existing entry first.
*/
while (i-- > 0)
{
if (!pszValue)
{
}
else
{
char *pszCopy;
if (RT_FAILURE(rc))
return rc;
}
return VINF_SUCCESS;
}
/*
* Insert a new entry.
*/
i = pState->cSvnPropChanges;
if ((i % 32) == 0)
{
if (!pvNew)
return VERR_NO_MEMORY;
}
else
{
return VERR_NO_MEMORY;
}
return VINF_SUCCESS;
}
/**
* Schedules a property deletion.
*
* @returns IPRT status code.
* @param pState The rewrite state to work on.
* @param pszName The name of the property to delete.
*/
{
}
/**
* Applies any SVN property changes to the work copy of the file.
*
* @returns IPRT status code.
* @param pState The rewrite state which SVN property changes
* should be applied.
*/
{
while (i-- > 0)
{
if (pszValue)
else
}
return VINF_SUCCESS;
}
/**
* Applies any SVN property changes to the work copy of the file.
*
* @returns IPRT status code.
* @param pState The rewrite state which SVN property changes
* should be applied.
*/
{
#ifdef SCM_WITHOUT_LIBSVN
/*
* Iterate thru the changes and apply them by starting the svn client.
*/
{
const char *apszArgv[6];
apszArgv[0] = g_szSvnPath;
int iArg = 3;
if (RT_FAILURE(rc))
return rc;
}
return VINF_SUCCESS;
#else
return VERR_NOT_IMPLEMENTED;
#endif
}