VirtualBox

source: vbox/trunk/src/bldprogs/scmsubversion.cpp@ 85704

Last change on this file since 85704 was 85696, checked in by vboxsync, 4 years ago

scm/svn: Try use proplist instead of propget to detect whether a file is in the working copy or not.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 56.2 KB
Line 
1/* $Id: scmsubversion.cpp 85696 2020-08-11 16:40:39Z vboxsync $ */
2/** @file
3 * IPRT Testcase / Tool - Source Code Massager, Subversion Access.
4 */
5
6/*
7 * Copyright (C) 2010-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#define SCM_WITH_DYNAMIC_LIB_SVN
19
20
21/*********************************************************************************************************************************
22* Header Files *
23*********************************************************************************************************************************/
24#include <iprt/assert.h>
25#include <iprt/ctype.h>
26#include <iprt/dir.h>
27#include <iprt/env.h>
28#include <iprt/err.h>
29#include <iprt/file.h>
30#include <iprt/getopt.h>
31#include <iprt/handle.h>
32#include <iprt/initterm.h>
33#include <iprt/ldr.h>
34#include <iprt/mem.h>
35#include <iprt/message.h>
36#include <iprt/param.h>
37#include <iprt/path.h>
38#include <iprt/pipe.h>
39#include <iprt/poll.h>
40#include <iprt/process.h>
41#include <iprt/stream.h>
42#include <iprt/string.h>
43
44#include "scm.h"
45
46#if defined(SCM_WITH_DYNAMIC_LIB_SVN) && defined(SCM_WITH_SVN_HEADERS)
47# include <svn_client.h>
48#endif
49
50
51/*********************************************************************************************************************************
52* Defined Constants And Macros *
53*********************************************************************************************************************************/
54#ifdef SCM_WITH_DYNAMIC_LIB_SVN
55# if defined(RT_OS_WINDOWS) && defined(RT_ARCH_X86)
56# define APR_CALL __stdcall
57# define SVN_CALL /* __stdcall ?? */
58# else
59# define APR_CALL
60# define SVN_CALL
61# endif
62#endif
63#if defined(SCM_WITH_DYNAMIC_LIB_SVN) && !defined(SCM_WITH_SVN_HEADERS)
64# define SVN_ERR_MISC_CATEGORY_START 200000
65# define SVN_ERR_UNVERSIONED_RESOURCE (SVN_ERR_MISC_CATEGORY_START + 5)
66#endif
67
68
69/*********************************************************************************************************************************
70* Structures and Typedefs *
71*********************************************************************************************************************************/
72#if defined(SCM_WITH_DYNAMIC_LIB_SVN) && !defined(SCM_WITH_SVN_HEADERS)
73typedef int apr_status_t;
74typedef int64_t apr_time_t;
75typedef struct apr_pool_t apr_pool_t;
76typedef struct apr_hash_t apr_hash_t;
77typedef struct apr_hash_index_t apr_hash_index_t;
78typedef struct apr_array_header_t apr_array_header_t;
79
80
81typedef struct svn_error_t
82{
83 apr_status_t apr_err;
84 const char *_dbgr_message;
85 struct svn_error_t *_dbgr_child;
86 apr_pool_t *_dbgr_pool;
87 const char *_dbgr_file;
88 long _dbgr_line;
89} svn_error_t;
90typedef int svn_boolean_t;
91typedef long int svn_revnum_t;
92typedef struct svn_client_ctx_t svn_client_ctx_t;
93typedef enum svn_opt_revision_kind
94{
95 svn_opt_revision_unspecified = 0,
96 svn_opt_revision_number,
97 svn_opt_revision_date,
98 svn_opt_revision_committed,
99 svn_opt_revision_previous,
100 svn_opt_revision_base,
101 svn_opt_revision_working,
102 svn_opt_revision_head
103} svn_opt_revision_kind;
104typedef union svn_opt_revision_value_t
105{
106 svn_revnum_t number;
107 apr_time_t date;
108} svn_opt_revision_value_t;
109typedef struct svn_opt_revision_t
110{
111 svn_opt_revision_kind kind;
112 svn_opt_revision_value_t value;
113} svn_opt_revision_t;
114typedef enum svn_depth_t
115{
116 svn_depth_unknown = -2,
117 svn_depth_exclude,
118 svn_depth_empty,
119 svn_depth_files,
120 svn_depth_immediates,
121 svn_depth_infinity
122} svn_depth_t;
123
124#endif /* SCM_WITH_DYNAMIC_LIB_SVN && !SCM_WITH_SVN_HEADERS */
125
126
127/*********************************************************************************************************************************
128* Global Variables *
129*********************************************************************************************************************************/
130static char g_szSvnPath[RTPATH_MAX];
131static enum
132{
133 kScmSvnVersion_Ancient = 1,
134 kScmSvnVersion_1_6,
135 kScmSvnVersion_1_7,
136 kScmSvnVersion_1_8,
137 kScmSvnVersion_End
138} g_enmSvnVersion = kScmSvnVersion_Ancient;
139
140
141#ifdef SCM_WITH_DYNAMIC_LIB_SVN
142/** Set if all the function pointers are valid. */
143static bool g_fSvnFunctionPointersValid;
144/** @name SVN and APR imports.
145 * @{ */
146static apr_status_t (APR_CALL *g_pfnAprInitialize)(void);
147static apr_hash_index_t * (APR_CALL *g_pfnAprHashFirst)(apr_pool_t *pPool, apr_hash_t *pHashTab);
148static apr_hash_index_t * (APR_CALL *g_pfnAprHashNext)(apr_hash_index_t *pCurIdx);
149static void * (APR_CALL *g_pfnAprHashThisVal)(apr_hash_index_t *pHashIdx);
150static apr_pool_t * (SVN_CALL *g_pfnSvnPoolCreateEx)(apr_pool_t *pParent, void *pvAllocator);
151static void (APR_CALL *g_pfnAprPoolClear)(apr_pool_t *pPool);
152static void (APR_CALL *g_pfnAprPoolDestroy)(apr_pool_t *pPool);
153
154static svn_error_t * (SVN_CALL *g_pfnSvnClientCreateContext)(svn_client_ctx_t **ppCtx, apr_pool_t *pPool);
155static svn_error_t * (SVN_CALL *g_pfnSvnClientPropGet4)(apr_hash_t **ppHashProps, const char *pszPropName,
156 const char *pszTarget, const svn_opt_revision_t *pPeggedRev,
157 const svn_opt_revision_t *pRevision, svn_revnum_t *pActualRev,
158 svn_depth_t enmDepth, const apr_array_header_t *pChangeList,
159 svn_client_ctx_t *pCtx, apr_pool_t *pResultPool,
160 apr_pool_t *pScratchPool);
161/**@} */
162
163/** Cached APR pool. */
164static apr_pool_t *g_pSvnPool = NULL;
165/** Cached SVN client context. */
166static svn_client_ctx_t *g_pSvnClientCtx = NULL;
167/** Number of times the current context has been used. */
168static uint32_t g_cSvnClientCtxUsed = 0;
169
170#endif
171
172
173/*********************************************************************************************************************************
174* Internal Functions *
175*********************************************************************************************************************************/
176#ifdef SCM_WITH_DYNAMIC_LIB_SVN
177static void scmSvnFlushClientContextAndPool(void);
178#endif
179
180
181
182/**
183 * Callback that is call for each path to search.
184 */
185static DECLCALLBACK(int) scmSvnFindSvnBinaryCallback(char const *pchPath, size_t cchPath, void *pvUser1, void *pvUser2)
186{
187 char *pszDst = (char *)pvUser1;
188 size_t cchDst = (size_t)pvUser2;
189 if (cchDst > cchPath)
190 {
191 memcpy(pszDst, pchPath, cchPath);
192 pszDst[cchPath] = '\0';
193#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
194 int rc = RTPathAppend(pszDst, cchDst, "svn.exe");
195#else
196 int rc = RTPathAppend(pszDst, cchDst, "svn");
197#endif
198 if ( RT_SUCCESS(rc)
199 && RTFileExists(pszDst))
200 return VINF_SUCCESS;
201 }
202 return VERR_TRY_AGAIN;
203}
204
205
206/**
207 * Reads from a pipe.
208 *
209 * @returns @a rc or other status code.
210 * @param rc The current status of the operation. Error status
211 * are preserved and returned.
212 * @param phPipeR Pointer to the pipe handle.
213 * @param pcbAllocated Pointer to the buffer size variable.
214 * @param poffCur Pointer to the buffer offset variable.
215 * @param ppszBuffer Pointer to the buffer pointer variable.
216 */
217static int rtProcProcessOutput(int rc, PRTPIPE phPipeR, size_t *pcbAllocated, size_t *poffCur, char **ppszBuffer,
218 RTPOLLSET hPollSet, uint32_t idPollSet)
219{
220 size_t cbRead;
221 char szTmp[_4K - 1];
222 for (;;)
223 {
224 int rc2 = RTPipeRead(*phPipeR, szTmp, sizeof(szTmp), &cbRead);
225 if (RT_SUCCESS(rc2) && cbRead)
226 {
227 /* Resize the buffer. */
228 if (*poffCur + cbRead >= *pcbAllocated)
229 {
230 if (*pcbAllocated >= _1G)
231 {
232 RTPollSetRemove(hPollSet, idPollSet);
233 rc2 = RTPipeClose(*phPipeR); AssertRC(rc2);
234 *phPipeR = NIL_RTPIPE;
235 return RT_SUCCESS(rc) ? VERR_TOO_MUCH_DATA : rc;
236 }
237
238 size_t cbNew = *pcbAllocated ? *pcbAllocated * 2 : sizeof(szTmp) + 1;
239 Assert(*poffCur + cbRead < cbNew);
240 rc2 = RTStrRealloc(ppszBuffer, cbNew);
241 if (RT_FAILURE(rc2))
242 {
243 RTPollSetRemove(hPollSet, idPollSet);
244 rc2 = RTPipeClose(*phPipeR); AssertRC(rc2);
245 *phPipeR = NIL_RTPIPE;
246 return RT_SUCCESS(rc) ? rc2 : rc;
247 }
248 *pcbAllocated = cbNew;
249 }
250
251 /* Append the new data, terminating it. */
252 memcpy(*ppszBuffer + *poffCur, szTmp, cbRead);
253 *poffCur += cbRead;
254 (*ppszBuffer)[*poffCur] = '\0';
255
256 /* Check for null terminators in the string. */
257 if (RT_SUCCESS(rc) && memchr(szTmp, '\0', cbRead))
258 rc = VERR_NO_TRANSLATION;
259
260 /* If we read a full buffer, try read some more. */
261 if (RT_SUCCESS(rc) && cbRead == sizeof(szTmp))
262 continue;
263 }
264 else if (rc2 != VINF_TRY_AGAIN)
265 {
266 if (RT_FAILURE(rc) && rc2 != VERR_BROKEN_PIPE)
267 rc = rc2;
268 RTPollSetRemove(hPollSet, idPollSet);
269 rc2 = RTPipeClose(*phPipeR); AssertRC(rc2);
270 *phPipeR = NIL_RTPIPE;
271 }
272 return rc;
273 }
274}
275
276/** @name RTPROCEXEC_FLAGS_XXX - flags for RTProcExec and RTProcExecToString.
277 * @{ */
278/** Redirect /dev/null to standard input. */
279#define RTPROCEXEC_FLAGS_STDIN_NULL RT_BIT_32(0)
280/** Redirect standard output to /dev/null. */
281#define RTPROCEXEC_FLAGS_STDOUT_NULL RT_BIT_32(1)
282/** Redirect standard error to /dev/null. */
283#define RTPROCEXEC_FLAGS_STDERR_NULL RT_BIT_32(2)
284/** Redirect all standard output to /dev/null as well as directing /dev/null
285 * to standard input. */
286#define RTPROCEXEC_FLAGS_STD_NULL ( RTPROCEXEC_FLAGS_STDIN_NULL \
287 | RTPROCEXEC_FLAGS_STDOUT_NULL \
288 | RTPROCEXEC_FLAGS_STDERR_NULL)
289/** Mask containing the valid flags. */
290#define RTPROCEXEC_FLAGS_VALID_MASK UINT32_C(0x00000007)
291/** @} */
292
293/**
294 * Runs a process, collecting the standard output and/or standard error.
295 *
296 *
297 * @returns IPRT status code
298 * @retval VERR_NO_TRANSLATION if the output of the program isn't valid UTF-8
299 * or contains a nul character.
300 * @retval VERR_TOO_MUCH_DATA if the process produced too much data.
301 *
302 * @param pszExec Executable image to use to create the child process.
303 * @param papszArgs Pointer to an array of arguments to the child. The
304 * array terminated by an entry containing NULL.
305 * @param hEnv Handle to the environment block for the child.
306 * @param fFlags A combination of RTPROCEXEC_FLAGS_XXX. The @a
307 * ppszStdOut and @a ppszStdErr parameters takes precedence
308 * over redirection flags.
309 * @param pStatus Where to return the status on success.
310 * @param ppszStdOut Where to return the text written to standard output. If
311 * NULL then standard output will not be collected and go
312 * to the standard output handle of the process.
313 * Free with RTStrFree, regardless of return status.
314 * @param ppszStdErr Where to return the text written to standard error. If
315 * NULL then standard output will not be collected and go
316 * to the standard error handle of the process.
317 * Free with RTStrFree, regardless of return status.
318 */
319int RTProcExecToString(const char *pszExec, const char * const *papszArgs, RTENV hEnv, uint32_t fFlags,
320 PRTPROCSTATUS pStatus, char **ppszStdOut, char **ppszStdErr)
321{
322 int rc2;
323
324 /*
325 * Clear output arguments (no returning failure here, simply crash!).
326 */
327 AssertPtr(pStatus);
328 pStatus->enmReason = RTPROCEXITREASON_ABEND;
329 pStatus->iStatus = RTEXITCODE_FAILURE;
330 AssertPtrNull(ppszStdOut);
331 if (ppszStdOut)
332 *ppszStdOut = NULL;
333 AssertPtrNull(ppszStdOut);
334 if (ppszStdErr)
335 *ppszStdErr = NULL;
336
337 /*
338 * Check input arguments.
339 */
340 AssertReturn(!(fFlags & ~RTPROCEXEC_FLAGS_VALID_MASK), VERR_INVALID_PARAMETER);
341
342 /*
343 * Do we need a standard input bitbucket?
344 */
345 int rc = VINF_SUCCESS;
346 PRTHANDLE phChildStdIn = NULL;
347 RTHANDLE hChildStdIn;
348 hChildStdIn.enmType = RTHANDLETYPE_FILE;
349 hChildStdIn.u.hFile = NIL_RTFILE;
350 if ((fFlags & RTPROCEXEC_FLAGS_STDIN_NULL) && RT_SUCCESS(rc))
351 {
352 phChildStdIn = &hChildStdIn;
353 rc = RTFileOpenBitBucket(&hChildStdIn.u.hFile, RTFILE_O_READ);
354 }
355
356 /*
357 * Create the output pipes / bitbuckets.
358 */
359 RTPIPE hPipeStdOutR = NIL_RTPIPE;
360 PRTHANDLE phChildStdOut = NULL;
361 RTHANDLE hChildStdOut;
362 hChildStdOut.enmType = RTHANDLETYPE_PIPE;
363 hChildStdOut.u.hPipe = NIL_RTPIPE;
364 if (ppszStdOut && RT_SUCCESS(rc))
365 {
366 phChildStdOut = &hChildStdOut;
367 rc = RTPipeCreate(&hPipeStdOutR, &hChildStdOut.u.hPipe, 0 /*fFlags*/);
368 }
369 else if ((fFlags & RTPROCEXEC_FLAGS_STDOUT_NULL) && RT_SUCCESS(rc))
370 {
371 phChildStdOut = &hChildStdOut;
372 hChildStdOut.enmType = RTHANDLETYPE_FILE;
373 hChildStdOut.u.hFile = NIL_RTFILE;
374 rc = RTFileOpenBitBucket(&hChildStdOut.u.hFile, RTFILE_O_WRITE);
375 }
376
377 RTPIPE hPipeStdErrR = NIL_RTPIPE;
378 PRTHANDLE phChildStdErr = NULL;
379 RTHANDLE hChildStdErr;
380 hChildStdErr.enmType = RTHANDLETYPE_PIPE;
381 hChildStdErr.u.hPipe = NIL_RTPIPE;
382 if (ppszStdErr && RT_SUCCESS(rc))
383 {
384 phChildStdErr = &hChildStdErr;
385 rc = RTPipeCreate(&hPipeStdErrR, &hChildStdErr.u.hPipe, 0 /*fFlags*/);
386 }
387 else if ((fFlags & RTPROCEXEC_FLAGS_STDERR_NULL) && RT_SUCCESS(rc))
388 {
389 phChildStdErr = &hChildStdErr;
390 hChildStdErr.enmType = RTHANDLETYPE_FILE;
391 hChildStdErr.u.hFile = NIL_RTFILE;
392 rc = RTFileOpenBitBucket(&hChildStdErr.u.hFile, RTFILE_O_WRITE);
393 }
394
395 if (RT_SUCCESS(rc))
396 {
397 RTPOLLSET hPollSet;
398 rc = RTPollSetCreate(&hPollSet);
399 if (RT_SUCCESS(rc))
400 {
401 if (hPipeStdOutR != NIL_RTPIPE && RT_SUCCESS(rc))
402 rc = RTPollSetAddPipe(hPollSet, hPipeStdOutR, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR, 1);
403 if (hPipeStdErrR != NIL_RTPIPE)
404 rc = RTPollSetAddPipe(hPollSet, hPipeStdErrR, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR, 2);
405 }
406 if (RT_SUCCESS(rc))
407 {
408 /*
409 * Create the process.
410 */
411 RTPROCESS hProc;
412 rc = RTProcCreateEx(pszExec,
413 papszArgs,
414 hEnv,
415 0 /*fFlags*/,
416 NULL /*phStdIn*/,
417 phChildStdOut,
418 phChildStdErr,
419 NULL /*pszAsUser*/,
420 NULL /*pszPassword*/,
421 NULL /*pvExtraData*/,
422 &hProc);
423 rc2 = RTHandleClose(&hChildStdErr); AssertRC(rc2);
424 rc2 = RTHandleClose(&hChildStdOut); AssertRC(rc2);
425
426 if (RT_SUCCESS(rc))
427 {
428 /*
429 * Process output and wait for the process to finish.
430 */
431 size_t cbStdOut = 0;
432 size_t offStdOut = 0;
433 size_t cbStdErr = 0;
434 size_t offStdErr = 0;
435 for (;;)
436 {
437 if (hPipeStdOutR != NIL_RTPIPE)
438 rc = rtProcProcessOutput(rc, &hPipeStdOutR, &cbStdOut, &offStdOut, ppszStdOut, hPollSet, 1);
439 if (hPipeStdErrR != NIL_RTPIPE)
440 rc = rtProcProcessOutput(rc, &hPipeStdErrR, &cbStdErr, &offStdErr, ppszStdErr, hPollSet, 2);
441 if (hPipeStdOutR == NIL_RTPIPE && hPipeStdErrR == NIL_RTPIPE)
442 break;
443
444 if (hProc != NIL_RTPROCESS)
445 {
446 rc2 = RTProcWait(hProc, RTPROCWAIT_FLAGS_NOBLOCK, pStatus);
447 if (rc2 != VERR_PROCESS_RUNNING)
448 {
449 if (RT_FAILURE(rc2))
450 rc = rc2;
451 hProc = NIL_RTPROCESS;
452 }
453 }
454
455 rc2 = RTPoll(hPollSet, 10000, NULL, NULL);
456 Assert(RT_SUCCESS(rc2) || rc2 == VERR_TIMEOUT);
457 }
458
459 if (RT_SUCCESS(rc))
460 {
461 if ( (ppszStdOut && *ppszStdOut && !RTStrIsValidEncoding(*ppszStdOut))
462 || (ppszStdErr && *ppszStdErr && !RTStrIsValidEncoding(*ppszStdErr)) )
463 rc = VERR_NO_TRANSLATION;
464 }
465
466 /*
467 * No more output, just wait for it to finish.
468 */
469 if (hProc != NIL_RTPROCESS)
470 {
471 rc2 = RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, pStatus);
472 if (RT_FAILURE(rc2))
473 rc = rc2;
474 }
475 }
476 RTPollSetDestroy(hPollSet);
477 }
478 }
479
480 rc2 = RTHandleClose(&hChildStdErr); AssertRC(rc2);
481 rc2 = RTHandleClose(&hChildStdOut); AssertRC(rc2);
482 rc2 = RTHandleClose(&hChildStdIn); AssertRC(rc2);
483 rc2 = RTPipeClose(hPipeStdErrR); AssertRC(rc2);
484 rc2 = RTPipeClose(hPipeStdOutR); AssertRC(rc2);
485 return rc;
486}
487
488
489/**
490 * Runs a process, waiting for it to complete.
491 *
492 * @returns IPRT status code
493 *
494 * @param pszExec Executable image to use to create the child process.
495 * @param papszArgs Pointer to an array of arguments to the child. The
496 * array terminated by an entry containing NULL.
497 * @param hEnv Handle to the environment block for the child.
498 * @param fFlags A combination of RTPROCEXEC_FLAGS_XXX.
499 * @param pStatus Where to return the status on success.
500 */
501int RTProcExec(const char *pszExec, const char * const *papszArgs, RTENV hEnv, uint32_t fFlags,
502 PRTPROCSTATUS pStatus)
503{
504 int rc;
505
506 /*
507 * Clear output argument (no returning failure here, simply crash!).
508 */
509 AssertPtr(pStatus);
510 pStatus->enmReason = RTPROCEXITREASON_ABEND;
511 pStatus->iStatus = RTEXITCODE_FAILURE;
512
513 /*
514 * Check input arguments.
515 */
516 AssertReturn(!(fFlags & ~RTPROCEXEC_FLAGS_VALID_MASK), VERR_INVALID_PARAMETER);
517
518 /*
519 * Set up /dev/null redirections.
520 */
521 PRTHANDLE aph[3] = { NULL, NULL, NULL };
522 RTHANDLE ah[3];
523 for (uint32_t i = 0; i < 3; i++)
524 {
525 ah[i].enmType = RTHANDLETYPE_FILE;
526 ah[i].u.hFile = NIL_RTFILE;
527 }
528 rc = VINF_SUCCESS;
529 if ((fFlags & RTPROCEXEC_FLAGS_STDIN_NULL) && RT_SUCCESS(rc))
530 {
531 aph[0] = &ah[0];
532 rc = RTFileOpenBitBucket(&ah[0].u.hFile, RTFILE_O_READ);
533 }
534 if ((fFlags & RTPROCEXEC_FLAGS_STDOUT_NULL) && RT_SUCCESS(rc))
535 {
536 aph[1] = &ah[1];
537 rc = RTFileOpenBitBucket(&ah[1].u.hFile, RTFILE_O_WRITE);
538 }
539 if ((fFlags & RTPROCEXEC_FLAGS_STDERR_NULL) && RT_SUCCESS(rc))
540 {
541 aph[2] = &ah[2];
542 rc = RTFileOpenBitBucket(&ah[2].u.hFile, RTFILE_O_WRITE);
543 }
544
545 /*
546 * Create the process.
547 */
548 RTPROCESS hProc = NIL_RTPROCESS;
549 if (RT_SUCCESS(rc))
550 rc = RTProcCreateEx(pszExec,
551 papszArgs,
552 hEnv,
553 0 /*fFlags*/,
554 aph[0],
555 aph[1],
556 aph[2],
557 NULL /*pszAsUser*/,
558 NULL /*pszPassword*/,
559 NULL /*pvExtraData*/,
560 &hProc);
561
562 for (uint32_t i = 0; i < 3; i++)
563 RTFileClose(ah[i].u.hFile);
564
565 if (RT_SUCCESS(rc))
566 rc = RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, pStatus);
567 return rc;
568}
569
570
571
572/**
573 * Executes SVN and gets the output.
574 *
575 * Standard error is suppressed.
576 *
577 * @returns VINF_SUCCESS if the command executed successfully.
578 * @param pState The rewrite state to work on. Can be NULL.
579 * @param papszArgs The SVN argument.
580 * @param fNormalFailureOk Whether normal failure is ok.
581 * @param ppszStdOut Where to return the output on success.
582 */
583static int scmSvnRunAndGetOutput(PSCMRWSTATE pState, const char **papszArgs, bool fNormalFailureOk, char **ppszStdOut)
584{
585 *ppszStdOut = NULL;
586
587#ifdef SCM_WITH_DYNAMIC_LIB_SVN
588 scmSvnFlushClientContextAndPool();
589#endif
590
591 char *pszCmdLine = NULL;
592 int rc = RTGetOptArgvToString(&pszCmdLine, papszArgs, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH);
593 if (RT_FAILURE(rc))
594 return rc;
595 ScmVerbose(pState, 2, "executing: %s\n", pszCmdLine);
596
597 RTPROCSTATUS Status;
598 rc = RTProcExecToString(g_szSvnPath, papszArgs, RTENV_DEFAULT,
599 RTPROCEXEC_FLAGS_STD_NULL, &Status, ppszStdOut, NULL);
600
601 if ( RT_SUCCESS(rc)
602 && ( Status.enmReason != RTPROCEXITREASON_NORMAL
603 || Status.iStatus != 0) )
604 {
605 if (fNormalFailureOk || Status.enmReason != RTPROCEXITREASON_NORMAL)
606 RTMsgError("%s: %s -> %s %u\n",
607 pState ? pState->pszFilename : "<NONE>", pszCmdLine,
608 Status.enmReason == RTPROCEXITREASON_NORMAL ? "exit code"
609 : Status.enmReason == RTPROCEXITREASON_SIGNAL ? "signal"
610 : Status.enmReason == RTPROCEXITREASON_ABEND ? "abnormal end"
611 : "abducted by alien",
612 Status.iStatus);
613 rc = VERR_GENERAL_FAILURE;
614 }
615 else if (RT_FAILURE(rc))
616 {
617 if (pState)
618 RTMsgError("%s: executing: %s => %Rrc\n", pState->pszFilename, pszCmdLine, rc);
619 else
620 RTMsgError("executing: %s => %Rrc\n", pszCmdLine, rc);
621 }
622
623 if (RT_FAILURE(rc))
624 {
625 RTStrFree(*ppszStdOut);
626 *ppszStdOut = NULL;
627 }
628 RTStrFree(pszCmdLine);
629 return rc;
630}
631
632
633/**
634 * Executes SVN.
635 *
636 * Standard error and standard output is suppressed.
637 *
638 * @returns VINF_SUCCESS if the command executed successfully.
639 * @param pState The rewrite state to work on.
640 * @param papszArgs The SVN argument.
641 * @param fNormalFailureOk Whether normal failure is ok.
642 */
643static int scmSvnRun(PSCMRWSTATE pState, const char **papszArgs, bool fNormalFailureOk)
644{
645#ifdef SCM_WITH_DYNAMIC_LIB_SVN
646 scmSvnFlushClientContextAndPool();
647#endif
648
649 char *pszCmdLine = NULL;
650 int rc = RTGetOptArgvToString(&pszCmdLine, papszArgs, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH);
651 if (RT_FAILURE(rc))
652 return rc;
653 ScmVerbose(pState, 2, "executing: %s\n", pszCmdLine);
654
655 /* Lazy bird uses RTProcExec. */
656 RTPROCSTATUS Status;
657 rc = RTProcExec(g_szSvnPath, papszArgs, RTENV_DEFAULT, RTPROCEXEC_FLAGS_STD_NULL, &Status);
658
659 if ( RT_SUCCESS(rc)
660 && ( Status.enmReason != RTPROCEXITREASON_NORMAL
661 || Status.iStatus != 0) )
662 {
663 if (fNormalFailureOk || Status.enmReason != RTPROCEXITREASON_NORMAL)
664 RTMsgError("%s: %s -> %s %u\n",
665 pState->pszFilename,
666 pszCmdLine,
667 Status.enmReason == RTPROCEXITREASON_NORMAL ? "exit code"
668 : Status.enmReason == RTPROCEXITREASON_SIGNAL ? "signal"
669 : Status.enmReason == RTPROCEXITREASON_ABEND ? "abnormal end"
670 : "abducted by alien",
671 Status.iStatus);
672 rc = VERR_GENERAL_FAILURE;
673 }
674 else if (RT_FAILURE(rc))
675 RTMsgError("%s: %s -> %Rrc\n", pState->pszFilename, pszCmdLine, rc);
676
677 RTStrFree(pszCmdLine);
678 return rc;
679}
680
681
682#ifdef SCM_WITH_DYNAMIC_LIB_SVN
683/**
684 * Attempts to resolve the necessary subversion and apache portable runtime APIs
685 * we require dynamically.
686 *
687 * Will set all global function pointers and g_fSvnFunctionPointersValid to true
688 * on success.
689 */
690static void scmSvnTryResolveFunctions(void)
691{
692 char szPath[RTPATH_MAX];
693 int rc = RTStrCopy(szPath, sizeof(szPath), g_szSvnPath);
694 if (RT_SUCCESS(rc))
695 {
696 RTPathStripFilename(szPath);
697 char *pszEndPath = strchr(szPath, '\0');
698# ifdef RT_OS_WINDOWS
699 RTPathChangeToDosSlashes(szPath, false);
700# endif
701
702 /*
703 * Try various prefixes/suffxies/locations.
704 */
705 static struct
706 {
707 const char *pszPrefix;
708 const char *pszSuffix;
709 } const s_aVariations[] =
710 {
711# ifdef RT_OS_WINDOWS
712 { "SlikSvn-lib", "-1.dll" }, /* SlikSVN */
713 { "lib", "-1.dll" }, /* Win32Svn,CollabNet,++ */
714# elif defined(RT_OS_DARWIN)
715 { "../lib/lib", "-1.dylib" },
716# else
717 { "../lib/lib", ".so" },
718 { "../lib/lib", "-1.so" },
719# if ARCH_BITS == 32
720 { "../lib32/lib", ".so" },
721 { "../lib32/lib", "-1.so" },
722# else
723 { "../lib64/lib", ".so" },
724 { "../lib64/lib", "-1.so" },
725# endif
726# ifdef RT_ARCH_X86
727 { "../lib/i386-linux-gnu/lib", ".so" },
728 { "../lib/i386-linux-gnu/lib", "-1.so" },
729# else
730 { "../lib/x86_64-linux-gnu/lib", ".so" },
731 { "../lib/x86_64-linux-gnu/lib", "-1.so" },
732# endif
733# endif
734 };
735 for (unsigned iVar = 0; iVar < RT_ELEMENTS(s_aVariations); iVar++)
736 {
737 /*
738 * Try load the svn_client library ...
739 */
740 static const char * const s_apszLibraries[] = { "svn_client", "svn_subr", "apr" };
741 RTLDRMOD ahMods[RT_ELEMENTS(s_apszLibraries)] = { NIL_RTLDRMOD, NIL_RTLDRMOD, NIL_RTLDRMOD };
742
743 rc = VINF_SUCCESS;
744 unsigned iLib;
745 for (iLib = 0; iLib < RT_ELEMENTS(s_apszLibraries) && RT_SUCCESS(rc); iLib++)
746 {
747 static const char * const s_apszSuffixes[] = { "", ".0", ".1" };
748 for (unsigned iSuff = 0; iSuff < RT_ELEMENTS(s_apszSuffixes); iSuff++)
749 {
750 *pszEndPath = '\0';
751 rc = RTPathAppend(szPath, sizeof(szPath), s_aVariations[iVar].pszPrefix);
752 if (RT_SUCCESS(rc))
753 rc = RTStrCat(szPath, sizeof(szPath), s_apszLibraries[iLib]);
754 if (RT_SUCCESS(rc))
755 rc = RTStrCat(szPath, sizeof(szPath), s_aVariations[iVar].pszSuffix);
756 if (RT_SUCCESS(rc))
757 rc = RTStrCat(szPath, sizeof(szPath), s_apszSuffixes[iSuff]);
758 if (RT_SUCCESS(rc))
759 {
760# ifdef RT_OS_WINDOWS
761 RTPathChangeToDosSlashes(pszEndPath, false);
762# endif
763 rc = RTLdrLoadEx(szPath, &ahMods[iLib], RTLDRLOAD_FLAGS_NT_SEARCH_DLL_LOAD_DIR , NULL);
764 if (RT_SUCCESS(rc))
765 {
766 RTMEM_WILL_LEAK(ahMods[iLib]);
767 break;
768 }
769 }
770 }
771 }
772 if (iLib == RT_ELEMENTS(s_apszLibraries) && RT_SUCCESS(rc))
773 {
774 static const struct
775 {
776 unsigned iLib;
777 const char *pszSymbol;
778 uintptr_t *ppfn; /**< The nothrow attrib of PFNRT goes down the wrong way with Clang 11, thus uintptr_t. */
779 } s_aSymbols[] =
780 {
781 { 2, "apr_initialize", (uintptr_t *)&g_pfnAprInitialize },
782 { 2, "apr_hash_first", (uintptr_t *)&g_pfnAprHashFirst },
783 { 2, "apr_hash_next", (uintptr_t *)&g_pfnAprHashNext },
784 { 2, "apr_hash_this_val", (uintptr_t *)&g_pfnAprHashThisVal },
785 { 1, "svn_pool_create_ex", (uintptr_t *)&g_pfnSvnPoolCreateEx },
786 { 2, "apr_pool_clear", (uintptr_t *)&g_pfnAprPoolClear },
787 { 2, "apr_pool_destroy", (uintptr_t *)&g_pfnAprPoolDestroy },
788 { 0, "svn_client_create_context", (uintptr_t *)&g_pfnSvnClientCreateContext },
789 { 0, "svn_client_propget4", (uintptr_t *)&g_pfnSvnClientPropGet4 },
790 };
791 for (unsigned i = 0; i < RT_ELEMENTS(s_aSymbols); i++)
792 {
793 rc = RTLdrGetSymbol(ahMods[s_aSymbols[i].iLib], s_aSymbols[i].pszSymbol,
794 (void **)(uintptr_t)s_aSymbols[i].ppfn);
795 if (RT_FAILURE(rc))
796 {
797 ScmVerbose(NULL, 0, "Failed to resolve '%s' in '%s'",
798 s_aSymbols[i].pszSymbol, s_apszLibraries[s_aSymbols[i].iLib]);
799 break;
800 }
801 }
802 if (RT_SUCCESS(rc))
803 {
804 apr_status_t rcApr = g_pfnAprInitialize();
805 if (rcApr == 0)
806 {
807 ScmVerbose(NULL, 1, "Found subversion APIs.\n");
808 g_fSvnFunctionPointersValid = true;
809 }
810 else
811 {
812 ScmVerbose(NULL, 0, "apr_initialize failed: %#x (%d)\n", rcApr, rcApr);
813 AssertMsgFailed(("%#x (%d)\n", rc, rc));
814 }
815 return;
816 }
817 }
818
819 while (iLib-- > 0)
820 RTLdrClose(ahMods[iLib]);
821 }
822 }
823}
824#endif /* SCM_WITH_DYNAMIC_LIB_SVN */
825
826
827/**
828 * Finds the svn binary, updating g_szSvnPath and g_enmSvnVersion.
829 */
830static void scmSvnFindSvnBinary(PSCMRWSTATE pState)
831{
832 /* Already been called? */
833 if (g_szSvnPath[0] != '\0')
834 return;
835
836 /*
837 * Locate it.
838 */
839 /** @todo code page fun... */
840#ifdef RT_OS_WINDOWS
841 const char *pszEnvVar = RTEnvGet("Path");
842#else
843 const char *pszEnvVar = RTEnvGet("PATH");
844#endif
845 if (pszEnvVar)
846 {
847#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
848 int rc = RTPathTraverseList(pszEnvVar, ';', scmSvnFindSvnBinaryCallback, g_szSvnPath, (void *)sizeof(g_szSvnPath));
849#else
850 int rc = RTPathTraverseList(pszEnvVar, ':', scmSvnFindSvnBinaryCallback, g_szSvnPath, (void *)sizeof(g_szSvnPath));
851#endif
852 if (RT_FAILURE(rc))
853 strcpy(g_szSvnPath, "svn");
854 }
855 else
856 strcpy(g_szSvnPath, "svn");
857
858 /*
859 * Check the version.
860 */
861 const char *apszArgs[] = { g_szSvnPath, "--version", "--quiet", NULL };
862 char *pszVersion;
863 int rc = scmSvnRunAndGetOutput(pState, apszArgs, false, &pszVersion);
864 if (RT_SUCCESS(rc))
865 {
866 char *pszStripped = RTStrStrip(pszVersion);
867 if (RTStrVersionCompare(pszStripped, "1.8") >= 0)
868 g_enmSvnVersion = kScmSvnVersion_1_8;
869 else if (RTStrVersionCompare(pszStripped, "1.7") >= 0)
870 g_enmSvnVersion = kScmSvnVersion_1_7;
871 else if (RTStrVersionCompare(pszStripped, "1.6") >= 0)
872 g_enmSvnVersion = kScmSvnVersion_1_6;
873 else
874 g_enmSvnVersion = kScmSvnVersion_Ancient;
875 RTStrFree(pszVersion);
876 }
877 else
878 g_enmSvnVersion = kScmSvnVersion_Ancient;
879
880#ifdef SCM_WITH_DYNAMIC_LIB_SVN
881 /*
882 * If we got version 1.8 or later, try see if we can locate a few of the
883 * simpler SVN APIs.
884 */
885 g_fSvnFunctionPointersValid = false;
886 if (g_enmSvnVersion >= kScmSvnVersion_1_8)
887 scmSvnTryResolveFunctions();
888#endif
889}
890
891
892/**
893 * Construct a dot svn filename for the file being rewritten.
894 *
895 * @returns IPRT status code.
896 * @param pState The rewrite state (for the name).
897 * @param pszDir The directory, including ".svn/".
898 * @param pszSuff The filename suffix.
899 * @param pszDst The output buffer. RTPATH_MAX in size.
900 */
901static int scmSvnConstructName(PSCMRWSTATE pState, const char *pszDir, const char *pszSuff, char *pszDst)
902{
903 strcpy(pszDst, pState->pszFilename); /* ASSUMES sizeof(szBuf) <= sizeof(szPath) */
904 RTPathStripFilename(pszDst);
905
906 int rc = RTPathAppend(pszDst, RTPATH_MAX, pszDir);
907 if (RT_SUCCESS(rc))
908 {
909 rc = RTPathAppend(pszDst, RTPATH_MAX, RTPathFilename(pState->pszFilename));
910 if (RT_SUCCESS(rc))
911 {
912 size_t cchDst = strlen(pszDst);
913 size_t cchSuff = strlen(pszSuff);
914 if (cchDst + cchSuff < RTPATH_MAX)
915 {
916 memcpy(&pszDst[cchDst], pszSuff, cchSuff + 1);
917 return VINF_SUCCESS;
918 }
919 else
920 rc = VERR_BUFFER_OVERFLOW;
921 }
922 }
923 return rc;
924}
925
926/**
927 * Interprets the specified string as decimal numbers.
928 *
929 * @returns true if parsed successfully, false if not.
930 * @param pch The string (not terminated).
931 * @param cch The string length.
932 * @param pu Where to return the value.
933 */
934static bool scmSvnReadNumber(const char *pch, size_t cch, size_t *pu)
935{
936 size_t u = 0;
937 while (cch-- > 0)
938 {
939 char ch = *pch++;
940 if (ch < '0' || ch > '9')
941 return false;
942 u *= 10;
943 u += ch - '0';
944 }
945 *pu = u;
946 return true;
947}
948
949
950#ifdef SCM_WITH_DYNAMIC_LIB_SVN
951
952/**
953 * Wrapper around RTPathAbs.
954 * @returns Same as RTPathAbs.
955 * @param pszPath The relative path.
956 * @param pszAbsPath Where to return the absolute path.
957 * @param cbAbsPath Size of the @a pszAbsPath buffer.
958 */
959static int scmSvnAbsPath(const char *pszPath, char *pszAbsPath, size_t cbAbsPath)
960{
961 int rc = RTPathAbs(pszPath, pszAbsPath, cbAbsPath);
962# if RTPATH_STYLE == RTPATH_STR_F_STYLE_DOS
963 if (RT_SUCCESS(rc))
964 {
965 RTPathChangeToUnixSlashes(pszAbsPath, true /*fForce*/);
966 /* To avoid: svn: E235000: In file '..\..\..\subversion\libsvn_client\prop_commands.c' line 796: assertion failed (svn_dirent_is_absolute(target)) */
967 if (pszAbsPath[1] == ':')
968 pszAbsPath[0] = RT_C_TO_UPPER(pszAbsPath[0]);
969 }
970# endif
971 return rc;
972}
973
974
975/**
976 * Gets a client context and pool.
977 *
978 * This implements caching.
979 *
980 * @returns IPRT status code.
981 * @param ppCtx Where to return the context
982 * @param ppPool Where to return the pool.
983 */
984static int scmSvnGetClientContextAndPool(svn_client_ctx_t **ppCtx, apr_pool_t **ppPool)
985{
986 /*
987 * Use cached if present.
988 */
989 if (g_pSvnClientCtx && g_pSvnPool)
990 {
991 g_cSvnClientCtxUsed++;
992 *ppCtx = g_pSvnClientCtx;
993 *ppPool = g_pSvnPool;
994 return VINF_SUCCESS;
995 }
996 Assert(!g_pSvnClientCtx);
997 Assert(!g_pSvnPool);
998
999 /*
1000 * Create new pool and context.
1001 */
1002 apr_pool_t *pPool = g_pfnSvnPoolCreateEx(NULL, NULL);
1003 if (pPool)
1004 {
1005 svn_client_ctx_t *pCtx = NULL;
1006 svn_error_t *pErr = g_pfnSvnClientCreateContext(&pCtx, pPool);
1007 if (!pErr)
1008 {
1009 g_cSvnClientCtxUsed = 1;
1010 g_pSvnClientCtx = *ppCtx = pCtx;
1011 g_pSvnPool = *ppPool = pPool;
1012 return VINF_SUCCESS;
1013 }
1014 g_pfnAprPoolDestroy(pPool);
1015 }
1016
1017 *ppCtx = NULL;
1018 *ppPool = NULL;
1019 return VERR_GENERAL_FAILURE;
1020}
1021
1022
1023/**
1024 * Puts back a client context and pool after use.
1025 *
1026 * @param pCtx The context.
1027 * @param pPool The pool.
1028 * @param fFlush Whether to flush it.
1029 */
1030static void scmSvnPutClientContextAndPool(svn_client_ctx_t *pCtx, apr_pool_t *pPool, bool fFlush)
1031{
1032 if (fFlush || g_cSvnClientCtxUsed > 4096) /* Disable this to force new context every time. */
1033 {
1034 g_pfnAprPoolDestroy(pPool);
1035 g_pSvnPool = NULL;
1036 g_pSvnClientCtx = NULL;
1037 }
1038 RT_NOREF(pCtx, fFlush);
1039}
1040
1041
1042/**
1043 * Flushes the cached client context and pool
1044 */
1045static void scmSvnFlushClientContextAndPool(void)
1046{
1047 if (g_pSvnPool)
1048 scmSvnPutClientContextAndPool(g_pSvnClientCtx, g_pSvnPool, true /*fFlush*/);
1049 Assert(!g_pSvnPool);
1050}
1051
1052
1053/**
1054 * Checks if @a pszPath exists in the current WC.
1055 *
1056 * @returns true, false or -1. In the latter case, please use the fallback.
1057 * @param pszPath Path to the object that should be investigated.
1058 */
1059static int scmSvnIsObjectInWorkingCopy(const char *pszPath)
1060{
1061 /* svn_client_propget4 and later requires absolute target path. */
1062 char szAbsPath[RTPATH_MAX];
1063 int rc = scmSvnAbsPath(pszPath, szAbsPath, sizeof(szAbsPath));
1064 if (RT_SUCCESS(rc))
1065 {
1066 apr_pool_t *pPool;
1067 svn_client_ctx_t *pCtx = NULL;
1068 rc = scmSvnGetClientContextAndPool(&pCtx, &pPool);
1069 if (RT_SUCCESS(rc))
1070 {
1071 /* Make the call. */
1072 apr_hash_t *pHash = NULL;
1073 svn_opt_revision_t Rev;
1074 RT_ZERO(Rev);
1075 Rev.kind = svn_opt_revision_working;
1076 Rev.value.number = -1L;
1077 svn_error_t *pErr = g_pfnSvnClientPropGet4(&pHash, "svn:no-such-property", szAbsPath, &Rev, &Rev,
1078 NULL /*pActualRev*/, svn_depth_empty, NULL /*pChangeList*/,
1079 pCtx, pPool, pPool);
1080 if (!pErr)
1081 rc = true;
1082 else if (pErr->apr_err == SVN_ERR_UNVERSIONED_RESOURCE)
1083 rc = false;
1084
1085 scmSvnPutClientContextAndPool(pCtx, pPool, false);
1086 }
1087 }
1088 return rc;
1089}
1090
1091#endif /* SCM_WITH_DYNAMIC_LIB_SVN */
1092
1093
1094/**
1095 * Checks if the file we're operating on is part of a SVN working copy.
1096 *
1097 * @returns true if it is, false if it isn't or we cannot tell.
1098 * @param pState The rewrite state to work on. Will use the
1099 * fIsInSvnWorkingCopy member for caching the result.
1100 */
1101bool ScmSvnIsInWorkingCopy(PSCMRWSTATE pState)
1102{
1103 /*
1104 * We don't ask SVN twice as that's expensive.
1105 */
1106 if (pState->fIsInSvnWorkingCopy != 0)
1107 return pState->fIsInSvnWorkingCopy > 0;
1108
1109#ifdef SCM_WITH_DYNAMIC_LIB_SVN
1110 if (g_fSvnFunctionPointersValid)
1111 {
1112 int rc = scmSvnIsObjectInWorkingCopy(pState->pszFilename);
1113 if (rc == (int)true || rc == (int)false)
1114 {
1115 pState->fIsInSvnWorkingCopy = rc == (int)true ? 1 : -1;
1116 return rc == (int)true;
1117 }
1118 }
1119
1120 /* Fallback: */
1121#endif
1122 if (g_enmSvnVersion < kScmSvnVersion_1_7)
1123 {
1124 /*
1125 * Hack: check if the .svn/text-base/<file>.svn-base file exists.
1126 */
1127 char szPath[RTPATH_MAX];
1128 int rc = scmSvnConstructName(pState, ".svn/text-base/", ".svn-base", szPath);
1129 if (RT_SUCCESS(rc))
1130 {
1131 if (RTFileExists(szPath))
1132 {
1133 pState->fIsInSvnWorkingCopy = 1;
1134 return true;
1135 }
1136 }
1137 }
1138 else
1139 {
1140 const char *apszArgs[] = { g_szSvnPath, "proplist", pState->pszFilename, NULL };
1141 char *pszValue;
1142 int rc = scmSvnRunAndGetOutput(pState, apszArgs, true, &pszValue);
1143 if (RT_SUCCESS(rc))
1144 {
1145 RTStrFree(pszValue);
1146 pState->fIsInSvnWorkingCopy = 1;
1147 return true;
1148 }
1149 }
1150 pState->fIsInSvnWorkingCopy = -1;
1151 return false;
1152}
1153
1154
1155/**
1156 * Checks if the specified directory is part of a SVN working copy.
1157 *
1158 * @returns true if it is, false if it isn't or we cannot tell.
1159 * @param pszDir The directory in question.
1160 */
1161bool ScmSvnIsDirInWorkingCopy(const char *pszDir)
1162{
1163#ifdef SCM_WITH_DYNAMIC_LIB_SVN
1164 if (g_fSvnFunctionPointersValid)
1165 {
1166 int rc = scmSvnIsObjectInWorkingCopy(pszDir);
1167 if (rc == (int)true || rc == (int)false)
1168 return rc == (int)true;
1169 }
1170
1171 /* Fallback: */
1172#endif
1173 if (g_enmSvnVersion < kScmSvnVersion_1_7)
1174 {
1175 /*
1176 * Hack: check if the .svn/ dir exists.
1177 */
1178 char szPath[RTPATH_MAX];
1179 int rc = RTPathJoin(szPath, sizeof(szPath), pszDir, ".svn");
1180 if (RT_SUCCESS(rc))
1181 return RTDirExists(szPath);
1182 }
1183 else
1184 {
1185 const char *apszArgs[] = { g_szSvnPath, "propget", "svn:no-such-property", pszDir, NULL };
1186 char *pszValue;
1187 int rc = scmSvnRunAndGetOutput(NULL, apszArgs, true, &pszValue);
1188 if (RT_SUCCESS(rc))
1189 {
1190 RTStrFree(pszValue);
1191 return true;
1192 }
1193 }
1194 return false;
1195}
1196
1197
1198#ifdef SCM_WITH_DYNAMIC_LIB_SVN
1199/**
1200 * Checks if @a pszPath exists in the current WC.
1201 *
1202 * @returns IPRT status code - VERR_NOT_SUPPORT if fallback should be attempted.
1203 * @param pszPath Path to the object that should be investigated.
1204 * @param pszProperty The property name.
1205 * @param ppszValue Where to return the property value. Optional.
1206 */
1207static int scmSvnQueryPropertyUsingApi(const char *pszPath, const char *pszProperty, char **ppszValue)
1208{
1209 /* svn_client_propget4 and later requires absolute target path. */
1210 char szAbsPath[RTPATH_MAX];
1211 int rc = scmSvnAbsPath(pszPath, szAbsPath, sizeof(szAbsPath));
1212 if (RT_SUCCESS(rc))
1213 {
1214 apr_pool_t *pPool;
1215 svn_client_ctx_t *pCtx = NULL;
1216 rc = scmSvnGetClientContextAndPool(&pCtx, &pPool);
1217 if (RT_SUCCESS(rc))
1218 {
1219 /* Make the call. */
1220 apr_hash_t *pHash = NULL;
1221 svn_opt_revision_t Rev;
1222 RT_ZERO(Rev);
1223 Rev.kind = svn_opt_revision_working;
1224 Rev.value.number = -1L;
1225 svn_error_t *pErr = g_pfnSvnClientPropGet4(&pHash, pszProperty, szAbsPath, &Rev, &Rev,
1226 NULL /*pActualRev*/, svn_depth_empty, NULL /*pChangeList*/,
1227 pCtx, pPool, pPool);
1228 if (!pErr)
1229 {
1230 /* Get the first value, if any. */
1231 rc = VERR_NOT_FOUND;
1232 apr_hash_index_t *pHashIdx = g_pfnAprHashFirst(pPool, pHash);
1233 if (pHashIdx)
1234 {
1235 const char **ppszFirst = (const char **)g_pfnAprHashThisVal(pHashIdx);
1236 if (ppszFirst && *ppszFirst)
1237 {
1238 if (ppszValue)
1239 rc = RTStrDupEx(ppszValue, *ppszFirst);
1240 else
1241 rc = VINF_SUCCESS;
1242 }
1243 }
1244 }
1245 else if (pErr->apr_err == SVN_ERR_UNVERSIONED_RESOURCE)
1246 rc = VERR_INVALID_STATE;
1247 else
1248 rc = VERR_GENERAL_FAILURE;
1249
1250 scmSvnPutClientContextAndPool(pCtx, pPool, false);
1251 }
1252 }
1253 return rc;
1254}
1255#endif /* SCM_WITH_DYNAMIC_LIB_SVN */
1256
1257
1258/**
1259 * Queries the value of an SVN property.
1260 *
1261 * This will automatically adjust for scheduled changes.
1262 *
1263 * @returns IPRT status code.
1264 * @retval VERR_INVALID_STATE if not a SVN WC file.
1265 * @retval VERR_NOT_FOUND if the property wasn't found.
1266 * @param pState The rewrite state to work on.
1267 * @param pszName The property name.
1268 * @param ppszValue Where to return the property value. Free this
1269 * using RTStrFree. Optional.
1270 */
1271int ScmSvnQueryProperty(PSCMRWSTATE pState, const char *pszName, char **ppszValue)
1272{
1273 int rc;
1274
1275 /*
1276 * Look it up in the scheduled changes.
1277 */
1278 size_t i = pState->cSvnPropChanges;
1279 while (i-- > 0)
1280 if (!strcmp(pState->paSvnPropChanges[i].pszName, pszName))
1281 {
1282 const char *pszValue = pState->paSvnPropChanges[i].pszValue;
1283 if (!pszValue)
1284 return VERR_NOT_FOUND;
1285 if (ppszValue)
1286 return RTStrDupEx(ppszValue, pszValue);
1287 return VINF_SUCCESS;
1288 }
1289
1290#ifdef SCM_WITH_DYNAMIC_LIB_SVN
1291 if (g_fSvnFunctionPointersValid)
1292 {
1293 rc = scmSvnQueryPropertyUsingApi(pState->pszFilename, pszName, ppszValue);
1294 if (rc != VERR_NOT_SUPPORTED)
1295 return rc;
1296 /* Fallback: */
1297 }
1298#endif
1299
1300 if (g_enmSvnVersion < kScmSvnVersion_1_7)
1301 {
1302 /*
1303 * Hack: Read the .svn/props/<file>.svn-work file exists.
1304 */
1305 char szPath[RTPATH_MAX];
1306 rc = scmSvnConstructName(pState, ".svn/props/", ".svn-work", szPath);
1307 if (RT_SUCCESS(rc) && !RTFileExists(szPath))
1308 rc = scmSvnConstructName(pState, ".svn/prop-base/", ".svn-base", szPath);
1309 if (RT_SUCCESS(rc))
1310 {
1311 SCMSTREAM Stream;
1312 rc = ScmStreamInitForReading(&Stream, szPath);
1313 if (RT_SUCCESS(rc))
1314 {
1315 /*
1316 * The current format is K len\n<name>\nV len\n<value>\n" ... END.
1317 */
1318 rc = VERR_NOT_FOUND;
1319 size_t const cchName = strlen(pszName);
1320 SCMEOL enmEol;
1321 size_t cchLine;
1322 const char *pchLine;
1323 while ((pchLine = ScmStreamGetLine(&Stream, &cchLine, &enmEol)) != NULL)
1324 {
1325 /*
1326 * Parse the 'K num' / 'END' line.
1327 */
1328 if ( cchLine == 3
1329 && !memcmp(pchLine, "END", 3))
1330 break;
1331 size_t cchKey;
1332 if ( cchLine < 3
1333 || pchLine[0] != 'K'
1334 || pchLine[1] != ' '
1335 || !scmSvnReadNumber(&pchLine[2], cchLine - 2, &cchKey)
1336 || cchKey == 0
1337 || cchKey > 4096)
1338 {
1339 RTMsgError("%s:%u: Unexpected data '%.*s'\n", szPath, ScmStreamTellLine(&Stream), cchLine, pchLine);
1340 rc = VERR_PARSE_ERROR;
1341 break;
1342 }
1343
1344 /*
1345 * Match the key and skip to the value line. Don't bother with
1346 * names containing EOL markers.
1347 */
1348 size_t const offKey = ScmStreamTell(&Stream);
1349 bool fMatch = cchName == cchKey;
1350 if (fMatch)
1351 {
1352 pchLine = ScmStreamGetLine(&Stream, &cchLine, &enmEol);
1353 if (!pchLine)
1354 break;
1355 fMatch = cchLine == cchName
1356 && !memcmp(pchLine, pszName, cchName);
1357 }
1358
1359 if (RT_FAILURE(ScmStreamSeekAbsolute(&Stream, offKey + cchKey)))
1360 break;
1361 if (RT_FAILURE(ScmStreamSeekByLine(&Stream, ScmStreamTellLine(&Stream) + 1)))
1362 break;
1363
1364 /*
1365 * Read and Parse the 'V num' line.
1366 */
1367 pchLine = ScmStreamGetLine(&Stream, &cchLine, &enmEol);
1368 if (!pchLine)
1369 break;
1370 size_t cchValue;
1371 if ( cchLine < 3
1372 || pchLine[0] != 'V'
1373 || pchLine[1] != ' '
1374 || !scmSvnReadNumber(&pchLine[2], cchLine - 2, &cchValue)
1375 || cchValue > _1M)
1376 {
1377 RTMsgError("%s:%u: Unexpected data '%.*s'\n", szPath, ScmStreamTellLine(&Stream), cchLine, pchLine);
1378 rc = VERR_PARSE_ERROR;
1379 break;
1380 }
1381
1382 /*
1383 * If we have a match, allocate a return buffer and read the
1384 * value into it. Otherwise skip this value and continue
1385 * searching.
1386 */
1387 if (fMatch)
1388 {
1389 if (!ppszValue)
1390 rc = VINF_SUCCESS;
1391 else
1392 {
1393 char *pszValue;
1394 rc = RTStrAllocEx(&pszValue, cchValue + 1);
1395 if (RT_SUCCESS(rc))
1396 {
1397 rc = ScmStreamRead(&Stream, pszValue, cchValue);
1398 if (RT_SUCCESS(rc))
1399 *ppszValue = pszValue;
1400 else
1401 RTStrFree(pszValue);
1402 }
1403 }
1404 break;
1405 }
1406
1407 if (RT_FAILURE(ScmStreamSeekRelative(&Stream, cchValue)))
1408 break;
1409 if (RT_FAILURE(ScmStreamSeekByLine(&Stream, ScmStreamTellLine(&Stream) + 1)))
1410 break;
1411 }
1412
1413 if (RT_FAILURE(ScmStreamGetStatus(&Stream)))
1414 {
1415 rc = ScmStreamGetStatus(&Stream);
1416 RTMsgError("%s: stream error %Rrc\n", szPath, rc);
1417 }
1418 ScmStreamDelete(&Stream);
1419 }
1420 }
1421
1422 if (rc == VERR_FILE_NOT_FOUND)
1423 rc = VERR_NOT_FOUND;
1424 }
1425 else
1426 {
1427 const char *apszArgs[] = { g_szSvnPath, "propget", "--strict", pszName, pState->pszFilename, NULL };
1428 char *pszValue;
1429 rc = scmSvnRunAndGetOutput(pState, apszArgs, false, &pszValue);
1430 if (RT_SUCCESS(rc))
1431 {
1432 if (pszValue && *pszValue)
1433 {
1434 if (ppszValue)
1435 {
1436 *ppszValue = pszValue;
1437 pszValue = NULL;
1438 }
1439 }
1440 else
1441 rc = VERR_NOT_FOUND;
1442 RTStrFree(pszValue);
1443 }
1444 }
1445 return rc;
1446}
1447
1448
1449/**
1450 * Queries the value of an SVN property on the parent dir/whatever.
1451 *
1452 * This will not adjust for scheduled changes to the parent!
1453 *
1454 * @returns IPRT status code.
1455 * @retval VERR_INVALID_STATE if not a SVN WC file.
1456 * @retval VERR_NOT_FOUND if the property wasn't found.
1457 * @param pState The rewrite state to work on.
1458 * @param pszName The property name.
1459 * @param ppszValue Where to return the property value. Free this
1460 * using RTStrFree. Optional.
1461 */
1462int ScmSvnQueryParentProperty(PSCMRWSTATE pState, const char *pszName, char **ppszValue)
1463{
1464 /*
1465 * Strip the filename and use ScmSvnQueryProperty.
1466 */
1467 char szPath[RTPATH_MAX];
1468 int rc = RTStrCopy(szPath, sizeof(szPath), pState->pszFilename);
1469 if (RT_SUCCESS(rc))
1470 {
1471 RTPathStripFilename(szPath);
1472 SCMRWSTATE ParentState;
1473 ParentState.pszFilename = szPath;
1474 ParentState.fFirst = false;
1475 ParentState.fIsInSvnWorkingCopy = true;
1476 ParentState.cSvnPropChanges = 0;
1477 ParentState.paSvnPropChanges = NULL;
1478 ParentState.rc = VINF_SUCCESS;
1479 rc = ScmSvnQueryProperty(&ParentState, pszName, ppszValue);
1480 if (RT_SUCCESS(rc))
1481 rc = ParentState.rc;
1482 }
1483 return rc;
1484}
1485
1486
1487/**
1488 * Schedules the setting of a property.
1489 *
1490 * @returns IPRT status code.
1491 * @retval VERR_INVALID_STATE if not a SVN WC file.
1492 * @param pState The rewrite state to work on.
1493 * @param pszName The name of the property to set.
1494 * @param pszValue The value. NULL means deleting it.
1495 */
1496int ScmSvnSetProperty(PSCMRWSTATE pState, const char *pszName, const char *pszValue)
1497{
1498 /*
1499 * Update any existing entry first.
1500 */
1501 size_t i = pState->cSvnPropChanges;
1502 while (i-- > 0)
1503 if (!strcmp(pState->paSvnPropChanges[i].pszName, pszName))
1504 {
1505 if (!pszValue)
1506 {
1507 RTStrFree(pState->paSvnPropChanges[i].pszValue);
1508 pState->paSvnPropChanges[i].pszValue = NULL;
1509 }
1510 else
1511 {
1512 char *pszCopy;
1513 int rc = RTStrDupEx(&pszCopy, pszValue);
1514 if (RT_FAILURE(rc))
1515 return rc;
1516 pState->paSvnPropChanges[i].pszValue = pszCopy;
1517 }
1518 return VINF_SUCCESS;
1519 }
1520
1521 /*
1522 * Insert a new entry.
1523 */
1524 i = pState->cSvnPropChanges;
1525 if ((i % 32) == 0)
1526 {
1527 void *pvNew = RTMemRealloc(pState->paSvnPropChanges, (i + 32) * sizeof(SCMSVNPROP));
1528 if (!pvNew)
1529 return VERR_NO_MEMORY;
1530 pState->paSvnPropChanges = (PSCMSVNPROP)pvNew;
1531 }
1532
1533 pState->paSvnPropChanges[i].pszName = RTStrDup(pszName);
1534 pState->paSvnPropChanges[i].pszValue = pszValue ? RTStrDup(pszValue) : NULL;
1535 if ( pState->paSvnPropChanges[i].pszName
1536 && (pState->paSvnPropChanges[i].pszValue || !pszValue) )
1537 pState->cSvnPropChanges = i + 1;
1538 else
1539 {
1540 RTStrFree(pState->paSvnPropChanges[i].pszName);
1541 pState->paSvnPropChanges[i].pszName = NULL;
1542 RTStrFree(pState->paSvnPropChanges[i].pszValue);
1543 pState->paSvnPropChanges[i].pszValue = NULL;
1544 return VERR_NO_MEMORY;
1545 }
1546 return VINF_SUCCESS;
1547}
1548
1549
1550/**
1551 * Schedules a property deletion.
1552 *
1553 * @returns IPRT status code.
1554 * @param pState The rewrite state to work on.
1555 * @param pszName The name of the property to delete.
1556 */
1557int ScmSvnDelProperty(PSCMRWSTATE pState, const char *pszName)
1558{
1559 return ScmSvnSetProperty(pState, pszName, NULL);
1560}
1561
1562
1563/**
1564 * Applies any SVN property changes to the work copy of the file.
1565 *
1566 * @returns IPRT status code.
1567 * @param pState The rewrite state which SVN property changes
1568 * should be applied.
1569 */
1570int ScmSvnDisplayChanges(PSCMRWSTATE pState)
1571{
1572 size_t i = pState->cSvnPropChanges;
1573 while (i-- > 0)
1574 {
1575 const char *pszName = pState->paSvnPropChanges[i].pszName;
1576 const char *pszValue = pState->paSvnPropChanges[i].pszValue;
1577 if (pszValue)
1578 ScmVerbose(pState, 0, "svn propset '%s' '%s' %s\n", pszName, pszValue, pState->pszFilename);
1579 else
1580 ScmVerbose(pState, 0, "svn propdel '%s' %s\n", pszName, pState->pszFilename);
1581 }
1582
1583 return VINF_SUCCESS;
1584}
1585
1586/**
1587 * Applies any SVN property changes to the work copy of the file.
1588 *
1589 * @returns IPRT status code.
1590 * @param pState The rewrite state which SVN property changes
1591 * should be applied.
1592 */
1593int ScmSvnApplyChanges(PSCMRWSTATE pState)
1594{
1595#ifdef SCM_WITH_LATER
1596 if (0)
1597 {
1598 return ...;
1599 }
1600
1601 /* Fallback: */
1602#endif
1603
1604 /*
1605 * Iterate thru the changes and apply them by starting the svn client.
1606 */
1607 for (size_t i = 0; i < pState->cSvnPropChanges; i++)
1608 {
1609 const char *apszArgv[6];
1610 apszArgv[0] = g_szSvnPath;
1611 apszArgv[1] = pState->paSvnPropChanges[i].pszValue ? "propset" : "propdel";
1612 apszArgv[2] = pState->paSvnPropChanges[i].pszName;
1613 int iArg = 3;
1614 if (pState->paSvnPropChanges[i].pszValue)
1615 apszArgv[iArg++] = pState->paSvnPropChanges[i].pszValue;
1616 apszArgv[iArg++] = pState->pszFilename;
1617 apszArgv[iArg++] = NULL;
1618
1619 int rc = scmSvnRun(pState, apszArgv, false);
1620 if (RT_FAILURE(rc))
1621 return rc;
1622 }
1623
1624 return VINF_SUCCESS;
1625}
1626
1627
1628/**
1629 * Initializes the subversion interface.
1630 */
1631void ScmSvnInit(void)
1632{
1633 scmSvnFindSvnBinary(NULL);
1634}
1635
1636
1637void ScmSvnTerm(void)
1638{
1639#ifdef SCM_WITH_DYNAMIC_LIB_SVN
1640 scmSvnFlushClientContextAndPool();
1641#endif
1642}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette