1 /* repos.c : repository creation; shared and exclusive repository locking
3 * ====================================================================
4 * Copyright (c) 2000-2007 CollabNet. All rights reserved.
6 * This software is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at http://subversion.tigris.org/license-1.html.
9 * If newer versions of this license are posted there, you may use a
10 * newer version instead, at your option.
12 * This software consists of voluntary contributions made by many
13 * individuals. For exact contribution history, see the revision
14 * history and logs, available at http://subversion.tigris.org/.
15 * ====================================================================
20 #include <apr_pools.h>
21 #include <apr_file_io.h>
23 #include "svn_pools.h"
24 #include "svn_error.h"
29 #include "svn_ra.h" /* for SVN_RA_CAPABILITY_* */
30 #include "svn_repos.h"
31 #include "svn_private_config.h" /* for SVN_TEMPLATE_ROOT_DIR */
35 /* Used to terminate lines in large multi-line string literals. */
36 #define NL APR_EOL_STR
39 /* Path accessor functions. */
43 svn_repos_path(svn_repos_t
*repos
, apr_pool_t
*pool
)
45 return apr_pstrdup(pool
, repos
->path
);
50 svn_repos_db_env(svn_repos_t
*repos
, apr_pool_t
*pool
)
52 return apr_pstrdup(pool
, repos
->db_path
);
57 svn_repos_conf_dir(svn_repos_t
*repos
, apr_pool_t
*pool
)
59 return apr_pstrdup(pool
, repos
->conf_path
);
64 svn_repos_svnserve_conf(svn_repos_t
*repos
, apr_pool_t
*pool
)
66 return svn_path_join(repos
->conf_path
, SVN_REPOS__CONF_SVNSERVE_CONF
, pool
);
71 svn_repos_lock_dir(svn_repos_t
*repos
, apr_pool_t
*pool
)
73 return apr_pstrdup(pool
, repos
->lock_path
);
78 svn_repos_db_lockfile(svn_repos_t
*repos
, apr_pool_t
*pool
)
80 return svn_path_join(repos
->lock_path
, SVN_REPOS__DB_LOCKFILE
, pool
);
85 svn_repos_db_logs_lockfile(svn_repos_t
*repos
, apr_pool_t
*pool
)
87 return svn_path_join(repos
->lock_path
, SVN_REPOS__DB_LOGS_LOCKFILE
, pool
);
91 svn_repos_hook_dir(svn_repos_t
*repos
, apr_pool_t
*pool
)
93 return apr_pstrdup(pool
, repos
->hook_path
);
98 svn_repos_start_commit_hook(svn_repos_t
*repos
, apr_pool_t
*pool
)
100 return svn_path_join(repos
->hook_path
, SVN_REPOS__HOOK_START_COMMIT
, pool
);
105 svn_repos_pre_commit_hook(svn_repos_t
*repos
, apr_pool_t
*pool
)
107 return svn_path_join(repos
->hook_path
, SVN_REPOS__HOOK_PRE_COMMIT
, pool
);
112 svn_repos_pre_lock_hook(svn_repos_t
*repos
, apr_pool_t
*pool
)
114 return svn_path_join(repos
->hook_path
, SVN_REPOS__HOOK_PRE_LOCK
, pool
);
119 svn_repos_pre_unlock_hook(svn_repos_t
*repos
, apr_pool_t
*pool
)
121 return svn_path_join(repos
->hook_path
, SVN_REPOS__HOOK_PRE_UNLOCK
, pool
);
125 svn_repos_post_lock_hook(svn_repos_t
*repos
, apr_pool_t
*pool
)
127 return svn_path_join(repos
->hook_path
, SVN_REPOS__HOOK_POST_LOCK
, pool
);
132 svn_repos_post_unlock_hook(svn_repos_t
*repos
, apr_pool_t
*pool
)
134 return svn_path_join(repos
->hook_path
, SVN_REPOS__HOOK_POST_UNLOCK
, pool
);
139 svn_repos_post_commit_hook(svn_repos_t
*repos
, apr_pool_t
*pool
)
141 return svn_path_join(repos
->hook_path
, SVN_REPOS__HOOK_POST_COMMIT
, pool
);
146 svn_repos_pre_revprop_change_hook(svn_repos_t
*repos
, apr_pool_t
*pool
)
148 return svn_path_join(repos
->hook_path
, SVN_REPOS__HOOK_PRE_REVPROP_CHANGE
,
154 svn_repos_post_revprop_change_hook(svn_repos_t
*repos
, apr_pool_t
*pool
)
156 return svn_path_join(repos
->hook_path
, SVN_REPOS__HOOK_POST_REVPROP_CHANGE
,
162 create_repos_dir(const char *path
, apr_pool_t
*pool
)
166 err
= svn_io_dir_make(path
, APR_OS_DEFAULT
, pool
);
167 if (err
&& (APR_STATUS_IS_EEXIST(err
->apr_err
)))
169 svn_boolean_t is_empty
;
171 svn_error_clear(err
);
173 SVN_ERR(svn_io_dir_empty(&is_empty
, path
, pool
));
178 err
= svn_error_createf(SVN_ERR_DIR_NOT_EMPTY
, 0,
179 _("'%s' exists and is non-empty"),
180 svn_path_local_style(path
, pool
));
186 static const char * bdb_lock_file_contents
=
187 "DB lock file, representing locks on the versioned filesystem." NL
189 "All accessors -- both readers and writers -- of the repository's" NL
190 "Berkeley DB environment take out shared locks on this file, and" NL
191 "each accessor removes its lock when done. If and when the DB" NL
192 "recovery procedure is run, the recovery code takes out an" NL
193 "exclusive lock on this file, so we can be sure no one else is" NL
194 "using the DB during the recovery." NL
196 "You should never have to edit or remove this file." NL
;
198 static const char * bdb_logs_lock_file_contents
=
199 "DB logs lock file, representing locks on the versioned filesystem logs." NL
201 "All log manipulators of the repository's Berkeley DB environment" NL
202 "take out exclusive locks on this file to ensure that only one" NL
203 "accessor manipulates the logs at a time." NL
205 "You should never have to edit or remove this file." NL
;
207 static const char * pre12_compat_unneeded_file_contents
=
208 "This file is not used by Subversion 1.3.x or later." NL
209 "However, its existence is required for compatibility with" NL
210 "Subversion 1.2.x or earlier." NL
;
212 /* Create the DB logs lockfile. */
214 create_db_logs_lock(svn_repos_t
*repos
, apr_pool_t
*pool
) {
215 const char *contents
;
216 const char *lockfile_path
;
218 lockfile_path
= svn_repos_db_logs_lockfile(repos
, pool
);
219 if (strcmp(repos
->fs_type
, SVN_FS_TYPE_BDB
) == 0)
220 contents
= bdb_logs_lock_file_contents
;
222 contents
= pre12_compat_unneeded_file_contents
;
224 SVN_ERR_W(svn_io_file_create(lockfile_path
, contents
, pool
),
225 _("Creating db logs lock file"));
230 /* Create the DB lockfile. */
232 create_db_lock(svn_repos_t
*repos
, apr_pool_t
*pool
) {
233 const char *contents
;
234 const char *lockfile_path
;
236 lockfile_path
= svn_repos_db_lockfile(repos
, pool
);
237 if (strcmp(repos
->fs_type
, SVN_FS_TYPE_BDB
) == 0)
238 contents
= bdb_lock_file_contents
;
240 contents
= pre12_compat_unneeded_file_contents
;
242 SVN_ERR_W(svn_io_file_create(lockfile_path
, contents
, pool
),
243 _("Creating db lock file"));
249 create_locks(svn_repos_t
*repos
, apr_pool_t
*pool
)
251 /* Create the locks directory. */
252 SVN_ERR_W(create_repos_dir(repos
->lock_path
, pool
),
253 _("Creating lock dir"));
255 SVN_ERR(create_db_lock(repos
, pool
));
256 SVN_ERR(create_db_logs_lock(repos
, pool
));
262 #define HOOKS_ENVIRONMENT_TEXT \
263 "# The hook program typically does not inherit the environment of" NL \
264 "# its parent process. For example, a common problem is for the" NL \
265 "# PATH environment variable to not be set to its usual value, so" NL \
266 "# that subprograms fail to launch unless invoked via absolute path." NL \
267 "# If you're having unexpected problems with a hook program, the" NL \
268 "# culprit may be unusual (or missing) environment variables." NL
270 #define PREWRITTEN_HOOKS_TEXT \
271 "# For more examples and pre-written hooks, see those in" NL \
272 "# the Subversion repository at" NL \
273 "# http://svn.collab.net/repos/svn/trunk/tools/hook-scripts/ and" NL \
274 "# http://svn.collab.net/repos/svn/trunk/contrib/hook-scripts/" NL
278 create_hooks(svn_repos_t
*repos
, apr_pool_t
*pool
)
280 const char *this_path
, *contents
;
282 /* Create the hook directory. */
283 SVN_ERR_W(create_repos_dir(repos
->hook_path
, pool
),
284 _("Creating hook directory"));
286 /*** Write a default template for each standard hook file. */
288 /* Start-commit hook. */
290 this_path
= apr_psprintf(pool
, "%s%s",
291 svn_repos_start_commit_hook(repos
, pool
),
292 SVN_REPOS__HOOK_DESC_EXT
);
294 #define SCRIPT_NAME SVN_REPOS__HOOK_START_COMMIT
299 "# START-COMMIT HOOK" NL
301 "# The start-commit hook is invoked before a Subversion txn is created" NL
302 "# in the process of doing a commit. Subversion runs this hook" NL
303 "# by invoking a program (script, executable, binary, etc.) named" NL
304 "# '"SCRIPT_NAME
"' (for which this file is a template)" NL
305 "# with the following ordered arguments:" NL
307 "# [1] REPOS-PATH (the path to this repository)" NL
308 "# [2] USER (the authenticated user attempting to commit)" NL
309 "# [3] CAPABILITIES (a colon-separated list of capabilities reported" NL
310 "# by the client; see note below)" NL
312 "# Note: The CAPABILITIES parameter is new in Subversion 1.5, and 1.5" NL
313 "# clients will typically report at least the \"" \
314 SVN_RA_CAPABILITY_MERGEINFO
"\" capability." NL
315 "# If there are other capabilities, then the list is colon-separated," NL
316 "# e.g.: \"" SVN_RA_CAPABILITY_MERGEINFO
":some-other-capability\" " \
317 "(the order is undefined)." NL
319 "# The list is self-reported by the client. Therefore, you should not" NL
320 "# make security assumptions based on the capabilities list, nor should" NL
321 "# you assume that clients reliably report every capability they have." NL
323 "# The working directory for this hook program's invocation is undefined," NL
324 "# so the program should set one explicitly if it cares." NL
326 "# If the hook program exits with success, the commit continues; but" NL
327 "# if it exits with failure (non-zero), the commit is stopped before" NL
328 "# a Subversion txn is created, and STDERR is returned to the client." NL
330 "# On a Unix system, the normal procedure is to have '"SCRIPT_NAME
"'" NL
331 "# invoke other programs to do the real work, though it may do the" NL
332 "# work itself too." NL
334 "# Note that '"SCRIPT_NAME
"' must be executable by the user(s) who will" NL
335 "# invoke it (typically the user httpd runs as), and that user must" NL
336 "# have filesystem-level permission to access the repository." NL
338 "# On a Windows system, you should name the hook program" NL
339 "# '"SCRIPT_NAME
".bat' or '"SCRIPT_NAME
".exe'," NL
340 "# but the basic idea is the same." NL
342 HOOKS_ENVIRONMENT_TEXT
344 "# Here is an example hook script, for a Unix /bin/sh interpreter." NL
345 PREWRITTEN_HOOKS_TEXT
351 "commit-allower.pl --repository \"$REPOS\" --user \"$USER\" || exit 1" NL
352 "special-auth-check.py --user \"$USER\" --auth-level 3 || exit 1" NL
354 "# All checks passed, so allow the commit." NL
359 SVN_ERR_W(svn_io_file_create(this_path
, contents
, pool
),
360 _("Creating start-commit hook"));
361 } /* end start-commit hook */
363 /* Pre-commit hook. */
365 this_path
= apr_psprintf(pool
, "%s%s",
366 svn_repos_pre_commit_hook(repos
, pool
),
367 SVN_REPOS__HOOK_DESC_EXT
);
369 #define SCRIPT_NAME SVN_REPOS__HOOK_PRE_COMMIT
374 "# PRE-COMMIT HOOK" NL
376 "# The pre-commit hook is invoked before a Subversion txn is" NL
377 "# committed. Subversion runs this hook by invoking a program" NL
378 "# (script, executable, binary, etc.) named '"SCRIPT_NAME
"' (for which" NL
379 "# this file is a template), with the following ordered arguments:" NL
381 "# [1] REPOS-PATH (the path to this repository)" NL
382 "# [2] TXN-NAME (the name of the txn about to be committed)" NL
384 "# The default working directory for the invocation is undefined, so" NL
385 "# the program should set one explicitly if it cares." NL
387 "# If the hook program exits with success, the txn is committed; but" NL
388 "# if it exits with failure (non-zero), the txn is aborted, no commit" NL
389 "# takes place, and STDERR is returned to the client. The hook" NL
390 "# program can use the 'svnlook' utility to help it examine the txn." NL
392 "# On a Unix system, the normal procedure is to have '"SCRIPT_NAME
"'" NL
393 "# invoke other programs to do the real work, though it may do the" NL
394 "# work itself too." NL
396 "# *** NOTE: THE HOOK PROGRAM MUST NOT MODIFY THE TXN, EXCEPT ***" NL
397 "# *** FOR REVISION PROPERTIES (like svn:log or svn:author). ***" NL
399 "# This is why we recommend using the read-only 'svnlook' utility." NL
400 "# In the future, Subversion may enforce the rule that pre-commit" NL
401 "# hooks should not modify the versioned data in txns, or else come" NL
402 "# up with a mechanism to make it safe to do so (by informing the" NL
403 "# committing client of the changes). However, right now neither" NL
404 "# mechanism is implemented, so hook writers just have to be careful." NL
406 "# Note that '"SCRIPT_NAME
"' must be executable by the user(s) who will" NL
407 "# invoke it (typically the user httpd runs as), and that user must" NL
408 "# have filesystem-level permission to access the repository." NL
410 "# On a Windows system, you should name the hook program" NL
411 "# '"SCRIPT_NAME
".bat' or '"SCRIPT_NAME
".exe'," NL
412 "# but the basic idea is the same." NL
414 HOOKS_ENVIRONMENT_TEXT
416 "# Here is an example hook script, for a Unix /bin/sh interpreter." NL
417 PREWRITTEN_HOOKS_TEXT
423 "# Make sure that the log message contains some text." NL
424 "SVNLOOK=" SVN_BINDIR
"/svnlook" NL
425 "$SVNLOOK log -t \"$TXN\" \"$REPOS\" | \\" NL
426 " grep \"[a-zA-Z0-9]\" > /dev/null || exit 1" NL
428 "# Check that the author of this commit has the rights to perform" NL
429 "# the commit on the files and directories being modified." NL
430 "commit-access-control.pl \"$REPOS\" \"$TXN\" commit-access-control.cfg || exit 1"
433 "# All checks passed, so allow the commit." NL
438 SVN_ERR_W(svn_io_file_create(this_path
, contents
, pool
),
439 _("Creating pre-commit hook"));
440 } /* end pre-commit hook */
443 /* Pre-revprop-change hook. */
445 this_path
= apr_psprintf(pool
, "%s%s",
446 svn_repos_pre_revprop_change_hook(repos
, pool
),
447 SVN_REPOS__HOOK_DESC_EXT
);
449 #define SCRIPT_NAME SVN_REPOS__HOOK_PRE_REVPROP_CHANGE
454 "# PRE-REVPROP-CHANGE HOOK" NL
456 "# The pre-revprop-change hook is invoked before a revision property" NL
457 "# is added, modified or deleted. Subversion runs this hook by invoking" NL
458 "# a program (script, executable, binary, etc.) named '"SCRIPT_NAME
"'" NL
459 "# (for which this file is a template), with the following ordered" NL
462 "# [1] REPOS-PATH (the path to this repository)" NL
463 "# [2] REVISION (the revision being tweaked)" NL
464 "# [3] USER (the username of the person tweaking the property)" NL
465 "# [4] PROPNAME (the property being set on the revision)" NL
466 "# [5] ACTION (the property is being 'A'dded, 'M'odified, or 'D'eleted)"
469 "# [STDIN] PROPVAL ** the new property value is passed via STDIN." NL
471 "# If the hook program exits with success, the propchange happens; but" NL
472 "# if it exits with failure (non-zero), the propchange doesn't happen." NL
473 "# The hook program can use the 'svnlook' utility to examine the " NL
474 "# existing value of the revision property." NL
476 "# WARNING: unlike other hooks, this hook MUST exist for revision" NL
477 "# properties to be changed. If the hook does not exist, Subversion " NL
478 "# will behave as if the hook were present, but failed. The reason" NL
479 "# for this is that revision properties are UNVERSIONED, meaning that" NL
480 "# a successful propchange is destructive; the old value is gone" NL
481 "# forever. We recommend the hook back up the old value somewhere." NL
483 "# On a Unix system, the normal procedure is to have '"SCRIPT_NAME
"'" NL
484 "# invoke other programs to do the real work, though it may do the" NL
485 "# work itself too." NL
487 "# Note that '"SCRIPT_NAME
"' must be executable by the user(s) who will" NL
488 "# invoke it (typically the user httpd runs as), and that user must" NL
489 "# have filesystem-level permission to access the repository." NL
491 "# On a Windows system, you should name the hook program" NL
492 "# '"SCRIPT_NAME
".bat' or '"SCRIPT_NAME
".exe'," NL
493 "# but the basic idea is the same." NL
495 HOOKS_ENVIRONMENT_TEXT
497 "# Here is an example hook script, for a Unix /bin/sh interpreter." NL
498 PREWRITTEN_HOOKS_TEXT
507 "if [ \"$ACTION\" = \"M\" -a \"$PROPNAME\" = \"svn:log\" ]; then exit 0; fi" NL
509 "echo \"Changing revision properties other than svn:log is prohibited\" >&2" NL
514 SVN_ERR_W(svn_io_file_create(this_path
, contents
, pool
),
515 _("Creating pre-revprop-change hook"));
516 } /* end pre-revprop-change hook */
521 this_path
= apr_psprintf(pool
, "%s%s",
522 svn_repos_pre_lock_hook(repos
, pool
),
523 SVN_REPOS__HOOK_DESC_EXT
);
525 #define SCRIPT_NAME SVN_REPOS__HOOK_PRE_LOCK
532 "# The pre-lock hook is invoked before an exclusive lock is" NL
533 "# created. Subversion runs this hook by invoking a program " NL
534 "# (script, executable, binary, etc.) named '"SCRIPT_NAME
"' (for which" NL
535 "# this file is a template), with the following ordered arguments:" NL
537 "# [1] REPOS-PATH (the path to this repository)" NL
538 "# [2] PATH (the path in the repository about to be locked)" NL
539 "# [3] USER (the user creating the lock)" NL
541 "# The default working directory for the invocation is undefined, so" NL
542 "# the program should set one explicitly if it cares." NL
544 "# If the hook program exits with success, the lock is created; but" NL
545 "# if it exits with failure (non-zero), the lock action is aborted" NL
546 "# and STDERR is returned to the client." NL
548 "# On a Unix system, the normal procedure is to have '"SCRIPT_NAME
"'" NL
549 "# invoke other programs to do the real work, though it may do the" NL
550 "# work itself too." NL
552 "# Note that '"SCRIPT_NAME
"' must be executable by the user(s) who will" NL
553 "# invoke it (typically the user httpd runs as), and that user must" NL
554 "# have filesystem-level permission to access the repository." NL
556 "# On a Windows system, you should name the hook program" NL
557 "# '"SCRIPT_NAME
".bat' or '"SCRIPT_NAME
".exe'," NL
558 "# but the basic idea is the same." NL
560 "# Here is an example hook script, for a Unix /bin/sh interpreter:" NL
566 "# If a lock exists and is owned by a different person, don't allow it" NL
567 "# to be stolen (e.g., with 'svn lock --force ...')." NL
569 "# (Maybe this script could send email to the lock owner?)" NL
570 "SVNLOOK=" SVN_BINDIR
"/svnlook" NL
574 "LOCK_OWNER=`$SVNLOOK lock \"$REPOS\" \"$PATH\" | \\" NL
575 " $GREP '^Owner: ' | $SED 's/Owner: //'`" NL
577 "# If we get no result from svnlook, there's no lock, allow the lock to" NL
579 "if [ \"$LOCK_OWNER\" = \"\" ]; then" NL
583 "# If the person locking matches the lock's owner, allow the lock to" NL
585 "if [ \"$LOCK_OWNER\" = \"$USER\" ]; then" NL
589 "# Otherwise, we've got an owner mismatch, so return failure:" NL
590 "echo \"Error: $PATH already locked by ${LOCK_OWNER}.\" 1>&2" NL
595 SVN_ERR_W(svn_io_file_create(this_path
, contents
, pool
),
596 "Creating pre-lock hook");
597 } /* end pre-lock hook */
600 /* Pre-unlock hook. */
602 this_path
= apr_psprintf(pool
, "%s%s",
603 svn_repos_pre_unlock_hook(repos
, pool
),
604 SVN_REPOS__HOOK_DESC_EXT
);
606 #define SCRIPT_NAME SVN_REPOS__HOOK_PRE_UNLOCK
611 "# PRE-UNLOCK HOOK" NL
613 "# The pre-unlock hook is invoked before an exclusive lock is" NL
614 "# destroyed. Subversion runs this hook by invoking a program " NL
615 "# (script, executable, binary, etc.) named '"SCRIPT_NAME
"' (for which" NL
616 "# this file is a template), with the following ordered arguments:" NL
618 "# [1] REPOS-PATH (the path to this repository)" NL
619 "# [2] PATH (the path in the repository about to be unlocked)" NL
620 "# [3] USER (the user destroying the lock)" NL
622 "# The default working directory for the invocation is undefined, so" NL
623 "# the program should set one explicitly if it cares." NL
625 "# If the hook program exits with success, the lock is destroyed; but" NL
626 "# if it exits with failure (non-zero), the unlock action is aborted" NL
627 "# and STDERR is returned to the client." NL
629 "# On a Unix system, the normal procedure is to have '"SCRIPT_NAME
"'" NL
630 "# invoke other programs to do the real work, though it may do the" NL
631 "# work itself too." NL
633 "# Note that '"SCRIPT_NAME
"' must be executable by the user(s) who will" NL
634 "# invoke it (typically the user httpd runs as), and that user must" NL
635 "# have filesystem-level permission to access the repository." NL
637 "# On a Windows system, you should name the hook program" NL
638 "# '"SCRIPT_NAME
".bat' or '"SCRIPT_NAME
".exe'," NL
639 "# but the basic idea is the same." NL
641 "# Here is an example hook script, for a Unix /bin/sh interpreter:" NL
647 "# If a lock is owned by a different person, don't allow it be broken." NL
648 "# (Maybe this script could send email to the lock owner?)" NL
650 "SVNLOOK=" SVN_BINDIR
"/svnlook" NL
654 "LOCK_OWNER=`$SVNLOOK lock \"$REPOS\" \"$PATH\" | \\" NL
655 " $GREP '^Owner: ' | $SED 's/Owner: //'`" NL
657 "# If we get no result from svnlook, there's no lock, return success:" NL
658 "if [ \"$LOCK_OWNER\" = \"\" ]; then" NL
662 "# If the person unlocking matches the lock's owner, return success:" NL
663 "if [ \"$LOCK_OWNER\" = \"$USER\" ]; then" NL
667 "# Otherwise, we've got an owner mismatch, so return failure:" NL
668 "echo \"Error: $PATH locked by ${LOCK_OWNER}.\" 1>&2" NL
673 SVN_ERR_W(svn_io_file_create(this_path
, contents
, pool
),
674 "Creating pre-unlock hook");
675 } /* end pre-unlock hook */
679 /* Post-commit hook. */
681 this_path
= apr_psprintf(pool
, "%s%s",
682 svn_repos_post_commit_hook(repos
, pool
),
683 SVN_REPOS__HOOK_DESC_EXT
);
685 #define SCRIPT_NAME SVN_REPOS__HOOK_POST_COMMIT
690 "# POST-COMMIT HOOK" NL
692 "# The post-commit hook is invoked after a commit. Subversion runs" NL
693 "# this hook by invoking a program (script, executable, binary, etc.)" NL
694 "# named '"SCRIPT_NAME
"' (for which this file is a template) with the " NL
695 "# following ordered arguments:" NL
697 "# [1] REPOS-PATH (the path to this repository)" NL
698 "# [2] REV (the number of the revision just committed)" NL
700 "# The default working directory for the invocation is undefined, so" NL
701 "# the program should set one explicitly if it cares." NL
703 "# Because the commit has already completed and cannot be undone," NL
704 "# the exit code of the hook program is ignored. The hook program" NL
705 "# can use the 'svnlook' utility to help it examine the" NL
706 "# newly-committed tree." NL
708 "# On a Unix system, the normal procedure is to have '"SCRIPT_NAME
"'" NL
709 "# invoke other programs to do the real work, though it may do the" NL
710 "# work itself too." NL
712 "# Note that '"SCRIPT_NAME
"' must be executable by the user(s) who will" NL
713 "# invoke it (typically the user httpd runs as), and that user must" NL
714 "# have filesystem-level permission to access the repository." NL
716 "# On a Windows system, you should name the hook program" NL
717 "# '"SCRIPT_NAME
".bat' or '"SCRIPT_NAME
".exe'," NL
718 "# but the basic idea is the same." NL
720 HOOKS_ENVIRONMENT_TEXT
722 "# Here is an example hook script, for a Unix /bin/sh interpreter." NL
723 PREWRITTEN_HOOKS_TEXT
729 "commit-email.pl \"$REPOS\" \"$REV\" commit-watchers@example.org" NL
730 "log-commit.py --repository \"$REPOS\" --revision \"$REV\"" NL
;
734 SVN_ERR_W(svn_io_file_create(this_path
, contents
, pool
),
735 _("Creating post-commit hook"));
736 } /* end post-commit hook */
739 /* Post-lock hook. */
741 this_path
= apr_psprintf(pool
, "%s%s",
742 svn_repos_post_lock_hook(repos
, pool
),
743 SVN_REPOS__HOOK_DESC_EXT
);
745 #define SCRIPT_NAME SVN_REPOS__HOOK_POST_LOCK
750 "# POST-LOCK HOOK" NL
752 "# The post-lock hook is run after a path is locked. Subversion runs" NL
753 "# this hook by invoking a program (script, executable, binary, etc.)" NL
754 "# named '"SCRIPT_NAME
"' (for which this file is a template) with the " NL
755 "# following ordered arguments:" NL
757 "# [1] REPOS-PATH (the path to this repository)" NL
758 "# [2] USER (the user who created the lock)" NL
760 "# The paths that were just locked are passed to the hook via STDIN (as" NL
761 "# of Subversion 1.2, only one path is passed per invocation, but the" NL
762 "# plan is to pass all locked paths at once, so the hook program" NL
763 "# should be written accordingly)." NL
765 "# The default working directory for the invocation is undefined, so" NL
766 "# the program should set one explicitly if it cares." NL
768 "# Because the lock has already been created and cannot be undone," NL
769 "# the exit code of the hook program is ignored. The hook program" NL
770 "# can use the 'svnlook' utility to help it examine the" NL
771 "# newly-created lock." NL
773 "# On a Unix system, the normal procedure is to have '"SCRIPT_NAME
"'" NL
774 "# invoke other programs to do the real work, though it may do the" NL
775 "# work itself too." NL
777 "# Note that '"SCRIPT_NAME
"' must be executable by the user(s) who will" NL
778 "# invoke it (typically the user httpd runs as), and that user must" NL
779 "# have filesystem-level permission to access the repository." NL
781 "# On a Windows system, you should name the hook program" NL
782 "# '"SCRIPT_NAME
".bat' or '"SCRIPT_NAME
".exe'," NL
783 "# but the basic idea is the same." NL
785 "# Here is an example hook script, for a Unix /bin/sh interpreter:" NL
790 "# Send email to interested parties, let them know a lock was created:" NL
791 "mailer.py lock \"$REPOS\" \"$USER\" /path/to/mailer.conf" NL
;
795 SVN_ERR_W(svn_io_file_create(this_path
, contents
, pool
),
796 "Creating post-lock hook");
797 } /* end post-lock hook */
800 /* Post-unlock hook. */
802 this_path
= apr_psprintf(pool
, "%s%s",
803 svn_repos_post_unlock_hook(repos
, pool
),
804 SVN_REPOS__HOOK_DESC_EXT
);
806 #define SCRIPT_NAME SVN_REPOS__HOOK_POST_UNLOCK
811 "# POST-UNLOCK HOOK" NL
813 "# The post-unlock hook runs after a path is unlocked. Subversion runs" NL
814 "# this hook by invoking a program (script, executable, binary, etc.)" NL
815 "# named '"SCRIPT_NAME
"' (for which this file is a template) with the " NL
816 "# following ordered arguments:" NL
818 "# [1] REPOS-PATH (the path to this repository)" NL
819 "# [2] USER (the user who destroyed the lock)" NL
821 "# The paths that were just unlocked are passed to the hook via STDIN" NL
822 "# (as of Subversion 1.2, only one path is passed per invocation, but" NL
823 "# the plan is to pass all unlocked paths at once, so the hook program" NL
824 "# should be written accordingly)." NL
826 "# The default working directory for the invocation is undefined, so" NL
827 "# the program should set one explicitly if it cares." NL
829 "# Because the lock has already been destroyed and cannot be undone," NL
830 "# the exit code of the hook program is ignored." NL
832 "# On a Unix system, the normal procedure is to have '"SCRIPT_NAME
"'" NL
833 "# invoke other programs to do the real work, though it may do the" NL
834 "# work itself too." NL
836 "# Note that '"SCRIPT_NAME
"' must be executable by the user(s) who will" NL
837 "# invoke it (typically the user httpd runs as), and that user must" NL
838 "# have filesystem-level permission to access the repository." NL
840 "# On a Windows system, you should name the hook program" NL
841 "# '"SCRIPT_NAME
".bat' or '"SCRIPT_NAME
".exe'," NL
842 "# but the basic idea is the same." NL
844 "# Here is an example hook script, for a Unix /bin/sh interpreter:" NL
849 "# Send email to interested parties, let them know a lock was removed:" NL
850 "mailer.py unlock \"$REPOS\" \"$USER\" /path/to/mailer.conf" NL
;
854 SVN_ERR_W(svn_io_file_create(this_path
, contents
, pool
),
855 "Creating post-unlock hook");
856 } /* end post-unlock hook */
859 /* Post-revprop-change hook. */
861 this_path
= apr_psprintf(pool
, "%s%s",
862 svn_repos_post_revprop_change_hook(repos
, pool
),
863 SVN_REPOS__HOOK_DESC_EXT
);
865 #define SCRIPT_NAME SVN_REPOS__HOOK_POST_REVPROP_CHANGE
870 "# POST-REVPROP-CHANGE HOOK" NL
872 "# The post-revprop-change hook is invoked after a revision property" NL
873 "# has been added, modified or deleted. Subversion runs this hook by" NL
874 "# invoking a program (script, executable, binary, etc.) named" NL
875 "# '"SCRIPT_NAME
"' (for which this file is a template), with the" NL
876 "# following ordered arguments:" NL
878 "# [1] REPOS-PATH (the path to this repository)" NL
879 "# [2] REV (the revision that was tweaked)" NL
880 "# [3] USER (the username of the person tweaking the property)" NL
881 "# [4] PROPNAME (the property that was changed)" NL
882 "# [5] ACTION (the property was 'A'dded, 'M'odified, or 'D'eleted)" NL
884 "# [STDIN] PROPVAL ** the old property value is passed via STDIN." NL
886 "# Because the propchange has already completed and cannot be undone," NL
887 "# the exit code of the hook program is ignored. The hook program" NL
888 "# can use the 'svnlook' utility to help it examine the" NL
889 "# new property value." NL
891 "# On a Unix system, the normal procedure is to have '"SCRIPT_NAME
"'" NL
892 "# invoke other programs to do the real work, though it may do the" NL
893 "# work itself too." NL
895 "# Note that '"SCRIPT_NAME
"' must be executable by the user(s) who will" NL
896 "# invoke it (typically the user httpd runs as), and that user must" NL
897 "# have filesystem-level permission to access the repository." NL
899 "# On a Windows system, you should name the hook program" NL
900 "# '"SCRIPT_NAME
".bat' or '"SCRIPT_NAME
".exe'," NL
901 "# but the basic idea is the same." NL
903 HOOKS_ENVIRONMENT_TEXT
905 "# Here is an example hook script, for a Unix /bin/sh interpreter." NL
906 PREWRITTEN_HOOKS_TEXT
915 "commit-email.pl --revprop-change \"$REPOS\" \"$REV\" \"$USER\" \"$PROPNAME\" "
916 "watchers@example.org" NL
;
920 SVN_ERR_W(svn_io_file_create(this_path
, contents
, pool
),
921 _("Creating post-revprop-change hook"));
922 } /* end post-revprop-change hook */
928 create_conf(svn_repos_t
*repos
, apr_pool_t
*pool
)
930 SVN_ERR_W(create_repos_dir(repos
->conf_path
, pool
),
931 _("Creating conf directory"));
933 /* Write a default template for svnserve.conf. */
935 static const char * const svnserve_conf_contents
=
936 "### This file controls the configuration of the svnserve daemon, if you" NL
937 "### use it to allow access to this repository. (If you only allow" NL
938 "### access through http: and/or file: URLs, then this file is" NL
939 "### irrelevant.)" NL
941 "### Visit http://subversion.tigris.org/ for more information." NL
944 "### These options control access to the repository for unauthenticated" NL
945 "### and authenticated users. Valid values are \"write\", \"read\"," NL
946 "### and \"none\". The sample settings below are the defaults." NL
947 "# anon-access = read" NL
948 "# auth-access = write" NL
949 "### The password-db option controls the location of the password" NL
950 "### database file. Unless you specify a path starting with a /," NL
951 "### the file's location is relative to the directory containing" NL
952 "### this configuration file." NL
954 "### If use-sasl is set to \"true\" below, this file will NOT be used." NL
956 "### Uncomment the line below to use the default password file." NL
957 "# password-db = passwd" NL
958 "### The authz-db option controls the location of the authorization" NL
959 "### rules for path-based access control. Unless you specify a path" NL
960 "### starting with a /, the file's location is relative to the the" NL
961 "### directory containing this file. If you don't specify an" NL
962 "### authz-db, no path-based access control is done." NL
963 "### Uncomment the line below to use the default authorization file." NL
964 "# authz-db = " SVN_REPOS__CONF_AUTHZ NL
965 "### This option specifies the authentication realm of the repository." NL
966 "### If two repositories have the same authentication realm, they should" NL
967 "### have the same password database, and vice versa. The default realm" NL
968 "### is repository's uuid." NL
969 #ifndef SVN_HAVE_SASL
970 "# realm = My First Repository" NL
;
972 "# realm = My First Repository" NL
975 "### This option specifies whether you want to use the Cyrus SASL" NL
976 "### library for authentication. Default is false." NL
977 "# use-sasl = true" NL
978 "### These options specify the desired strength of the security layer" NL
979 "### that you want SASL to provide. 0 means no encryption, 1 means" NL
980 "### integrity-checking only, values larger than 1 are correlated" NL
981 "### to the effective key length for encryption (e.g. 128 means 128-bit" NL
982 "### encryption). The values below are the defaults." NL
983 "# min-encryption = 0" NL
984 "# max-encryption = 256" NL
;
987 SVN_ERR_W(svn_io_file_create(svn_repos_svnserve_conf(repos
, pool
),
988 svnserve_conf_contents
, pool
),
989 _("Creating svnserve.conf file"));
993 static const char * const passwd_contents
=
994 "### This file is an example password file for svnserve." NL
995 "### Its format is similar to that of svnserve.conf. As shown in the" NL
996 "### example below it contains one section labelled [users]." NL
997 "### The name and password for each user follow, one account per line." NL
1000 "# harry = harryssecret" NL
1001 "# sally = sallyssecret" NL
;
1003 SVN_ERR_W(svn_io_file_create(svn_path_join(repos
->conf_path
,
1004 SVN_REPOS__CONF_PASSWD
,
1006 passwd_contents
, pool
),
1007 _("Creating passwd file"));
1011 static const char * const authz_contents
=
1012 "### This file is an example authorization file for svnserve." NL
1013 "### Its format is identical to that of mod_authz_svn authorization" NL
1015 "### As shown below each section defines authorizations for the path and" NL
1016 "### (optional) repository specified by the section name." NL
1017 "### The authorizations follow. An authorization line can refer to:" NL
1018 "### - a single user," NL
1019 "### - a group of users defined in a special [groups] section," NL
1020 "### - an alias defined in a special [aliases] section," NL
1021 "### - all authenticated users, using the '$authenticated' token," NL
1022 "### - only anonymous users, using the '$anonymous' token," NL
1023 "### - anyone, using the '*' wildcard." NL
1025 "### A match can be inverted by prefixing the rule with '~'. Rules can" NL
1026 "### grant read ('r') access, read-write ('rw') access, or no access" NL
1030 "# joe = /C=XZ/ST=Dessert/L=Snake City/O=Snake Oil, Ltd./OU=Research Institute/CN=Joe Average" NL
1033 "# harry_and_sally = harry,sally" NL
1034 "# harry_sally_and_joe = harry,sally,&joe" NL
1041 "# [repository:/baz/fuz]" NL
1042 "# @harry_and_sally = rw" NL
1045 SVN_ERR_W(svn_io_file_create(svn_path_join(repos
->conf_path
,
1046 SVN_REPOS__CONF_AUTHZ
,
1048 authz_contents
, pool
),
1049 _("Creating authz file"));
1052 return SVN_NO_ERROR
;
1055 /* Allocate and return a new svn_repos_t * object, initializing the
1056 directory pathname members based on PATH.
1057 The members FS, FORMAT, and FS_TYPE are *not* initialized (they are null),
1058 and it is the caller's responsibility to fill them in if needed. */
1059 static svn_repos_t
*
1060 create_svn_repos_t(const char *path
, apr_pool_t
*pool
)
1062 svn_repos_t
*repos
= apr_pcalloc(pool
, sizeof(*repos
));
1064 repos
->path
= apr_pstrdup(pool
, path
);
1065 repos
->db_path
= svn_path_join(path
, SVN_REPOS__DB_DIR
, pool
);
1066 repos
->conf_path
= svn_path_join(path
, SVN_REPOS__CONF_DIR
, pool
);
1067 repos
->hook_path
= svn_path_join(path
, SVN_REPOS__HOOK_DIR
, pool
);
1068 repos
->lock_path
= svn_path_join(path
, SVN_REPOS__LOCK_DIR
, pool
);
1074 static svn_error_t
*
1075 create_repos_structure(svn_repos_t
*repos
,
1077 apr_hash_t
*fs_config
,
1080 /* Create the top-level repository directory. */
1081 SVN_ERR_W(create_repos_dir(path
, pool
),
1082 _("Could not create top-level directory"));
1084 /* Create the DAV sandbox directory if pre-1.4 or pre-1.5-compatible. */
1086 && (apr_hash_get(fs_config
, SVN_FS_CONFIG_PRE_1_4_COMPATIBLE
,
1087 APR_HASH_KEY_STRING
)
1088 || apr_hash_get(fs_config
, SVN_FS_CONFIG_PRE_1_5_COMPATIBLE
,
1089 APR_HASH_KEY_STRING
)))
1091 const char *dav_path
= svn_path_join(repos
->path
,
1092 SVN_REPOS__DAV_DIR
, pool
);
1093 SVN_ERR_W(create_repos_dir(dav_path
, pool
),
1094 _("Creating DAV sandbox dir"));
1097 /* Create the lock directory. */
1098 SVN_ERR(create_locks(repos
, pool
));
1100 /* Create the hooks directory. */
1101 SVN_ERR(create_hooks(repos
, pool
));
1103 /* Create the conf directory. */
1104 SVN_ERR(create_conf(repos
, pool
));
1106 /* Write the top-level README file. */
1108 const char * const readme_header
=
1109 "This is a Subversion repository; use the 'svnadmin' tool to examine" NL
1110 "it. Do not add, delete, or modify files here unless you know how" NL
1111 "to avoid corrupting the repository." NL
1113 const char * const readme_bdb_insert
=
1114 "The directory \"" SVN_REPOS__DB_DIR
"\" contains a Berkeley DB environment." NL
1115 "you may need to tweak the values in \"" SVN_REPOS__DB_DIR
"/DB_CONFIG\" to match the" NL
1116 "requirements of your site." NL
1118 const char * const readme_footer
=
1119 "Visit http://subversion.tigris.org/ for more information." NL
;
1123 SVN_ERR(svn_io_file_open(&f
,
1124 svn_path_join(path
, SVN_REPOS__README
, pool
),
1125 (APR_WRITE
| APR_CREATE
| APR_EXCL
),
1126 APR_OS_DEFAULT
, pool
));
1128 SVN_ERR(svn_io_file_write_full(f
, readme_header
, strlen(readme_header
),
1130 if (strcmp(repos
->fs_type
, SVN_FS_TYPE_BDB
) == 0)
1131 SVN_ERR(svn_io_file_write_full(f
, readme_bdb_insert
,
1132 strlen(readme_bdb_insert
),
1134 SVN_ERR(svn_io_file_write_full(f
, readme_footer
, strlen(readme_footer
),
1137 SVN_ERR(svn_io_file_close(f
, pool
));
1140 return SVN_NO_ERROR
;
1144 /* There is, at present, nothing within the direct responsibility
1145 of libsvn_repos which requires locking. For historical compatibility
1146 reasons, the BDB libsvn_fs backend does not do its own locking, expecting
1147 libsvn_repos to do the locking for it. Here we take care of that
1148 backend-specific requirement.
1149 The kind of lock is controlled by EXCLUSIVE and NONBLOCKING.
1150 The lock is scoped to POOL. */
1151 static svn_error_t
*
1152 lock_repos(svn_repos_t
*repos
,
1153 svn_boolean_t exclusive
,
1154 svn_boolean_t nonblocking
,
1157 if (strcmp(repos
->fs_type
, SVN_FS_TYPE_BDB
) == 0)
1160 const char *lockfile_path
= svn_repos_db_lockfile(repos
, pool
);
1162 err
= svn_io_file_lock2(lockfile_path
, exclusive
, nonblocking
, pool
);
1163 if (err
!= NULL
&& APR_STATUS_IS_EAGAIN(err
->apr_err
))
1165 SVN_ERR_W(err
, _("Error opening db lockfile"));
1167 return SVN_NO_ERROR
;
1172 svn_repos_create(svn_repos_t
**repos_p
,
1174 const char *unused_1
,
1175 const char *unused_2
,
1177 apr_hash_t
*fs_config
,
1183 /* Allocate a repository object, filling in the format we will create. */
1184 repos
= create_svn_repos_t(path
, pool
);
1185 repos
->format
= SVN_REPOS__FORMAT_NUMBER
;
1187 /* Discover the type of the filesystem we are about to create. */
1190 repos
->fs_type
= apr_hash_get(fs_config
, SVN_FS_CONFIG_FS_TYPE
,
1191 APR_HASH_KEY_STRING
);
1192 if (apr_hash_get(fs_config
, SVN_FS_CONFIG_PRE_1_4_COMPATIBLE
,
1193 APR_HASH_KEY_STRING
))
1194 repos
->format
= SVN_REPOS__FORMAT_NUMBER_LEGACY
;
1197 if (! repos
->fs_type
)
1198 repos
->fs_type
= DEFAULT_FS_TYPE
;
1200 /* Create the various files and subdirectories for the repository. */
1201 SVN_ERR_W(create_repos_structure(repos
, path
, fs_config
, pool
),
1202 _("Repository creation failed"));
1204 /* Lock if needed. */
1205 SVN_ERR(lock_repos(repos
, FALSE
, FALSE
, pool
));
1207 /* Create an environment for the filesystem. */
1208 if ((err
= svn_fs_create(&repos
->fs
, repos
->db_path
, fs_config
, pool
)))
1210 /* If there was an error making the filesytem, e.g. unknown/supported
1211 * filesystem type. Clean up after ourselves. Yes this is safe because
1212 * create_repos_structure will fail if the path existed before we started
1213 * so we can't accidentally remove a directory that previously existed. */
1214 svn_error_clear(svn_io_remove_dir2(path
, FALSE
, NULL
, NULL
, pool
));
1218 /* This repository is ready. Stamp it with a format number. */
1219 SVN_ERR(svn_io_write_version_file
1220 (svn_path_join(path
, SVN_REPOS__FORMAT
, pool
),
1221 repos
->format
, pool
));
1224 return SVN_NO_ERROR
;
1228 /* Check if @a path is the root of a repository by checking if the
1229 * path contains the expected files and directories. Return TRUE
1230 * on errors (which would be permission errors, probably) so that
1231 * we the user will see them after we try to open the repository
1233 static svn_boolean_t
1234 check_repos_path(const char *path
,
1237 svn_node_kind_t kind
;
1240 err
= svn_io_check_path(svn_path_join(path
, SVN_REPOS__FORMAT
, pool
),
1244 svn_error_clear(err
);
1247 if (kind
!= svn_node_file
)
1250 /* Check the db/ subdir, but allow it to be a symlink (Subversion
1251 works just fine if it's a symlink). */
1252 err
= svn_io_check_resolved_path
1253 (svn_path_join(path
, SVN_REPOS__DB_DIR
, pool
), &kind
, pool
);
1256 svn_error_clear(err
);
1259 if (kind
!= svn_node_dir
)
1266 /* Verify that REPOS's format is suitable.
1267 Use POOL for temporary allocation. */
1268 static svn_error_t
*
1269 check_repos_format(svn_repos_t
*repos
,
1273 const char *format_path
;
1275 format_path
= svn_path_join(repos
->path
, SVN_REPOS__FORMAT
, pool
);
1276 SVN_ERR(svn_io_read_version_file(&format
, format_path
, pool
));
1278 if (format
!= SVN_REPOS__FORMAT_NUMBER
&&
1279 format
!= SVN_REPOS__FORMAT_NUMBER_LEGACY
)
1281 return svn_error_createf
1282 (SVN_ERR_REPOS_UNSUPPORTED_VERSION
, NULL
,
1283 _("Expected repository format '%d' or '%d'; found format '%d'"),
1284 SVN_REPOS__FORMAT_NUMBER_LEGACY
, SVN_REPOS__FORMAT_NUMBER
,
1288 repos
->format
= format
;
1290 return SVN_NO_ERROR
;
1294 /* Set *REPOS_P to a repository at PATH which has been opened.
1295 See lock_repos() above regarding EXCLUSIVE and NONBLOCKING.
1296 OPEN_FS indicates whether the Subversion filesystem should be opened,
1297 the handle being placed into repos->fs.
1298 Do all allocation in POOL. */
1299 static svn_error_t
*
1300 get_repos(svn_repos_t
**repos_p
,
1302 svn_boolean_t exclusive
,
1303 svn_boolean_t nonblocking
,
1304 svn_boolean_t open_fs
,
1309 /* Allocate a repository object. */
1310 repos
= create_svn_repos_t(path
, pool
);
1312 /* Verify the validity of our repository format. */
1313 SVN_ERR(check_repos_format(repos
, pool
));
1315 /* Discover the FS type. */
1316 SVN_ERR(svn_fs_type(&repos
->fs_type
, repos
->db_path
, pool
));
1318 /* Lock if needed. */
1319 SVN_ERR(lock_repos(repos
, exclusive
, nonblocking
, pool
));
1321 /* Open up the filesystem only after obtaining the lock. */
1323 SVN_ERR(svn_fs_open(&repos
->fs
, repos
->db_path
, NULL
, pool
));
1326 return SVN_NO_ERROR
;
1332 svn_repos_find_root_path(const char *path
,
1335 const char *candidate
= path
;
1336 const char *decoded
;
1341 /* Try to decode the path, so we don't fail if it contains characters
1342 that aren't supported by the OS filesystem. The subversion fs
1343 isn't restricted by the OS filesystem character set. */
1344 err
= svn_utf_cstring_from_utf8(&decoded
, candidate
, pool
);
1345 if (!err
&& check_repos_path(candidate
, pool
))
1347 svn_error_clear(err
);
1349 if (candidate
[0] == '\0' ||
1350 svn_dirent_is_root(candidate
, strlen(candidate
)))
1353 candidate
= svn_path_dirname(candidate
, pool
);
1361 svn_repos_open(svn_repos_t
**repos_p
,
1365 /* Fetch a repository object initialized with a shared read/write
1366 lock on the database. */
1368 SVN_ERR(get_repos(repos_p
, path
, FALSE
, FALSE
, TRUE
, pool
));
1370 return SVN_NO_ERROR
;
1375 svn_repos_delete(const char *path
,
1378 const char *db_path
= svn_path_join(path
, SVN_REPOS__DB_DIR
, pool
);
1380 /* Delete the filesystem environment... */
1381 SVN_ERR(svn_fs_delete_fs(db_path
, pool
));
1383 /* ...then blow away everything else. */
1384 SVN_ERR(svn_io_remove_dir2(path
, FALSE
, NULL
, NULL
, pool
));
1386 return SVN_NO_ERROR
;
1391 svn_repos_fs(svn_repos_t
*repos
)
1399 /* For historical reasons, for the Berkeley DB backend, this code uses
1400 * repository locking, which is motivated by the need to support the
1401 * Berkeley DB error DB_RUN_RECOVERY. (FSFS takes care of locking
1402 * itself, inside its implementation of svn_fs_recover.) Here's how
1405 * Every accessor of a repository's database takes out a shared lock
1406 * on the repository -- both readers and writers get shared locks, and
1407 * there can be an unlimited number of shared locks simultaneously.
1409 * Sometimes, a db access returns the error DB_RUN_RECOVERY. When
1410 * this happens, we need to run svn_fs_recover() on the db
1411 * with no other accessors present. So we take out an exclusive lock
1412 * on the repository. From the moment we request the exclusive lock,
1413 * no more shared locks are granted, and when the last shared lock
1414 * disappears, the exclusive lock is granted. As soon as we get it,
1415 * we can run recovery.
1417 * We assume that once any berkeley call returns DB_RUN_RECOVERY, they
1418 * all do, until recovery is run.
1422 svn_repos_recover3(const char *path
,
1423 svn_boolean_t nonblocking
,
1424 svn_error_t
*(*start_callback
)(void *baton
),
1425 void *start_callback_baton
,
1426 svn_cancel_func_t cancel_func
, void *cancel_baton
,
1430 apr_pool_t
*subpool
= svn_pool_create(pool
);
1432 /* Fetch a repository object; for the Berkeley DB backend, it is
1433 initialized with an EXCLUSIVE lock on the database. This will at
1434 least prevent others from trying to read or write to it while we
1435 run recovery. (Other backends should do their own locking; see
1437 SVN_ERR(get_repos(&repos
, path
, TRUE
, nonblocking
,
1438 FALSE
, /* don't try to open the db yet. */
1442 SVN_ERR(start_callback(start_callback_baton
));
1444 /* Recover the database to a consistent state. */
1445 SVN_ERR(svn_fs_recover(repos
->db_path
, cancel_func
, cancel_baton
, subpool
));
1447 /* Close shop and free the subpool, to release the exclusive lock. */
1448 svn_pool_destroy(subpool
);
1450 return SVN_NO_ERROR
;
1455 svn_repos_recover2(const char *path
,
1456 svn_boolean_t nonblocking
,
1457 svn_error_t
*(*start_callback
)(void *baton
),
1458 void *start_callback_baton
,
1461 return svn_repos_recover3(path
, nonblocking
,
1462 start_callback
, start_callback_baton
,
1468 svn_repos_recover(const char *path
,
1471 return svn_repos_recover2(path
, FALSE
, NULL
, NULL
, pool
);
1474 svn_error_t
*svn_repos_db_logfiles(apr_array_header_t
**logfiles
,
1476 svn_boolean_t only_unused
,
1482 SVN_ERR(get_repos(&repos
, path
,
1484 FALSE
, /* Do not open fs. */
1487 SVN_ERR(svn_fs_berkeley_logfiles(logfiles
,
1488 svn_repos_db_env(repos
, pool
),
1492 /* Loop, printing log files. */
1493 for (i
= 0; i
< (*logfiles
)->nelts
; i
++)
1495 const char ** log_file
= &(APR_ARRAY_IDX(*logfiles
, i
, const char *));
1496 *log_file
= svn_path_join(SVN_REPOS__DB_DIR
, *log_file
, pool
);
1499 return SVN_NO_ERROR
;
1502 /** Hot copy structure copy context.
1504 struct hotcopy_ctx_t
{
1505 const char *dest
; /* target location to construct */
1506 unsigned int src_len
; /* len of the source path*/
1509 /** Called by (svn_io_dir_walk).
1510 * Copies the repository structure with exception of @c SVN_REPOS__DB_DIR,
1511 * @c SVN_REPOS__LOCK_DIR and @c SVN_REPOS__FORMAT.
1512 * Those directories and files are handled separetly.
1513 * @a baton is a pointer to (struct hotcopy_ctx_t) specifying
1514 * destination path to copy to and the length of the source path.
1516 * @copydoc svn_io_dir_walk()
1518 static svn_error_t
*hotcopy_structure(void *baton
,
1520 const apr_finfo_t
*finfo
,
1523 const struct hotcopy_ctx_t
*ctx
= ((struct hotcopy_ctx_t
*) baton
);
1524 const char *sub_path
;
1527 if (strlen(path
) == ctx
->src_len
)
1533 sub_path
= &path
[ctx
->src_len
+1];
1535 /* Check if we are inside db directory and if so skip it */
1536 if (svn_path_compare_paths
1537 (svn_path_get_longest_ancestor(SVN_REPOS__DB_DIR
, sub_path
, pool
),
1538 SVN_REPOS__DB_DIR
) == 0)
1539 return SVN_NO_ERROR
;
1541 if (svn_path_compare_paths
1542 (svn_path_get_longest_ancestor(SVN_REPOS__LOCK_DIR
, sub_path
, pool
),
1543 SVN_REPOS__LOCK_DIR
) == 0)
1544 return SVN_NO_ERROR
;
1546 if (svn_path_compare_paths
1547 (svn_path_get_longest_ancestor(SVN_REPOS__FORMAT
, sub_path
, pool
),
1548 SVN_REPOS__FORMAT
) == 0)
1549 return SVN_NO_ERROR
;
1552 target
= svn_path_join(ctx
->dest
, sub_path
, pool
);
1554 if (finfo
->filetype
== APR_DIR
)
1556 SVN_ERR(create_repos_dir(target
, pool
));
1558 else if (finfo
->filetype
== APR_REG
)
1561 SVN_ERR(svn_io_copy_file(path
, target
, TRUE
, pool
));
1564 return SVN_NO_ERROR
;
1568 /** Obtain a lock on db logs lock file. Create one if it does not exist.
1570 static svn_error_t
*
1571 lock_db_logs_file(svn_repos_t
*repos
,
1572 svn_boolean_t exclusive
,
1575 const char * lock_file
= svn_repos_db_logs_lockfile(repos
, pool
);
1577 /* Try to create a lock file, in case if it is missing. As in case of the
1578 repositories created before hotcopy functionality. */
1579 svn_error_clear(create_db_logs_lock(repos
, pool
));
1581 SVN_ERR(svn_io_file_lock2(lock_file
, exclusive
, FALSE
, pool
));
1583 return SVN_NO_ERROR
;
1587 /* Make a copy of a repository with hot backup of fs. */
1589 svn_repos_hotcopy(const char *src_path
,
1590 const char *dst_path
,
1591 svn_boolean_t clean_logs
,
1594 svn_repos_t
*src_repos
;
1595 svn_repos_t
*dst_repos
;
1596 struct hotcopy_ctx_t hotcopy_context
;
1598 /* Try to open original repository */
1599 SVN_ERR(get_repos(&src_repos
, src_path
,
1601 FALSE
, /* don't try to open the db yet. */
1604 /* If we are going to clean logs, then get an exclusive lock on
1605 db-logs.lock, to ensure that no one else will work with logs.
1607 If we are just copying, then get a shared lock to ensure that
1608 no one else will clean logs while we copying them */
1610 SVN_ERR(lock_db_logs_file(src_repos
, clean_logs
, pool
));
1612 /* Copy the repository to a new path, with exception of
1613 specially handled directories */
1615 hotcopy_context
.dest
= dst_path
;
1616 hotcopy_context
.src_len
= strlen(src_path
);
1617 SVN_ERR(svn_io_dir_walk(src_path
,
1623 /* Prepare dst_repos object so that we may create locks,
1624 so that we may open repository */
1626 dst_repos
= create_svn_repos_t(dst_path
, pool
);
1627 dst_repos
->fs_type
= src_repos
->fs_type
;
1628 dst_repos
->format
= src_repos
->format
;
1630 SVN_ERR(create_locks(dst_repos
, pool
));
1632 SVN_ERR(svn_io_dir_make_sgid(dst_repos
->db_path
, APR_OS_DEFAULT
, pool
));
1634 /* Exclusively lock the new repository.
1635 No one should be accessing it at the moment */
1636 SVN_ERR(lock_repos(dst_repos
, TRUE
, FALSE
, pool
));
1638 SVN_ERR(svn_fs_hotcopy(src_repos
->db_path
, dst_repos
->db_path
,
1641 /* Destination repository is ready. Stamp it with a format number. */
1642 SVN_ERR(svn_io_write_version_file
1643 (svn_path_join(dst_repos
->path
, SVN_REPOS__FORMAT
, pool
),
1644 dst_repos
->format
, pool
));
1646 return SVN_NO_ERROR
;
1649 /* Return the library version number. */
1650 const svn_version_t
*
1651 svn_repos_version(void)
1659 svn_repos_stat(svn_dirent_t
**dirent
,
1660 svn_fs_root_t
*root
,
1664 svn_node_kind_t kind
;
1666 const char *datestring
;
1667 apr_hash_t
*prophash
;
1669 SVN_ERR(svn_fs_check_path(&kind
, root
, path
, pool
));
1671 if (kind
== svn_node_none
)
1674 return SVN_NO_ERROR
;
1677 ent
= apr_pcalloc(pool
, sizeof(*ent
));
1680 if (kind
== svn_node_file
)
1681 SVN_ERR(svn_fs_file_length(&(ent
->size
), root
, path
, pool
));
1683 SVN_ERR(svn_fs_node_proplist(&prophash
, root
, path
, pool
));
1684 if (apr_hash_count(prophash
) > 0)
1685 ent
->has_props
= TRUE
;
1687 SVN_ERR(svn_repos_get_committed_info(&(ent
->created_rev
),
1689 &(ent
->last_author
),
1692 SVN_ERR(svn_time_from_cstring(&(ent
->time
), datestring
, pool
));
1695 return SVN_NO_ERROR
;
1699 svn_repos_remember_client_capabilities(svn_repos_t
*repos
,
1700 apr_array_header_t
*capabilities
)
1702 repos
->client_capabilities
= capabilities
;
1703 return SVN_NO_ERROR
;