VirtualBox

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

Last change on this file since 62991 was 62854, checked in by vboxsync, 8 years ago

scm: warnings

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 50.3 KB
Line 
1/* $Id: scmsubversion.cpp 62854 2016-08-01 22:36:01Z vboxsync $ */
2/** @file
3 * IPRT Testcase / Tool - Source Code Massager, Subversion Access.
4 */
5
6/*
7 * Copyright (C) 2010-2016 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#endif
163
164
165
166/**
167 * Callback that is call for each path to search.
168 */
169static DECLCALLBACK(int) scmSvnFindSvnBinaryCallback(char const *pchPath, size_t cchPath, void *pvUser1, void *pvUser2)
170{
171 char *pszDst = (char *)pvUser1;
172 size_t cchDst = (size_t)pvUser2;
173 if (cchDst > cchPath)
174 {
175 memcpy(pszDst, pchPath, cchPath);
176 pszDst[cchPath] = '\0';
177#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
178 int rc = RTPathAppend(pszDst, cchDst, "svn.exe");
179#else
180 int rc = RTPathAppend(pszDst, cchDst, "svn");
181#endif
182 if ( RT_SUCCESS(rc)
183 && RTFileExists(pszDst))
184 return VINF_SUCCESS;
185 }
186 return VERR_TRY_AGAIN;
187}
188
189
190/**
191 * Reads from a pipe.
192 *
193 * @returns @a rc or other status code.
194 * @param rc The current status of the operation. Error status
195 * are preserved and returned.
196 * @param phPipeR Pointer to the pipe handle.
197 * @param pcbAllocated Pointer to the buffer size variable.
198 * @param poffCur Pointer to the buffer offset variable.
199 * @param ppszBuffer Pointer to the buffer pointer variable.
200 */
201static int rtProcProcessOutput(int rc, PRTPIPE phPipeR, size_t *pcbAllocated, size_t *poffCur, char **ppszBuffer,
202 RTPOLLSET hPollSet, uint32_t idPollSet)
203{
204 size_t cbRead;
205 char szTmp[_4K - 1];
206 for (;;)
207 {
208 int rc2 = RTPipeRead(*phPipeR, szTmp, sizeof(szTmp), &cbRead);
209 if (RT_SUCCESS(rc2) && cbRead)
210 {
211 /* Resize the buffer. */
212 if (*poffCur + cbRead >= *pcbAllocated)
213 {
214 if (*pcbAllocated >= _1G)
215 {
216 RTPollSetRemove(hPollSet, idPollSet);
217 rc2 = RTPipeClose(*phPipeR); AssertRC(rc2);
218 *phPipeR = NIL_RTPIPE;
219 return RT_SUCCESS(rc) ? VERR_TOO_MUCH_DATA : rc;
220 }
221
222 size_t cbNew = *pcbAllocated ? *pcbAllocated * 2 : sizeof(szTmp) + 1;
223 Assert(*poffCur + cbRead < cbNew);
224 rc2 = RTStrRealloc(ppszBuffer, cbNew);
225 if (RT_FAILURE(rc2))
226 {
227 RTPollSetRemove(hPollSet, idPollSet);
228 rc2 = RTPipeClose(*phPipeR); AssertRC(rc2);
229 *phPipeR = NIL_RTPIPE;
230 return RT_SUCCESS(rc) ? rc2 : rc;
231 }
232 *pcbAllocated = cbNew;
233 }
234
235 /* Append the new data, terminating it. */
236 memcpy(*ppszBuffer + *poffCur, szTmp, cbRead);
237 *poffCur += cbRead;
238 (*ppszBuffer)[*poffCur] = '\0';
239
240 /* Check for null terminators in the string. */
241 if (RT_SUCCESS(rc) && memchr(szTmp, '\0', cbRead))
242 rc = VERR_NO_TRANSLATION;
243
244 /* If we read a full buffer, try read some more. */
245 if (RT_SUCCESS(rc) && cbRead == sizeof(szTmp))
246 continue;
247 }
248 else if (rc2 != VINF_TRY_AGAIN)
249 {
250 if (RT_FAILURE(rc) && rc2 != VERR_BROKEN_PIPE)
251 rc = rc2;
252 RTPollSetRemove(hPollSet, idPollSet);
253 rc2 = RTPipeClose(*phPipeR); AssertRC(rc2);
254 *phPipeR = NIL_RTPIPE;
255 }
256 return rc;
257 }
258}
259
260/** @name RTPROCEXEC_FLAGS_XXX - flags for RTProcExec and RTProcExecToString.
261 * @{ */
262/** Redirect /dev/null to standard input. */
263#define RTPROCEXEC_FLAGS_STDIN_NULL RT_BIT_32(0)
264/** Redirect standard output to /dev/null. */
265#define RTPROCEXEC_FLAGS_STDOUT_NULL RT_BIT_32(1)
266/** Redirect standard error to /dev/null. */
267#define RTPROCEXEC_FLAGS_STDERR_NULL RT_BIT_32(2)
268/** Redirect all standard output to /dev/null as well as directing /dev/null
269 * to standard input. */
270#define RTPROCEXEC_FLAGS_STD_NULL ( RTPROCEXEC_FLAGS_STDIN_NULL \
271 | RTPROCEXEC_FLAGS_STDOUT_NULL \
272 | RTPROCEXEC_FLAGS_STDERR_NULL)
273/** Mask containing the valid flags. */
274#define RTPROCEXEC_FLAGS_VALID_MASK UINT32_C(0x00000007)
275/** @} */
276
277/**
278 * Runs a process, collecting the standard output and/or standard error.
279 *
280 *
281 * @returns IPRT status code
282 * @retval VERR_NO_TRANSLATION if the output of the program isn't valid UTF-8
283 * or contains a nul character.
284 * @retval VERR_TOO_MUCH_DATA if the process produced too much data.
285 *
286 * @param pszExec Executable image to use to create the child process.
287 * @param papszArgs Pointer to an array of arguments to the child. The
288 * array terminated by an entry containing NULL.
289 * @param hEnv Handle to the environment block for the child.
290 * @param fFlags A combination of RTPROCEXEC_FLAGS_XXX. The @a
291 * ppszStdOut and @a ppszStdErr parameters takes precedence
292 * over redirection flags.
293 * @param pStatus Where to return the status on success.
294 * @param ppszStdOut Where to return the text written to standard output. If
295 * NULL then standard output will not be collected and go
296 * to the standard output handle of the process.
297 * Free with RTStrFree, regardless of return status.
298 * @param ppszStdErr Where to return the text written to standard error. If
299 * NULL then standard output will not be collected and go
300 * to the standard error handle of the process.
301 * Free with RTStrFree, regardless of return status.
302 */
303int RTProcExecToString(const char *pszExec, const char * const *papszArgs, RTENV hEnv, uint32_t fFlags,
304 PRTPROCSTATUS pStatus, char **ppszStdOut, char **ppszStdErr)
305{
306 int rc2;
307
308 /*
309 * Clear output arguments (no returning failure here, simply crash!).
310 */
311 AssertPtr(pStatus);
312 pStatus->enmReason = RTPROCEXITREASON_ABEND;
313 pStatus->iStatus = RTEXITCODE_FAILURE;
314 AssertPtrNull(ppszStdOut);
315 if (ppszStdOut)
316 *ppszStdOut = NULL;
317 AssertPtrNull(ppszStdOut);
318 if (ppszStdErr)
319 *ppszStdErr = NULL;
320
321 /*
322 * Check input arguments.
323 */
324 AssertReturn(!(fFlags & ~RTPROCEXEC_FLAGS_VALID_MASK), VERR_INVALID_PARAMETER);
325
326 /*
327 * Do we need a standard input bitbucket?
328 */
329 int rc = VINF_SUCCESS;
330 PRTHANDLE phChildStdIn = NULL;
331 RTHANDLE hChildStdIn;
332 hChildStdIn.enmType = RTHANDLETYPE_FILE;
333 hChildStdIn.u.hFile = NIL_RTFILE;
334 if ((fFlags & RTPROCEXEC_FLAGS_STDIN_NULL) && RT_SUCCESS(rc))
335 {
336 phChildStdIn = &hChildStdIn;
337 rc = RTFileOpenBitBucket(&hChildStdIn.u.hFile, RTFILE_O_READ);
338 }
339
340 /*
341 * Create the output pipes / bitbuckets.
342 */
343 RTPIPE hPipeStdOutR = NIL_RTPIPE;
344 PRTHANDLE phChildStdOut = NULL;
345 RTHANDLE hChildStdOut;
346 hChildStdOut.enmType = RTHANDLETYPE_PIPE;
347 hChildStdOut.u.hPipe = NIL_RTPIPE;
348 if (ppszStdOut && RT_SUCCESS(rc))
349 {
350 phChildStdOut = &hChildStdOut;
351 rc = RTPipeCreate(&hPipeStdOutR, &hChildStdOut.u.hPipe, 0 /*fFlags*/);
352 }
353 else if ((fFlags & RTPROCEXEC_FLAGS_STDOUT_NULL) && RT_SUCCESS(rc))
354 {
355 phChildStdOut = &hChildStdOut;
356 hChildStdOut.enmType = RTHANDLETYPE_FILE;
357 hChildStdOut.u.hFile = NIL_RTFILE;
358 rc = RTFileOpenBitBucket(&hChildStdOut.u.hFile, RTFILE_O_WRITE);
359 }
360
361 RTPIPE hPipeStdErrR = NIL_RTPIPE;
362 PRTHANDLE phChildStdErr = NULL;
363 RTHANDLE hChildStdErr;
364 hChildStdErr.enmType = RTHANDLETYPE_PIPE;
365 hChildStdErr.u.hPipe = NIL_RTPIPE;
366 if (ppszStdErr && RT_SUCCESS(rc))
367 {
368 phChildStdErr = &hChildStdErr;
369 rc = RTPipeCreate(&hPipeStdErrR, &hChildStdErr.u.hPipe, 0 /*fFlags*/);
370 }
371 else if ((fFlags & RTPROCEXEC_FLAGS_STDERR_NULL) && RT_SUCCESS(rc))
372 {
373 phChildStdErr = &hChildStdErr;
374 hChildStdErr.enmType = RTHANDLETYPE_FILE;
375 hChildStdErr.u.hFile = NIL_RTFILE;
376 rc = RTFileOpenBitBucket(&hChildStdErr.u.hFile, RTFILE_O_WRITE);
377 }
378
379 if (RT_SUCCESS(rc))
380 {
381 RTPOLLSET hPollSet;
382 rc = RTPollSetCreate(&hPollSet);
383 if (RT_SUCCESS(rc))
384 {
385 if (hPipeStdOutR != NIL_RTPIPE && RT_SUCCESS(rc))
386 rc = RTPollSetAddPipe(hPollSet, hPipeStdOutR, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR, 1);
387 if (hPipeStdErrR != NIL_RTPIPE)
388 rc = RTPollSetAddPipe(hPollSet, hPipeStdErrR, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR, 2);
389 }
390 if (RT_SUCCESS(rc))
391 {
392 /*
393 * Create the process.
394 */
395 RTPROCESS hProc;
396 rc = RTProcCreateEx(pszExec,
397 papszArgs,
398 hEnv,
399 0 /*fFlags*/,
400 NULL /*phStdIn*/,
401 phChildStdOut,
402 phChildStdErr,
403 NULL /*pszAsUser*/,
404 NULL /*pszPassword*/,
405 &hProc);
406 rc2 = RTHandleClose(&hChildStdErr); AssertRC(rc2);
407 rc2 = RTHandleClose(&hChildStdOut); AssertRC(rc2);
408
409 if (RT_SUCCESS(rc))
410 {
411 /*
412 * Process output and wait for the process to finish.
413 */
414 size_t cbStdOut = 0;
415 size_t offStdOut = 0;
416 size_t cbStdErr = 0;
417 size_t offStdErr = 0;
418 for (;;)
419 {
420 if (hPipeStdOutR != NIL_RTPIPE)
421 rc = rtProcProcessOutput(rc, &hPipeStdOutR, &cbStdOut, &offStdOut, ppszStdOut, hPollSet, 1);
422 if (hPipeStdErrR != NIL_RTPIPE)
423 rc = rtProcProcessOutput(rc, &hPipeStdErrR, &cbStdErr, &offStdErr, ppszStdErr, hPollSet, 2);
424 if (hPipeStdOutR == NIL_RTPIPE && hPipeStdErrR == NIL_RTPIPE)
425 break;
426
427 if (hProc != NIL_RTPROCESS)
428 {
429 rc2 = RTProcWait(hProc, RTPROCWAIT_FLAGS_NOBLOCK, pStatus);
430 if (rc2 != VERR_PROCESS_RUNNING)
431 {
432 if (RT_FAILURE(rc2))
433 rc = rc2;
434 hProc = NIL_RTPROCESS;
435 }
436 }
437
438 rc2 = RTPoll(hPollSet, 10000, NULL, NULL);
439 Assert(RT_SUCCESS(rc2) || rc2 == VERR_TIMEOUT);
440 }
441
442 if (RT_SUCCESS(rc))
443 {
444 if ( (ppszStdOut && *ppszStdOut && !RTStrIsValidEncoding(*ppszStdOut))
445 || (ppszStdErr && *ppszStdErr && !RTStrIsValidEncoding(*ppszStdErr)) )
446 rc = VERR_NO_TRANSLATION;
447 }
448
449 /*
450 * No more output, just wait for it to finish.
451 */
452 if (hProc != NIL_RTPROCESS)
453 {
454 rc2 = RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, pStatus);
455 if (RT_FAILURE(rc2))
456 rc = rc2;
457 }
458 }
459 RTPollSetDestroy(hPollSet);
460 }
461 }
462
463 rc2 = RTHandleClose(&hChildStdErr); AssertRC(rc2);
464 rc2 = RTHandleClose(&hChildStdOut); AssertRC(rc2);
465 rc2 = RTHandleClose(&hChildStdIn); AssertRC(rc2);
466 rc2 = RTPipeClose(hPipeStdErrR); AssertRC(rc2);
467 rc2 = RTPipeClose(hPipeStdOutR); AssertRC(rc2);
468 return rc;
469}
470
471
472/**
473 * Runs a process, waiting for it to complete.
474 *
475 * @returns IPRT status code
476 *
477 * @param pszExec Executable image to use to create the child process.
478 * @param papszArgs Pointer to an array of arguments to the child. The
479 * array terminated by an entry containing NULL.
480 * @param hEnv Handle to the environment block for the child.
481 * @param fFlags A combination of RTPROCEXEC_FLAGS_XXX.
482 * @param pStatus Where to return the status on success.
483 */
484int RTProcExec(const char *pszExec, const char * const *papszArgs, RTENV hEnv, uint32_t fFlags,
485 PRTPROCSTATUS pStatus)
486{
487 int rc;
488
489 /*
490 * Clear output argument (no returning failure here, simply crash!).
491 */
492 AssertPtr(pStatus);
493 pStatus->enmReason = RTPROCEXITREASON_ABEND;
494 pStatus->iStatus = RTEXITCODE_FAILURE;
495
496 /*
497 * Check input arguments.
498 */
499 AssertReturn(!(fFlags & ~RTPROCEXEC_FLAGS_VALID_MASK), VERR_INVALID_PARAMETER);
500
501 /*
502 * Set up /dev/null redirections.
503 */
504 PRTHANDLE aph[3] = { NULL, NULL, NULL };
505 RTHANDLE ah[3];
506 for (uint32_t i = 0; i < 3; i++)
507 {
508 ah[i].enmType = RTHANDLETYPE_FILE;
509 ah[i].u.hFile = NIL_RTFILE;
510 }
511 rc = VINF_SUCCESS;
512 if ((fFlags & RTPROCEXEC_FLAGS_STDIN_NULL) && RT_SUCCESS(rc))
513 {
514 aph[0] = &ah[0];
515 rc = RTFileOpenBitBucket(&ah[0].u.hFile, RTFILE_O_READ);
516 }
517 if ((fFlags & RTPROCEXEC_FLAGS_STDOUT_NULL) && RT_SUCCESS(rc))
518 {
519 aph[1] = &ah[1];
520 rc = RTFileOpenBitBucket(&ah[1].u.hFile, RTFILE_O_WRITE);
521 }
522 if ((fFlags & RTPROCEXEC_FLAGS_STDERR_NULL) && RT_SUCCESS(rc))
523 {
524 aph[2] = &ah[2];
525 rc = RTFileOpenBitBucket(&ah[2].u.hFile, RTFILE_O_WRITE);
526 }
527
528 /*
529 * Create the process.
530 */
531 RTPROCESS hProc = NIL_RTPROCESS;
532 if (RT_SUCCESS(rc))
533 rc = RTProcCreateEx(pszExec,
534 papszArgs,
535 hEnv,
536 0 /*fFlags*/,
537 aph[0],
538 aph[1],
539 aph[2],
540 NULL /*pszAsUser*/,
541 NULL /*pszPassword*/,
542 &hProc);
543
544 for (uint32_t i = 0; i < 3; i++)
545 RTFileClose(ah[i].u.hFile);
546
547 if (RT_SUCCESS(rc))
548 rc = RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, pStatus);
549 return rc;
550}
551
552
553
554/**
555 * Executes SVN and gets the output.
556 *
557 * Standard error is suppressed.
558 *
559 * @returns VINF_SUCCESS if the command executed successfully.
560 * @param pState The rewrite state to work on. Can be NULL.
561 * @param papszArgs The SVN argument.
562 * @param fNormalFailureOk Whether normal failure is ok.
563 * @param ppszStdOut Where to return the output on success.
564 */
565static int scmSvnRunAndGetOutput(PSCMRWSTATE pState, const char **papszArgs, bool fNormalFailureOk, char **ppszStdOut)
566{
567 *ppszStdOut = NULL;
568
569 char *pszCmdLine = NULL;
570 int rc = RTGetOptArgvToString(&pszCmdLine, papszArgs, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH);
571 if (RT_FAILURE(rc))
572 return rc;
573 ScmVerbose(pState, 2, "executing: %s\n", pszCmdLine);
574
575 RTPROCSTATUS Status;
576 rc = RTProcExecToString(g_szSvnPath, papszArgs, RTENV_DEFAULT,
577 RTPROCEXEC_FLAGS_STD_NULL, &Status, ppszStdOut, NULL);
578
579 if ( RT_SUCCESS(rc)
580 && ( Status.enmReason != RTPROCEXITREASON_NORMAL
581 || Status.iStatus != 0) )
582 {
583 if (fNormalFailureOk || Status.enmReason != RTPROCEXITREASON_NORMAL)
584 RTMsgError("%s: %s -> %s %u\n",
585 pState->pszFilename, pszCmdLine,
586 Status.enmReason == RTPROCEXITREASON_NORMAL ? "exit code"
587 : Status.enmReason == RTPROCEXITREASON_SIGNAL ? "signal"
588 : Status.enmReason == RTPROCEXITREASON_ABEND ? "abnormal end"
589 : "abducted by alien",
590 Status.iStatus);
591 rc = VERR_GENERAL_FAILURE;
592 }
593 else if (RT_FAILURE(rc))
594 {
595 if (pState)
596 RTMsgError("%s: executing: %s => %Rrc\n", pState->pszFilename, pszCmdLine, rc);
597 else
598 RTMsgError("executing: %s => %Rrc\n", pszCmdLine, rc);
599 }
600
601 if (RT_FAILURE(rc))
602 {
603 RTStrFree(*ppszStdOut);
604 *ppszStdOut = NULL;
605 }
606 RTStrFree(pszCmdLine);
607 return rc;
608}
609
610
611/**
612 * Executes SVN.
613 *
614 * Standard error and standard output is suppressed.
615 *
616 * @returns VINF_SUCCESS if the command executed successfully.
617 * @param pState The rewrite state to work on.
618 * @param papszArgs The SVN argument.
619 * @param fNormalFailureOk Whether normal failure is ok.
620 */
621static int scmSvnRun(PSCMRWSTATE pState, const char **papszArgs, bool fNormalFailureOk)
622{
623 char *pszCmdLine = NULL;
624 int rc = RTGetOptArgvToString(&pszCmdLine, papszArgs, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH);
625 if (RT_FAILURE(rc))
626 return rc;
627 ScmVerbose(pState, 2, "executing: %s\n", pszCmdLine);
628
629 /* Lazy bird uses RTProcExecToString. */
630 RTPROCSTATUS Status;
631 rc = RTProcExec(g_szSvnPath, papszArgs, RTENV_DEFAULT, RTPROCEXEC_FLAGS_STD_NULL, &Status);
632
633 if ( RT_SUCCESS(rc)
634 && ( Status.enmReason != RTPROCEXITREASON_NORMAL
635 || Status.iStatus != 0) )
636 {
637 if (fNormalFailureOk || Status.enmReason != RTPROCEXITREASON_NORMAL)
638 RTMsgError("%s: %s -> %s %u\n",
639 pState->pszFilename,
640 pszCmdLine,
641 Status.enmReason == RTPROCEXITREASON_NORMAL ? "exit code"
642 : Status.enmReason == RTPROCEXITREASON_SIGNAL ? "signal"
643 : Status.enmReason == RTPROCEXITREASON_ABEND ? "abnormal end"
644 : "abducted by alien",
645 Status.iStatus);
646 rc = VERR_GENERAL_FAILURE;
647 }
648 else if (RT_FAILURE(rc))
649 RTMsgError("%s: %s -> %Rrc\n", pState->pszFilename, pszCmdLine, rc);
650
651 RTStrFree(pszCmdLine);
652 return rc;
653}
654
655
656#ifdef SCM_WITH_DYNAMIC_LIB_SVN
657/**
658 * Attempts to resolve the necessary subversion and apache portable runtime APIs
659 * we require dynamically.
660 *
661 * Will set all global function pointers and g_fSvnFunctionPointersValid to true
662 * on success.
663 */
664static void scmSvnTryResolveFunctions(void)
665{
666 char szPath[RTPATH_MAX];
667 int rc = RTStrCopy(szPath, sizeof(szPath), g_szSvnPath);
668 if (RT_SUCCESS(rc))
669 {
670 RTPathStripFilename(szPath);
671 char *pszEndPath = strchr(szPath, '\0');
672# ifdef RT_OS_WINDOWS
673 RTPathChangeToDosSlashes(szPath, false);
674# endif
675
676 /*
677 * Try various prefixes/suffxies/locations.
678 */
679 static struct
680 {
681 const char *pszPrefix;
682 const char *pszSuffix;
683 } const s_aVariations[] =
684 {
685# ifdef RT_OS_WINDOWS
686 { "SlikSvn-lib", "-1.dll" }, /* SlikSVN */
687 { "lib", "-1.dll" }, /* Win32Svn,CollabNet,++ */
688# elif defined(RT_OS_DARWIN)
689 { "../lib/lib", "-1.dylib" },
690# else
691 { "../lib/lib", ".so" },
692 { "../lib/lib", "-1.so" },
693# endif
694 };
695 for (unsigned iVar = 0; RT_ELEMENTS(s_aVariations); iVar++)
696 {
697 /*
698 * Try load the svn_client library ...
699 */
700 static const char * const s_apszLibraries[] = { "svn_client", "svn_subr", "apr" };
701 RTLDRMOD ahMods[RT_ELEMENTS(s_apszLibraries)] = { NIL_RTLDRMOD, NIL_RTLDRMOD, NIL_RTLDRMOD };
702
703 rc = VINF_SUCCESS;
704 unsigned iLib;
705 for (iLib = 0; iLib < RT_ELEMENTS(s_apszLibraries) && RT_SUCCESS(rc); iLib++)
706 {
707 *pszEndPath = '\0';
708 rc = RTPathAppend(szPath, sizeof(szPath), s_aVariations[iVar].pszPrefix);
709 if (RT_SUCCESS(rc))
710 rc = RTStrCat(szPath, sizeof(szPath), s_apszLibraries[iLib]);
711 if (RT_SUCCESS(rc))
712 rc = RTStrCat(szPath, sizeof(szPath), s_aVariations[iVar].pszSuffix);
713 if (RT_SUCCESS(rc))
714 {
715# ifdef RT_OS_WINDOWS
716 RTPathChangeToDosSlashes(pszEndPath, false);
717# endif
718 rc = RTLdrLoadEx(szPath, &ahMods[iLib], RTLDRLOAD_FLAGS_NT_SEARCH_DLL_LOAD_DIR , NULL);
719 }
720 }
721 if (iLib == RT_ELEMENTS(s_apszLibraries) && RT_SUCCESS(rc))
722 {
723 static const struct
724 {
725 unsigned iLib;
726 const char *pszSymbol;
727 PFNRT *ppfn;
728 } s_aSymbols[] =
729 {
730 { 2, "apr_initialize", (PFNRT *)&g_pfnAprInitialize },
731 { 2, "apr_hash_first", (PFNRT *)&g_pfnAprHashFirst },
732 { 2, "apr_hash_next", (PFNRT *)&g_pfnAprHashNext },
733 { 2, "apr_hash_this_val", (PFNRT *)&g_pfnAprHashThisVal },
734 { 1, "svn_pool_create_ex", (PFNRT *)&g_pfnSvnPoolCreateEx },
735 { 2, "apr_pool_clear", (PFNRT *)&g_pfnAprPoolClear },
736 { 2, "apr_pool_destroy", (PFNRT *)&g_pfnAprPoolDestroy },
737 { 0, "svn_client_create_context", (PFNRT *)&g_pfnSvnClientCreateContext },
738 { 0, "svn_client_propget4", (PFNRT *)&g_pfnSvnClientPropGet4 },
739 };
740 for (unsigned i = 0; i < RT_ELEMENTS(s_aSymbols); i++)
741 {
742 rc = RTLdrGetSymbol(ahMods[s_aSymbols[i].iLib], s_aSymbols[i].pszSymbol,
743 (void **)(uintptr_t)s_aSymbols[i].ppfn);
744 if (RT_FAILURE(rc))
745 {
746 ScmVerbose(NULL, 0, "Failed to resolve '%s' in '%s'",
747 s_aSymbols[i].pszSymbol, s_apszLibraries[s_aSymbols[i].iLib]);
748 break;
749 }
750 }
751 if (RT_SUCCESS(rc))
752 {
753 apr_status_t rcApr = g_pfnAprInitialize();
754 if (rcApr == 0)
755 {
756 ScmVerbose(NULL, 1, "Found subversion APIs.\n");
757 g_fSvnFunctionPointersValid = true;
758 }
759 else
760 {
761 ScmVerbose(NULL, 0, "apr_initialize failed: %#x (%d)\n", rcApr, rcApr);
762 AssertMsgFailed(("%#x (%d)\n", rc, rc));
763 }
764 return;
765 }
766 }
767
768 while (iLib-- > 0)
769 RTLdrClose(ahMods[iLib]);
770 }
771 }
772}
773#endif /* SCM_WITH_DYNAMIC_LIB_SVN */
774
775
776/**
777 * Finds the svn binary, updating g_szSvnPath and g_enmSvnVersion.
778 */
779static void scmSvnFindSvnBinary(PSCMRWSTATE pState)
780{
781 /* Already been called? */
782 if (g_szSvnPath[0] != '\0')
783 return;
784
785 /*
786 * Locate it.
787 */
788 /** @todo code page fun... */
789#ifdef RT_OS_WINDOWS
790 const char *pszEnvVar = RTEnvGet("Path");
791#else
792 const char *pszEnvVar = RTEnvGet("PATH");
793#endif
794 if (pszEnvVar)
795 {
796#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
797 int rc = RTPathTraverseList(pszEnvVar, ';', scmSvnFindSvnBinaryCallback, g_szSvnPath, (void *)sizeof(g_szSvnPath));
798#else
799 int rc = RTPathTraverseList(pszEnvVar, ':', scmSvnFindSvnBinaryCallback, g_szSvnPath, (void *)sizeof(g_szSvnPath));
800#endif
801 if (RT_FAILURE(rc))
802 strcpy(g_szSvnPath, "svn");
803 }
804 else
805 strcpy(g_szSvnPath, "svn");
806
807 /*
808 * Check the version.
809 */
810 const char *apszArgs[] = { g_szSvnPath, "--version", "--quiet", NULL };
811 char *pszVersion;
812 int rc = scmSvnRunAndGetOutput(pState, apszArgs, false, &pszVersion);
813 if (RT_SUCCESS(rc))
814 {
815 char *pszStripped = RTStrStrip(pszVersion);
816 if (RTStrVersionCompare(pszStripped, "1.8") >= 0)
817 g_enmSvnVersion = kScmSvnVersion_1_8;
818 else if (RTStrVersionCompare(pszStripped, "1.7") >= 0)
819 g_enmSvnVersion = kScmSvnVersion_1_7;
820 else if (RTStrVersionCompare(pszStripped, "1.6") >= 0)
821 g_enmSvnVersion = kScmSvnVersion_1_6;
822 else
823 g_enmSvnVersion = kScmSvnVersion_Ancient;
824 RTStrFree(pszVersion);
825 }
826 else
827 g_enmSvnVersion = kScmSvnVersion_Ancient;
828
829#ifdef SCM_WITH_DYNAMIC_LIB_SVN
830 /*
831 * If we got version 1.8 or later, try see if we can locate a few of the
832 * simpler SVN APIs.
833 */
834 g_fSvnFunctionPointersValid = false;
835 if (g_enmSvnVersion >= kScmSvnVersion_1_8)
836 scmSvnTryResolveFunctions();
837#endif
838}
839
840
841/**
842 * Construct a dot svn filename for the file being rewritten.
843 *
844 * @returns IPRT status code.
845 * @param pState The rewrite state (for the name).
846 * @param pszDir The directory, including ".svn/".
847 * @param pszSuff The filename suffix.
848 * @param pszDst The output buffer. RTPATH_MAX in size.
849 */
850static int scmSvnConstructName(PSCMRWSTATE pState, const char *pszDir, const char *pszSuff, char *pszDst)
851{
852 strcpy(pszDst, pState->pszFilename); /* ASSUMES sizeof(szBuf) <= sizeof(szPath) */
853 RTPathStripFilename(pszDst);
854
855 int rc = RTPathAppend(pszDst, RTPATH_MAX, pszDir);
856 if (RT_SUCCESS(rc))
857 {
858 rc = RTPathAppend(pszDst, RTPATH_MAX, RTPathFilename(pState->pszFilename));
859 if (RT_SUCCESS(rc))
860 {
861 size_t cchDst = strlen(pszDst);
862 size_t cchSuff = strlen(pszSuff);
863 if (cchDst + cchSuff < RTPATH_MAX)
864 {
865 memcpy(&pszDst[cchDst], pszSuff, cchSuff + 1);
866 return VINF_SUCCESS;
867 }
868 else
869 rc = VERR_BUFFER_OVERFLOW;
870 }
871 }
872 return rc;
873}
874
875/**
876 * Interprets the specified string as decimal numbers.
877 *
878 * @returns true if parsed successfully, false if not.
879 * @param pch The string (not terminated).
880 * @param cch The string length.
881 * @param pu Where to return the value.
882 */
883static bool scmSvnReadNumber(const char *pch, size_t cch, size_t *pu)
884{
885 size_t u = 0;
886 while (cch-- > 0)
887 {
888 char ch = *pch++;
889 if (ch < '0' || ch > '9')
890 return false;
891 u *= 10;
892 u += ch - '0';
893 }
894 *pu = u;
895 return true;
896}
897
898
899#ifdef SCM_WITH_DYNAMIC_LIB_SVN
900
901/**
902 * Wrapper around RTPathAbs.
903 * @returns Same as RTPathAbs.
904 * @param pszPath The relative path.
905 * @param pszAbsPath Where to return the absolute path.
906 * @param cbAbsPath Size of the @a pszAbsPath buffer.
907 */
908static int scmSvnAbsPath(const char *pszPath, char *pszAbsPath, size_t cbAbsPath)
909{
910 int rc = RTPathAbs(pszPath, pszAbsPath, cbAbsPath);
911# if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
912 if (RT_SUCCESS(rc))
913 RTPathChangeToUnixSlashes(pszAbsPath, true /*fForce*/);
914# endif
915 return rc;
916}
917
918
919/**
920 * Checks if @a pszPath exists in the current WC.
921 *
922 * @returns true, false or -1. In the latter case, please use the fallback.
923 * @param pszPath Path to the object that should be investigated.
924 */
925static int scmSvnIsObjectInWorkingCopy(const char *pszPath)
926{
927 int rc = -1;
928
929 /* svn_client_propget4 and later requires absolute target path. */
930 char szAbsPath[RTPATH_MAX];
931 int rc2 = scmSvnAbsPath(pszPath, szAbsPath, sizeof(szAbsPath));
932 if (RT_SUCCESS(rc2))
933 {
934 /* Create calling context. */
935 apr_pool_t *pPool = g_pfnSvnPoolCreateEx(NULL, NULL);
936 if (pPool)
937 {
938 svn_client_ctx_t *pCtx = NULL;
939 svn_error_t *pErr = g_pfnSvnClientCreateContext(&pCtx, pPool);
940 if (!pErr)
941 {
942 /* Make the call. */
943 apr_hash_t *pHash = NULL;
944 svn_opt_revision_t Rev;
945 RT_ZERO(Rev);
946 Rev.kind = svn_opt_revision_base;
947 Rev.value.number = -1L;
948 pErr = g_pfnSvnClientPropGet4(&pHash, "svn:no-such-property", szAbsPath, &Rev, &Rev,
949 NULL /*pActualRev*/, svn_depth_empty, NULL /*pChangeList*/, pCtx, pPool, pPool);
950 if (!pErr)
951 rc = true;
952 else if (pErr->apr_err == SVN_ERR_UNVERSIONED_RESOURCE)
953 rc = false;
954 }
955 g_pfnAprPoolDestroy(pPool);
956 }
957 }
958 return rc;
959}
960
961#endif /* SCM_WITH_DYNAMIC_LIB_SVN */
962
963
964/**
965 * Checks if the file we're operating on is part of a SVN working copy.
966 *
967 * @returns true if it is, false if it isn't or we cannot tell.
968 * @param pState The rewrite state to work on.
969 */
970bool ScmSvnIsInWorkingCopy(PSCMRWSTATE pState)
971{
972 scmSvnFindSvnBinary(pState);
973
974#ifdef SCM_WITH_DYNAMIC_LIB_SVN
975 if (g_fSvnFunctionPointersValid)
976 {
977 int rc = scmSvnIsObjectInWorkingCopy(pState->pszFilename);
978 if (rc == (int)true || rc == (int)false)
979 return rc == (int)true;
980 }
981
982 /* Fallback: */
983#endif
984 if (g_enmSvnVersion < kScmSvnVersion_1_7)
985 {
986 /*
987 * Hack: check if the .svn/text-base/<file>.svn-base file exists.
988 */
989 char szPath[RTPATH_MAX];
990 int rc = scmSvnConstructName(pState, ".svn/text-base/", ".svn-base", szPath);
991 if (RT_SUCCESS(rc))
992 return RTFileExists(szPath);
993 }
994 else
995 {
996 const char *apszArgs[] = { g_szSvnPath, "propget", "svn:no-such-property", pState->pszFilename, NULL };
997 char *pszValue;
998 int rc = scmSvnRunAndGetOutput(pState, apszArgs, true, &pszValue);
999 if (RT_SUCCESS(rc))
1000 {
1001 RTStrFree(pszValue);
1002 return true;
1003 }
1004 }
1005 return false;
1006}
1007
1008
1009/**
1010 * Checks if the specified directory is part of a SVN working copy.
1011 *
1012 * @returns true if it is, false if it isn't or we cannot tell.
1013 * @param pszDir The directory in question.
1014 */
1015bool ScmSvnIsDirInWorkingCopy(const char *pszDir)
1016{
1017 scmSvnFindSvnBinary(NULL);
1018
1019#ifdef SCM_WITH_DYNAMIC_LIB_SVN
1020 if (g_fSvnFunctionPointersValid)
1021 {
1022 int rc = scmSvnIsObjectInWorkingCopy(pszDir);
1023 if (rc == (int)true || rc == (int)false)
1024 return rc == (int)true;
1025 }
1026
1027 /* Fallback: */
1028#endif
1029 if (g_enmSvnVersion < kScmSvnVersion_1_7)
1030 {
1031 /*
1032 * Hack: check if the .svn/ dir exists.
1033 */
1034 char szPath[RTPATH_MAX];
1035 int rc = RTPathJoin(szPath, sizeof(szPath), pszDir, ".svn");
1036 if (RT_SUCCESS(rc))
1037 return RTDirExists(szPath);
1038 }
1039 else
1040 {
1041 const char *apszArgs[] = { g_szSvnPath, "propget", "svn:no-such-property", pszDir, NULL };
1042 char *pszValue;
1043 int rc = scmSvnRunAndGetOutput(NULL, apszArgs, true, &pszValue);
1044 if (RT_SUCCESS(rc))
1045 {
1046 RTStrFree(pszValue);
1047 return true;
1048 }
1049 }
1050 return false;
1051}
1052
1053
1054#ifdef SCM_WITH_DYNAMIC_LIB_SVN
1055/**
1056 * Checks if @a pszPath exists in the current WC.
1057 *
1058 * @returns IPRT status code - VERR_NOT_SUPPORT if fallback should be attempted.
1059 * @param pszPath Path to the object that should be investigated.
1060 * @param pszProperty The property name.
1061 * @param ppszValue Where to return the property value. Optional.
1062 */
1063static int scmSvnQueryPropertyUsingApi(const char *pszPath, const char *pszProperty, char **ppszValue)
1064{
1065 int rc = VERR_NOT_SUPPORTED;
1066
1067 /* svn_client_propget4 and later requires absolute target path. */
1068 char szAbsPath[RTPATH_MAX];
1069 int rc2 = scmSvnAbsPath(pszPath, szAbsPath, sizeof(szAbsPath));
1070 if (RT_SUCCESS(rc2))
1071 {
1072 /* Create calling context. */
1073 apr_pool_t *pPool = g_pfnSvnPoolCreateEx(NULL, NULL);
1074 if (pPool)
1075 {
1076 svn_client_ctx_t *pCtx = NULL;
1077 svn_error_t *pErr = g_pfnSvnClientCreateContext(&pCtx, pPool);
1078 if (!pErr)
1079 {
1080 /* Make the call. */
1081 apr_hash_t *pHash = NULL;
1082 svn_opt_revision_t Rev;
1083 RT_ZERO(Rev);
1084 Rev.kind = svn_opt_revision_base;
1085 Rev.value.number = -1L;
1086 pErr = g_pfnSvnClientPropGet4(&pHash, pszProperty, szAbsPath, &Rev, &Rev,
1087 NULL /*pActualRev*/, svn_depth_empty, NULL /*pChangeList*/, pCtx, pPool, pPool);
1088 if (!pErr)
1089 {
1090 /* Get the first value, if any. */
1091 rc = VERR_NOT_FOUND;
1092 apr_hash_index_t *pHashIdx = g_pfnAprHashFirst(pPool, pHash);
1093 if (pHashIdx)
1094 {
1095 const char **ppszFirst = (const char **)g_pfnAprHashThisVal(pHashIdx);
1096 if (ppszFirst && *ppszFirst)
1097 {
1098 if (ppszValue)
1099 rc = RTStrDupEx(ppszValue, *ppszFirst);
1100 else
1101 rc = VINF_SUCCESS;
1102 }
1103 }
1104 }
1105 else if (pErr->apr_err == SVN_ERR_UNVERSIONED_RESOURCE)
1106 rc = VERR_INVALID_STATE;
1107 else
1108 rc = VERR_GENERAL_FAILURE;
1109 }
1110 g_pfnAprPoolDestroy(pPool);
1111 }
1112 }
1113 return rc;
1114}
1115#endif /* SCM_WITH_DYNAMIC_LIB_SVN */
1116
1117
1118/**
1119 * Queries the value of an SVN property.
1120 *
1121 * This will automatically adjust for scheduled changes.
1122 *
1123 * @returns IPRT status code.
1124 * @retval VERR_INVALID_STATE if not a SVN WC file.
1125 * @retval VERR_NOT_FOUND if the property wasn't found.
1126 * @param pState The rewrite state to work on.
1127 * @param pszName The property name.
1128 * @param ppszValue Where to return the property value. Free this
1129 * using RTStrFree. Optional.
1130 */
1131int ScmSvnQueryProperty(PSCMRWSTATE pState, const char *pszName, char **ppszValue)
1132{
1133 int rc;
1134
1135 /*
1136 * Look it up in the scheduled changes.
1137 */
1138 size_t i = pState->cSvnPropChanges;
1139 while (i-- > 0)
1140 if (!strcmp(pState->paSvnPropChanges[i].pszName, pszName))
1141 {
1142 const char *pszValue = pState->paSvnPropChanges[i].pszValue;
1143 if (!pszValue)
1144 return VERR_NOT_FOUND;
1145 if (ppszValue)
1146 return RTStrDupEx(ppszValue, pszValue);
1147 return VINF_SUCCESS;
1148 }
1149
1150 scmSvnFindSvnBinary(pState);
1151
1152#ifdef SCM_WITH_DYNAMIC_LIB_SVN
1153 if (g_fSvnFunctionPointersValid)
1154 {
1155 rc = scmSvnQueryPropertyUsingApi(pState->pszFilename, pszName, ppszValue);
1156 if (rc != VERR_NOT_SUPPORTED)
1157 return rc;
1158 /* Fallback: */
1159 }
1160#endif
1161
1162 if (g_enmSvnVersion < kScmSvnVersion_1_7)
1163 {
1164 /*
1165 * Hack: Read the .svn/props/<file>.svn-work file exists.
1166 */
1167 char szPath[RTPATH_MAX];
1168 rc = scmSvnConstructName(pState, ".svn/props/", ".svn-work", szPath);
1169 if (RT_SUCCESS(rc) && !RTFileExists(szPath))
1170 rc = scmSvnConstructName(pState, ".svn/prop-base/", ".svn-base", szPath);
1171 if (RT_SUCCESS(rc))
1172 {
1173 SCMSTREAM Stream;
1174 rc = ScmStreamInitForReading(&Stream, szPath);
1175 if (RT_SUCCESS(rc))
1176 {
1177 /*
1178 * The current format is K len\n<name>\nV len\n<value>\n" ... END.
1179 */
1180 rc = VERR_NOT_FOUND;
1181 size_t const cchName = strlen(pszName);
1182 SCMEOL enmEol;
1183 size_t cchLine;
1184 const char *pchLine;
1185 while ((pchLine = ScmStreamGetLine(&Stream, &cchLine, &enmEol)) != NULL)
1186 {
1187 /*
1188 * Parse the 'K num' / 'END' line.
1189 */
1190 if ( cchLine == 3
1191 && !memcmp(pchLine, "END", 3))
1192 break;
1193 size_t cchKey;
1194 if ( cchLine < 3
1195 || pchLine[0] != 'K'
1196 || pchLine[1] != ' '
1197 || !scmSvnReadNumber(&pchLine[2], cchLine - 2, &cchKey)
1198 || cchKey == 0
1199 || cchKey > 4096)
1200 {
1201 RTMsgError("%s:%u: Unexpected data '%.*s'\n", szPath, ScmStreamTellLine(&Stream), cchLine, pchLine);
1202 rc = VERR_PARSE_ERROR;
1203 break;
1204 }
1205
1206 /*
1207 * Match the key and skip to the value line. Don't bother with
1208 * names containing EOL markers.
1209 */
1210 size_t const offKey = ScmStreamTell(&Stream);
1211 bool fMatch = cchName == cchKey;
1212 if (fMatch)
1213 {
1214 pchLine = ScmStreamGetLine(&Stream, &cchLine, &enmEol);
1215 if (!pchLine)
1216 break;
1217 fMatch = cchLine == cchName
1218 && !memcmp(pchLine, pszName, cchName);
1219 }
1220
1221 if (RT_FAILURE(ScmStreamSeekAbsolute(&Stream, offKey + cchKey)))
1222 break;
1223 if (RT_FAILURE(ScmStreamSeekByLine(&Stream, ScmStreamTellLine(&Stream) + 1)))
1224 break;
1225
1226 /*
1227 * Read and Parse the 'V num' line.
1228 */
1229 pchLine = ScmStreamGetLine(&Stream, &cchLine, &enmEol);
1230 if (!pchLine)
1231 break;
1232 size_t cchValue;
1233 if ( cchLine < 3
1234 || pchLine[0] != 'V'
1235 || pchLine[1] != ' '
1236 || !scmSvnReadNumber(&pchLine[2], cchLine - 2, &cchValue)
1237 || cchValue > _1M)
1238 {
1239 RTMsgError("%s:%u: Unexpected data '%.*s'\n", szPath, ScmStreamTellLine(&Stream), cchLine, pchLine);
1240 rc = VERR_PARSE_ERROR;
1241 break;
1242 }
1243
1244 /*
1245 * If we have a match, allocate a return buffer and read the
1246 * value into it. Otherwise skip this value and continue
1247 * searching.
1248 */
1249 if (fMatch)
1250 {
1251 if (!ppszValue)
1252 rc = VINF_SUCCESS;
1253 else
1254 {
1255 char *pszValue;
1256 rc = RTStrAllocEx(&pszValue, cchValue + 1);
1257 if (RT_SUCCESS(rc))
1258 {
1259 rc = ScmStreamRead(&Stream, pszValue, cchValue);
1260 if (RT_SUCCESS(rc))
1261 *ppszValue = pszValue;
1262 else
1263 RTStrFree(pszValue);
1264 }
1265 }
1266 break;
1267 }
1268
1269 if (RT_FAILURE(ScmStreamSeekRelative(&Stream, cchValue)))
1270 break;
1271 if (RT_FAILURE(ScmStreamSeekByLine(&Stream, ScmStreamTellLine(&Stream) + 1)))
1272 break;
1273 }
1274
1275 if (RT_FAILURE(ScmStreamGetStatus(&Stream)))
1276 {
1277 rc = ScmStreamGetStatus(&Stream);
1278 RTMsgError("%s: stream error %Rrc\n", szPath, rc);
1279 }
1280 ScmStreamDelete(&Stream);
1281 }
1282 }
1283
1284 if (rc == VERR_FILE_NOT_FOUND)
1285 rc = VERR_NOT_FOUND;
1286 }
1287 else
1288 {
1289 const char *apszArgs[] = { g_szSvnPath, "propget", "--strict", pszName, pState->pszFilename, NULL };
1290 char *pszValue;
1291 rc = scmSvnRunAndGetOutput(pState, apszArgs, false, &pszValue);
1292 if (RT_SUCCESS(rc))
1293 {
1294 if (pszValue && *pszValue)
1295 {
1296 if (ppszValue)
1297 {
1298 *ppszValue = pszValue;
1299 pszValue = NULL;
1300 }
1301 }
1302 else
1303 rc = VERR_NOT_FOUND;
1304 RTStrFree(pszValue);
1305 }
1306 }
1307 return rc;
1308}
1309
1310
1311/**
1312 * Schedules the setting of a property.
1313 *
1314 * @returns IPRT status code.
1315 * @retval VERR_INVALID_STATE if not a SVN WC file.
1316 * @param pState The rewrite state to work on.
1317 * @param pszName The name of the property to set.
1318 * @param pszValue The value. NULL means deleting it.
1319 */
1320int ScmSvnSetProperty(PSCMRWSTATE pState, const char *pszName, const char *pszValue)
1321{
1322 /*
1323 * Update any existing entry first.
1324 */
1325 size_t i = pState->cSvnPropChanges;
1326 while (i-- > 0)
1327 if (!strcmp(pState->paSvnPropChanges[i].pszName, pszName))
1328 {
1329 if (!pszValue)
1330 {
1331 RTStrFree(pState->paSvnPropChanges[i].pszValue);
1332 pState->paSvnPropChanges[i].pszValue = NULL;
1333 }
1334 else
1335 {
1336 char *pszCopy;
1337 int rc = RTStrDupEx(&pszCopy, pszValue);
1338 if (RT_FAILURE(rc))
1339 return rc;
1340 pState->paSvnPropChanges[i].pszValue = pszCopy;
1341 }
1342 return VINF_SUCCESS;
1343 }
1344
1345 /*
1346 * Insert a new entry.
1347 */
1348 i = pState->cSvnPropChanges;
1349 if ((i % 32) == 0)
1350 {
1351 void *pvNew = RTMemRealloc(pState->paSvnPropChanges, (i + 32) * sizeof(SCMSVNPROP));
1352 if (!pvNew)
1353 return VERR_NO_MEMORY;
1354 pState->paSvnPropChanges = (PSCMSVNPROP)pvNew;
1355 }
1356
1357 pState->paSvnPropChanges[i].pszName = RTStrDup(pszName);
1358 pState->paSvnPropChanges[i].pszValue = pszValue ? RTStrDup(pszValue) : NULL;
1359 if ( pState->paSvnPropChanges[i].pszName
1360 && (pState->paSvnPropChanges[i].pszValue || !pszValue) )
1361 pState->cSvnPropChanges = i + 1;
1362 else
1363 {
1364 RTStrFree(pState->paSvnPropChanges[i].pszName);
1365 pState->paSvnPropChanges[i].pszName = NULL;
1366 RTStrFree(pState->paSvnPropChanges[i].pszValue);
1367 pState->paSvnPropChanges[i].pszValue = NULL;
1368 return VERR_NO_MEMORY;
1369 }
1370 return VINF_SUCCESS;
1371}
1372
1373
1374/**
1375 * Schedules a property deletion.
1376 *
1377 * @returns IPRT status code.
1378 * @param pState The rewrite state to work on.
1379 * @param pszName The name of the property to delete.
1380 */
1381int ScmSvnDelProperty(PSCMRWSTATE pState, const char *pszName)
1382{
1383 return ScmSvnSetProperty(pState, pszName, NULL);
1384}
1385
1386
1387/**
1388 * Applies any SVN property changes to the work copy of the file.
1389 *
1390 * @returns IPRT status code.
1391 * @param pState The rewrite state which SVN property changes
1392 * should be applied.
1393 */
1394int ScmSvnDisplayChanges(PSCMRWSTATE pState)
1395{
1396 size_t i = pState->cSvnPropChanges;
1397 while (i-- > 0)
1398 {
1399 const char *pszName = pState->paSvnPropChanges[i].pszName;
1400 const char *pszValue = pState->paSvnPropChanges[i].pszValue;
1401 if (pszValue)
1402 ScmVerbose(pState, 0, "svn propset '%s' '%s' %s\n", pszName, pszValue, pState->pszFilename);
1403 else
1404 ScmVerbose(pState, 0, "svn propdel '%s' %s\n", pszName, pState->pszFilename);
1405 }
1406
1407 return VINF_SUCCESS;
1408}
1409
1410/**
1411 * Applies any SVN property changes to the work copy of the file.
1412 *
1413 * @returns IPRT status code.
1414 * @param pState The rewrite state which SVN property changes
1415 * should be applied.
1416 */
1417int ScmSvnApplyChanges(PSCMRWSTATE pState)
1418{
1419 scmSvnFindSvnBinary(pState);
1420
1421#ifdef SCM_WITH_LATER
1422 if (0)
1423 {
1424 return ...;
1425 }
1426
1427 /* Fallback: */
1428#endif
1429
1430 /*
1431 * Iterate thru the changes and apply them by starting the svn client.
1432 */
1433 for (size_t i = 0; i < pState->cSvnPropChanges; i++)
1434 {
1435 const char *apszArgv[6];
1436 apszArgv[0] = g_szSvnPath;
1437 apszArgv[1] = pState->paSvnPropChanges[i].pszValue ? "propset" : "propdel";
1438 apszArgv[2] = pState->paSvnPropChanges[i].pszName;
1439 int iArg = 3;
1440 if (pState->paSvnPropChanges[i].pszValue)
1441 apszArgv[iArg++] = pState->paSvnPropChanges[i].pszValue;
1442 apszArgv[iArg++] = pState->pszFilename;
1443 apszArgv[iArg++] = NULL;
1444
1445 int rc = scmSvnRun(pState, apszArgv, false);
1446 if (RT_FAILURE(rc))
1447 return rc;
1448 }
1449
1450 return VINF_SUCCESS;
1451}
1452
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