VirtualBox

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

Last change on this file since 66250 was 65185, checked in by vboxsync, 8 years ago

scm: fixed a loop, added support for Linux distributions which have the path in /usr/lib/*-linux-gnu and also look for .so.0 and .so.1 libraries (if the -dev/-devel package is not installed)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 51.0 KB
Line 
1/* $Id: scmsubversion.cpp 65185 2017-01-06 15:32:57Z 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# ifdef RT_ARCH_X86
694 { "../lib/i386-linux-gnu/lib", ".so" },
695 { "../lib/i386-linux-gnu/lib", "-1.so" },
696# else
697 { "../lib/x86_64-linux-gnu/lib", ".so" },
698 { "../lib/x86_64-linux-gnu/lib", "-1.so" },
699# endif
700# endif
701 };
702 for (unsigned iVar = 0; iVar < RT_ELEMENTS(s_aVariations); iVar++)
703 {
704 /*
705 * Try load the svn_client library ...
706 */
707 static const char * const s_apszLibraries[] = { "svn_client", "svn_subr", "apr" };
708 RTLDRMOD ahMods[RT_ELEMENTS(s_apszLibraries)] = { NIL_RTLDRMOD, NIL_RTLDRMOD, NIL_RTLDRMOD };
709
710 rc = VINF_SUCCESS;
711 unsigned iLib;
712 for (iLib = 0; iLib < RT_ELEMENTS(s_apszLibraries) && RT_SUCCESS(rc); iLib++)
713 {
714 static const char * const s_apszSuffixes[] = { "", ".0", ".1" };
715 for (unsigned iSuff = 0; iSuff < RT_ELEMENTS(s_apszSuffixes); iSuff++)
716 {
717 *pszEndPath = '\0';
718 rc = RTPathAppend(szPath, sizeof(szPath), s_aVariations[iVar].pszPrefix);
719 if (RT_SUCCESS(rc))
720 rc = RTStrCat(szPath, sizeof(szPath), s_apszLibraries[iLib]);
721 if (RT_SUCCESS(rc))
722 rc = RTStrCat(szPath, sizeof(szPath), s_aVariations[iVar].pszSuffix);
723 if (RT_SUCCESS(rc))
724 rc = RTStrCat(szPath, sizeof(szPath), s_apszSuffixes[iSuff]);
725 if (RT_SUCCESS(rc))
726 {
727# ifdef RT_OS_WINDOWS
728 RTPathChangeToDosSlashes(pszEndPath, false);
729# endif
730 rc = RTLdrLoadEx(szPath, &ahMods[iLib], RTLDRLOAD_FLAGS_NT_SEARCH_DLL_LOAD_DIR , NULL);
731 if (RT_SUCCESS(rc))
732 break;
733 }
734 }
735 }
736 if (iLib == RT_ELEMENTS(s_apszLibraries) && RT_SUCCESS(rc))
737 {
738 static const struct
739 {
740 unsigned iLib;
741 const char *pszSymbol;
742 PFNRT *ppfn;
743 } s_aSymbols[] =
744 {
745 { 2, "apr_initialize", (PFNRT *)&g_pfnAprInitialize },
746 { 2, "apr_hash_first", (PFNRT *)&g_pfnAprHashFirst },
747 { 2, "apr_hash_next", (PFNRT *)&g_pfnAprHashNext },
748 { 2, "apr_hash_this_val", (PFNRT *)&g_pfnAprHashThisVal },
749 { 1, "svn_pool_create_ex", (PFNRT *)&g_pfnSvnPoolCreateEx },
750 { 2, "apr_pool_clear", (PFNRT *)&g_pfnAprPoolClear },
751 { 2, "apr_pool_destroy", (PFNRT *)&g_pfnAprPoolDestroy },
752 { 0, "svn_client_create_context", (PFNRT *)&g_pfnSvnClientCreateContext },
753 { 0, "svn_client_propget4", (PFNRT *)&g_pfnSvnClientPropGet4 },
754 };
755 for (unsigned i = 0; i < RT_ELEMENTS(s_aSymbols); i++)
756 {
757 rc = RTLdrGetSymbol(ahMods[s_aSymbols[i].iLib], s_aSymbols[i].pszSymbol,
758 (void **)(uintptr_t)s_aSymbols[i].ppfn);
759 if (RT_FAILURE(rc))
760 {
761 ScmVerbose(NULL, 0, "Failed to resolve '%s' in '%s'",
762 s_aSymbols[i].pszSymbol, s_apszLibraries[s_aSymbols[i].iLib]);
763 break;
764 }
765 }
766 if (RT_SUCCESS(rc))
767 {
768 apr_status_t rcApr = g_pfnAprInitialize();
769 if (rcApr == 0)
770 {
771 ScmVerbose(NULL, 1, "Found subversion APIs.\n");
772 g_fSvnFunctionPointersValid = true;
773 }
774 else
775 {
776 ScmVerbose(NULL, 0, "apr_initialize failed: %#x (%d)\n", rcApr, rcApr);
777 AssertMsgFailed(("%#x (%d)\n", rc, rc));
778 }
779 return;
780 }
781 }
782
783 while (iLib-- > 0)
784 RTLdrClose(ahMods[iLib]);
785 }
786 }
787}
788#endif /* SCM_WITH_DYNAMIC_LIB_SVN */
789
790
791/**
792 * Finds the svn binary, updating g_szSvnPath and g_enmSvnVersion.
793 */
794static void scmSvnFindSvnBinary(PSCMRWSTATE pState)
795{
796 /* Already been called? */
797 if (g_szSvnPath[0] != '\0')
798 return;
799
800 /*
801 * Locate it.
802 */
803 /** @todo code page fun... */
804#ifdef RT_OS_WINDOWS
805 const char *pszEnvVar = RTEnvGet("Path");
806#else
807 const char *pszEnvVar = RTEnvGet("PATH");
808#endif
809 if (pszEnvVar)
810 {
811#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
812 int rc = RTPathTraverseList(pszEnvVar, ';', scmSvnFindSvnBinaryCallback, g_szSvnPath, (void *)sizeof(g_szSvnPath));
813#else
814 int rc = RTPathTraverseList(pszEnvVar, ':', scmSvnFindSvnBinaryCallback, g_szSvnPath, (void *)sizeof(g_szSvnPath));
815#endif
816 if (RT_FAILURE(rc))
817 strcpy(g_szSvnPath, "svn");
818 }
819 else
820 strcpy(g_szSvnPath, "svn");
821
822 /*
823 * Check the version.
824 */
825 const char *apszArgs[] = { g_szSvnPath, "--version", "--quiet", NULL };
826 char *pszVersion;
827 int rc = scmSvnRunAndGetOutput(pState, apszArgs, false, &pszVersion);
828 if (RT_SUCCESS(rc))
829 {
830 char *pszStripped = RTStrStrip(pszVersion);
831 if (RTStrVersionCompare(pszStripped, "1.8") >= 0)
832 g_enmSvnVersion = kScmSvnVersion_1_8;
833 else if (RTStrVersionCompare(pszStripped, "1.7") >= 0)
834 g_enmSvnVersion = kScmSvnVersion_1_7;
835 else if (RTStrVersionCompare(pszStripped, "1.6") >= 0)
836 g_enmSvnVersion = kScmSvnVersion_1_6;
837 else
838 g_enmSvnVersion = kScmSvnVersion_Ancient;
839 RTStrFree(pszVersion);
840 }
841 else
842 g_enmSvnVersion = kScmSvnVersion_Ancient;
843
844#ifdef SCM_WITH_DYNAMIC_LIB_SVN
845 /*
846 * If we got version 1.8 or later, try see if we can locate a few of the
847 * simpler SVN APIs.
848 */
849 g_fSvnFunctionPointersValid = false;
850 if (g_enmSvnVersion >= kScmSvnVersion_1_8)
851 scmSvnTryResolveFunctions();
852#endif
853}
854
855
856/**
857 * Construct a dot svn filename for the file being rewritten.
858 *
859 * @returns IPRT status code.
860 * @param pState The rewrite state (for the name).
861 * @param pszDir The directory, including ".svn/".
862 * @param pszSuff The filename suffix.
863 * @param pszDst The output buffer. RTPATH_MAX in size.
864 */
865static int scmSvnConstructName(PSCMRWSTATE pState, const char *pszDir, const char *pszSuff, char *pszDst)
866{
867 strcpy(pszDst, pState->pszFilename); /* ASSUMES sizeof(szBuf) <= sizeof(szPath) */
868 RTPathStripFilename(pszDst);
869
870 int rc = RTPathAppend(pszDst, RTPATH_MAX, pszDir);
871 if (RT_SUCCESS(rc))
872 {
873 rc = RTPathAppend(pszDst, RTPATH_MAX, RTPathFilename(pState->pszFilename));
874 if (RT_SUCCESS(rc))
875 {
876 size_t cchDst = strlen(pszDst);
877 size_t cchSuff = strlen(pszSuff);
878 if (cchDst + cchSuff < RTPATH_MAX)
879 {
880 memcpy(&pszDst[cchDst], pszSuff, cchSuff + 1);
881 return VINF_SUCCESS;
882 }
883 else
884 rc = VERR_BUFFER_OVERFLOW;
885 }
886 }
887 return rc;
888}
889
890/**
891 * Interprets the specified string as decimal numbers.
892 *
893 * @returns true if parsed successfully, false if not.
894 * @param pch The string (not terminated).
895 * @param cch The string length.
896 * @param pu Where to return the value.
897 */
898static bool scmSvnReadNumber(const char *pch, size_t cch, size_t *pu)
899{
900 size_t u = 0;
901 while (cch-- > 0)
902 {
903 char ch = *pch++;
904 if (ch < '0' || ch > '9')
905 return false;
906 u *= 10;
907 u += ch - '0';
908 }
909 *pu = u;
910 return true;
911}
912
913
914#ifdef SCM_WITH_DYNAMIC_LIB_SVN
915
916/**
917 * Wrapper around RTPathAbs.
918 * @returns Same as RTPathAbs.
919 * @param pszPath The relative path.
920 * @param pszAbsPath Where to return the absolute path.
921 * @param cbAbsPath Size of the @a pszAbsPath buffer.
922 */
923static int scmSvnAbsPath(const char *pszPath, char *pszAbsPath, size_t cbAbsPath)
924{
925 int rc = RTPathAbs(pszPath, pszAbsPath, cbAbsPath);
926# if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
927 if (RT_SUCCESS(rc))
928 RTPathChangeToUnixSlashes(pszAbsPath, true /*fForce*/);
929# endif
930 return rc;
931}
932
933
934/**
935 * Checks if @a pszPath exists in the current WC.
936 *
937 * @returns true, false or -1. In the latter case, please use the fallback.
938 * @param pszPath Path to the object that should be investigated.
939 */
940static int scmSvnIsObjectInWorkingCopy(const char *pszPath)
941{
942 int rc = -1;
943
944 /* svn_client_propget4 and later requires absolute target path. */
945 char szAbsPath[RTPATH_MAX];
946 int rc2 = scmSvnAbsPath(pszPath, szAbsPath, sizeof(szAbsPath));
947 if (RT_SUCCESS(rc2))
948 {
949 /* Create calling context. */
950 apr_pool_t *pPool = g_pfnSvnPoolCreateEx(NULL, NULL);
951 if (pPool)
952 {
953 svn_client_ctx_t *pCtx = NULL;
954 svn_error_t *pErr = g_pfnSvnClientCreateContext(&pCtx, pPool);
955 if (!pErr)
956 {
957 /* Make the call. */
958 apr_hash_t *pHash = NULL;
959 svn_opt_revision_t Rev;
960 RT_ZERO(Rev);
961 Rev.kind = svn_opt_revision_base;
962 Rev.value.number = -1L;
963 pErr = g_pfnSvnClientPropGet4(&pHash, "svn:no-such-property", szAbsPath, &Rev, &Rev,
964 NULL /*pActualRev*/, svn_depth_empty, NULL /*pChangeList*/, pCtx, pPool, pPool);
965 if (!pErr)
966 rc = true;
967 else if (pErr->apr_err == SVN_ERR_UNVERSIONED_RESOURCE)
968 rc = false;
969 }
970 g_pfnAprPoolDestroy(pPool);
971 }
972 }
973 return rc;
974}
975
976#endif /* SCM_WITH_DYNAMIC_LIB_SVN */
977
978
979/**
980 * Checks if the file we're operating on is part of a SVN working copy.
981 *
982 * @returns true if it is, false if it isn't or we cannot tell.
983 * @param pState The rewrite state to work on.
984 */
985bool ScmSvnIsInWorkingCopy(PSCMRWSTATE pState)
986{
987 scmSvnFindSvnBinary(pState);
988
989#ifdef SCM_WITH_DYNAMIC_LIB_SVN
990 if (g_fSvnFunctionPointersValid)
991 {
992 int rc = scmSvnIsObjectInWorkingCopy(pState->pszFilename);
993 if (rc == (int)true || rc == (int)false)
994 return rc == (int)true;
995 }
996
997 /* Fallback: */
998#endif
999 if (g_enmSvnVersion < kScmSvnVersion_1_7)
1000 {
1001 /*
1002 * Hack: check if the .svn/text-base/<file>.svn-base file exists.
1003 */
1004 char szPath[RTPATH_MAX];
1005 int rc = scmSvnConstructName(pState, ".svn/text-base/", ".svn-base", szPath);
1006 if (RT_SUCCESS(rc))
1007 return RTFileExists(szPath);
1008 }
1009 else
1010 {
1011 const char *apszArgs[] = { g_szSvnPath, "propget", "svn:no-such-property", pState->pszFilename, NULL };
1012 char *pszValue;
1013 int rc = scmSvnRunAndGetOutput(pState, apszArgs, true, &pszValue);
1014 if (RT_SUCCESS(rc))
1015 {
1016 RTStrFree(pszValue);
1017 return true;
1018 }
1019 }
1020 return false;
1021}
1022
1023
1024/**
1025 * Checks if the specified directory is part of a SVN working copy.
1026 *
1027 * @returns true if it is, false if it isn't or we cannot tell.
1028 * @param pszDir The directory in question.
1029 */
1030bool ScmSvnIsDirInWorkingCopy(const char *pszDir)
1031{
1032 scmSvnFindSvnBinary(NULL);
1033
1034#ifdef SCM_WITH_DYNAMIC_LIB_SVN
1035 if (g_fSvnFunctionPointersValid)
1036 {
1037 int rc = scmSvnIsObjectInWorkingCopy(pszDir);
1038 if (rc == (int)true || rc == (int)false)
1039 return rc == (int)true;
1040 }
1041
1042 /* Fallback: */
1043#endif
1044 if (g_enmSvnVersion < kScmSvnVersion_1_7)
1045 {
1046 /*
1047 * Hack: check if the .svn/ dir exists.
1048 */
1049 char szPath[RTPATH_MAX];
1050 int rc = RTPathJoin(szPath, sizeof(szPath), pszDir, ".svn");
1051 if (RT_SUCCESS(rc))
1052 return RTDirExists(szPath);
1053 }
1054 else
1055 {
1056 const char *apszArgs[] = { g_szSvnPath, "propget", "svn:no-such-property", pszDir, NULL };
1057 char *pszValue;
1058 int rc = scmSvnRunAndGetOutput(NULL, apszArgs, true, &pszValue);
1059 if (RT_SUCCESS(rc))
1060 {
1061 RTStrFree(pszValue);
1062 return true;
1063 }
1064 }
1065 return false;
1066}
1067
1068
1069#ifdef SCM_WITH_DYNAMIC_LIB_SVN
1070/**
1071 * Checks if @a pszPath exists in the current WC.
1072 *
1073 * @returns IPRT status code - VERR_NOT_SUPPORT if fallback should be attempted.
1074 * @param pszPath Path to the object that should be investigated.
1075 * @param pszProperty The property name.
1076 * @param ppszValue Where to return the property value. Optional.
1077 */
1078static int scmSvnQueryPropertyUsingApi(const char *pszPath, const char *pszProperty, char **ppszValue)
1079{
1080 int rc = VERR_NOT_SUPPORTED;
1081
1082 /* svn_client_propget4 and later requires absolute target path. */
1083 char szAbsPath[RTPATH_MAX];
1084 int rc2 = scmSvnAbsPath(pszPath, szAbsPath, sizeof(szAbsPath));
1085 if (RT_SUCCESS(rc2))
1086 {
1087 /* Create calling context. */
1088 apr_pool_t *pPool = g_pfnSvnPoolCreateEx(NULL, NULL);
1089 if (pPool)
1090 {
1091 svn_client_ctx_t *pCtx = NULL;
1092 svn_error_t *pErr = g_pfnSvnClientCreateContext(&pCtx, pPool);
1093 if (!pErr)
1094 {
1095 /* Make the call. */
1096 apr_hash_t *pHash = NULL;
1097 svn_opt_revision_t Rev;
1098 RT_ZERO(Rev);
1099 Rev.kind = svn_opt_revision_base;
1100 Rev.value.number = -1L;
1101 pErr = g_pfnSvnClientPropGet4(&pHash, pszProperty, szAbsPath, &Rev, &Rev,
1102 NULL /*pActualRev*/, svn_depth_empty, NULL /*pChangeList*/, pCtx, pPool, pPool);
1103 if (!pErr)
1104 {
1105 /* Get the first value, if any. */
1106 rc = VERR_NOT_FOUND;
1107 apr_hash_index_t *pHashIdx = g_pfnAprHashFirst(pPool, pHash);
1108 if (pHashIdx)
1109 {
1110 const char **ppszFirst = (const char **)g_pfnAprHashThisVal(pHashIdx);
1111 if (ppszFirst && *ppszFirst)
1112 {
1113 if (ppszValue)
1114 rc = RTStrDupEx(ppszValue, *ppszFirst);
1115 else
1116 rc = VINF_SUCCESS;
1117 }
1118 }
1119 }
1120 else if (pErr->apr_err == SVN_ERR_UNVERSIONED_RESOURCE)
1121 rc = VERR_INVALID_STATE;
1122 else
1123 rc = VERR_GENERAL_FAILURE;
1124 }
1125 g_pfnAprPoolDestroy(pPool);
1126 }
1127 }
1128 return rc;
1129}
1130#endif /* SCM_WITH_DYNAMIC_LIB_SVN */
1131
1132
1133/**
1134 * Queries the value of an SVN property.
1135 *
1136 * This will automatically adjust for scheduled changes.
1137 *
1138 * @returns IPRT status code.
1139 * @retval VERR_INVALID_STATE if not a SVN WC file.
1140 * @retval VERR_NOT_FOUND if the property wasn't found.
1141 * @param pState The rewrite state to work on.
1142 * @param pszName The property name.
1143 * @param ppszValue Where to return the property value. Free this
1144 * using RTStrFree. Optional.
1145 */
1146int ScmSvnQueryProperty(PSCMRWSTATE pState, const char *pszName, char **ppszValue)
1147{
1148 int rc;
1149
1150 /*
1151 * Look it up in the scheduled changes.
1152 */
1153 size_t i = pState->cSvnPropChanges;
1154 while (i-- > 0)
1155 if (!strcmp(pState->paSvnPropChanges[i].pszName, pszName))
1156 {
1157 const char *pszValue = pState->paSvnPropChanges[i].pszValue;
1158 if (!pszValue)
1159 return VERR_NOT_FOUND;
1160 if (ppszValue)
1161 return RTStrDupEx(ppszValue, pszValue);
1162 return VINF_SUCCESS;
1163 }
1164
1165 scmSvnFindSvnBinary(pState);
1166
1167#ifdef SCM_WITH_DYNAMIC_LIB_SVN
1168 if (g_fSvnFunctionPointersValid)
1169 {
1170 rc = scmSvnQueryPropertyUsingApi(pState->pszFilename, pszName, ppszValue);
1171 if (rc != VERR_NOT_SUPPORTED)
1172 return rc;
1173 /* Fallback: */
1174 }
1175#endif
1176
1177 if (g_enmSvnVersion < kScmSvnVersion_1_7)
1178 {
1179 /*
1180 * Hack: Read the .svn/props/<file>.svn-work file exists.
1181 */
1182 char szPath[RTPATH_MAX];
1183 rc = scmSvnConstructName(pState, ".svn/props/", ".svn-work", szPath);
1184 if (RT_SUCCESS(rc) && !RTFileExists(szPath))
1185 rc = scmSvnConstructName(pState, ".svn/prop-base/", ".svn-base", szPath);
1186 if (RT_SUCCESS(rc))
1187 {
1188 SCMSTREAM Stream;
1189 rc = ScmStreamInitForReading(&Stream, szPath);
1190 if (RT_SUCCESS(rc))
1191 {
1192 /*
1193 * The current format is K len\n<name>\nV len\n<value>\n" ... END.
1194 */
1195 rc = VERR_NOT_FOUND;
1196 size_t const cchName = strlen(pszName);
1197 SCMEOL enmEol;
1198 size_t cchLine;
1199 const char *pchLine;
1200 while ((pchLine = ScmStreamGetLine(&Stream, &cchLine, &enmEol)) != NULL)
1201 {
1202 /*
1203 * Parse the 'K num' / 'END' line.
1204 */
1205 if ( cchLine == 3
1206 && !memcmp(pchLine, "END", 3))
1207 break;
1208 size_t cchKey;
1209 if ( cchLine < 3
1210 || pchLine[0] != 'K'
1211 || pchLine[1] != ' '
1212 || !scmSvnReadNumber(&pchLine[2], cchLine - 2, &cchKey)
1213 || cchKey == 0
1214 || cchKey > 4096)
1215 {
1216 RTMsgError("%s:%u: Unexpected data '%.*s'\n", szPath, ScmStreamTellLine(&Stream), cchLine, pchLine);
1217 rc = VERR_PARSE_ERROR;
1218 break;
1219 }
1220
1221 /*
1222 * Match the key and skip to the value line. Don't bother with
1223 * names containing EOL markers.
1224 */
1225 size_t const offKey = ScmStreamTell(&Stream);
1226 bool fMatch = cchName == cchKey;
1227 if (fMatch)
1228 {
1229 pchLine = ScmStreamGetLine(&Stream, &cchLine, &enmEol);
1230 if (!pchLine)
1231 break;
1232 fMatch = cchLine == cchName
1233 && !memcmp(pchLine, pszName, cchName);
1234 }
1235
1236 if (RT_FAILURE(ScmStreamSeekAbsolute(&Stream, offKey + cchKey)))
1237 break;
1238 if (RT_FAILURE(ScmStreamSeekByLine(&Stream, ScmStreamTellLine(&Stream) + 1)))
1239 break;
1240
1241 /*
1242 * Read and Parse the 'V num' line.
1243 */
1244 pchLine = ScmStreamGetLine(&Stream, &cchLine, &enmEol);
1245 if (!pchLine)
1246 break;
1247 size_t cchValue;
1248 if ( cchLine < 3
1249 || pchLine[0] != 'V'
1250 || pchLine[1] != ' '
1251 || !scmSvnReadNumber(&pchLine[2], cchLine - 2, &cchValue)
1252 || cchValue > _1M)
1253 {
1254 RTMsgError("%s:%u: Unexpected data '%.*s'\n", szPath, ScmStreamTellLine(&Stream), cchLine, pchLine);
1255 rc = VERR_PARSE_ERROR;
1256 break;
1257 }
1258
1259 /*
1260 * If we have a match, allocate a return buffer and read the
1261 * value into it. Otherwise skip this value and continue
1262 * searching.
1263 */
1264 if (fMatch)
1265 {
1266 if (!ppszValue)
1267 rc = VINF_SUCCESS;
1268 else
1269 {
1270 char *pszValue;
1271 rc = RTStrAllocEx(&pszValue, cchValue + 1);
1272 if (RT_SUCCESS(rc))
1273 {
1274 rc = ScmStreamRead(&Stream, pszValue, cchValue);
1275 if (RT_SUCCESS(rc))
1276 *ppszValue = pszValue;
1277 else
1278 RTStrFree(pszValue);
1279 }
1280 }
1281 break;
1282 }
1283
1284 if (RT_FAILURE(ScmStreamSeekRelative(&Stream, cchValue)))
1285 break;
1286 if (RT_FAILURE(ScmStreamSeekByLine(&Stream, ScmStreamTellLine(&Stream) + 1)))
1287 break;
1288 }
1289
1290 if (RT_FAILURE(ScmStreamGetStatus(&Stream)))
1291 {
1292 rc = ScmStreamGetStatus(&Stream);
1293 RTMsgError("%s: stream error %Rrc\n", szPath, rc);
1294 }
1295 ScmStreamDelete(&Stream);
1296 }
1297 }
1298
1299 if (rc == VERR_FILE_NOT_FOUND)
1300 rc = VERR_NOT_FOUND;
1301 }
1302 else
1303 {
1304 const char *apszArgs[] = { g_szSvnPath, "propget", "--strict", pszName, pState->pszFilename, NULL };
1305 char *pszValue;
1306 rc = scmSvnRunAndGetOutput(pState, apszArgs, false, &pszValue);
1307 if (RT_SUCCESS(rc))
1308 {
1309 if (pszValue && *pszValue)
1310 {
1311 if (ppszValue)
1312 {
1313 *ppszValue = pszValue;
1314 pszValue = NULL;
1315 }
1316 }
1317 else
1318 rc = VERR_NOT_FOUND;
1319 RTStrFree(pszValue);
1320 }
1321 }
1322 return rc;
1323}
1324
1325
1326/**
1327 * Schedules the setting of a property.
1328 *
1329 * @returns IPRT status code.
1330 * @retval VERR_INVALID_STATE if not a SVN WC file.
1331 * @param pState The rewrite state to work on.
1332 * @param pszName The name of the property to set.
1333 * @param pszValue The value. NULL means deleting it.
1334 */
1335int ScmSvnSetProperty(PSCMRWSTATE pState, const char *pszName, const char *pszValue)
1336{
1337 /*
1338 * Update any existing entry first.
1339 */
1340 size_t i = pState->cSvnPropChanges;
1341 while (i-- > 0)
1342 if (!strcmp(pState->paSvnPropChanges[i].pszName, pszName))
1343 {
1344 if (!pszValue)
1345 {
1346 RTStrFree(pState->paSvnPropChanges[i].pszValue);
1347 pState->paSvnPropChanges[i].pszValue = NULL;
1348 }
1349 else
1350 {
1351 char *pszCopy;
1352 int rc = RTStrDupEx(&pszCopy, pszValue);
1353 if (RT_FAILURE(rc))
1354 return rc;
1355 pState->paSvnPropChanges[i].pszValue = pszCopy;
1356 }
1357 return VINF_SUCCESS;
1358 }
1359
1360 /*
1361 * Insert a new entry.
1362 */
1363 i = pState->cSvnPropChanges;
1364 if ((i % 32) == 0)
1365 {
1366 void *pvNew = RTMemRealloc(pState->paSvnPropChanges, (i + 32) * sizeof(SCMSVNPROP));
1367 if (!pvNew)
1368 return VERR_NO_MEMORY;
1369 pState->paSvnPropChanges = (PSCMSVNPROP)pvNew;
1370 }
1371
1372 pState->paSvnPropChanges[i].pszName = RTStrDup(pszName);
1373 pState->paSvnPropChanges[i].pszValue = pszValue ? RTStrDup(pszValue) : NULL;
1374 if ( pState->paSvnPropChanges[i].pszName
1375 && (pState->paSvnPropChanges[i].pszValue || !pszValue) )
1376 pState->cSvnPropChanges = i + 1;
1377 else
1378 {
1379 RTStrFree(pState->paSvnPropChanges[i].pszName);
1380 pState->paSvnPropChanges[i].pszName = NULL;
1381 RTStrFree(pState->paSvnPropChanges[i].pszValue);
1382 pState->paSvnPropChanges[i].pszValue = NULL;
1383 return VERR_NO_MEMORY;
1384 }
1385 return VINF_SUCCESS;
1386}
1387
1388
1389/**
1390 * Schedules a property deletion.
1391 *
1392 * @returns IPRT status code.
1393 * @param pState The rewrite state to work on.
1394 * @param pszName The name of the property to delete.
1395 */
1396int ScmSvnDelProperty(PSCMRWSTATE pState, const char *pszName)
1397{
1398 return ScmSvnSetProperty(pState, pszName, NULL);
1399}
1400
1401
1402/**
1403 * Applies any SVN property changes to the work copy of the file.
1404 *
1405 * @returns IPRT status code.
1406 * @param pState The rewrite state which SVN property changes
1407 * should be applied.
1408 */
1409int ScmSvnDisplayChanges(PSCMRWSTATE pState)
1410{
1411 size_t i = pState->cSvnPropChanges;
1412 while (i-- > 0)
1413 {
1414 const char *pszName = pState->paSvnPropChanges[i].pszName;
1415 const char *pszValue = pState->paSvnPropChanges[i].pszValue;
1416 if (pszValue)
1417 ScmVerbose(pState, 0, "svn propset '%s' '%s' %s\n", pszName, pszValue, pState->pszFilename);
1418 else
1419 ScmVerbose(pState, 0, "svn propdel '%s' %s\n", pszName, pState->pszFilename);
1420 }
1421
1422 return VINF_SUCCESS;
1423}
1424
1425/**
1426 * Applies any SVN property changes to the work copy of the file.
1427 *
1428 * @returns IPRT status code.
1429 * @param pState The rewrite state which SVN property changes
1430 * should be applied.
1431 */
1432int ScmSvnApplyChanges(PSCMRWSTATE pState)
1433{
1434 scmSvnFindSvnBinary(pState);
1435
1436#ifdef SCM_WITH_LATER
1437 if (0)
1438 {
1439 return ...;
1440 }
1441
1442 /* Fallback: */
1443#endif
1444
1445 /*
1446 * Iterate thru the changes and apply them by starting the svn client.
1447 */
1448 for (size_t i = 0; i < pState->cSvnPropChanges; i++)
1449 {
1450 const char *apszArgv[6];
1451 apszArgv[0] = g_szSvnPath;
1452 apszArgv[1] = pState->paSvnPropChanges[i].pszValue ? "propset" : "propdel";
1453 apszArgv[2] = pState->paSvnPropChanges[i].pszName;
1454 int iArg = 3;
1455 if (pState->paSvnPropChanges[i].pszValue)
1456 apszArgv[iArg++] = pState->paSvnPropChanges[i].pszValue;
1457 apszArgv[iArg++] = pState->pszFilename;
1458 apszArgv[iArg++] = NULL;
1459
1460 int rc = scmSvnRun(pState, apszArgv, false);
1461 if (RT_FAILURE(rc))
1462 return rc;
1463 }
1464
1465 return VINF_SUCCESS;
1466}
1467
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