Fix compiler warning due to missing function prototype.
[svn.git] / subversion / libsvn_repos / repos.c
blobc1d913a40850b701d5ffa5b2e1315f49d123eff5
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 * ====================================================================
18 #include <assert.h>
20 #include <apr_pools.h>
21 #include <apr_file_io.h>
23 #include "svn_pools.h"
24 #include "svn_error.h"
25 #include "svn_path.h"
26 #include "svn_utf.h"
27 #include "svn_time.h"
28 #include "svn_fs.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 */
33 #include "repos.h"
35 /* Used to terminate lines in large multi-line string literals. */
36 #define NL APR_EOL_STR
39 /* Path accessor functions. */
42 const char *
43 svn_repos_path(svn_repos_t *repos, apr_pool_t *pool)
45 return apr_pstrdup(pool, repos->path);
49 const char *
50 svn_repos_db_env(svn_repos_t *repos, apr_pool_t *pool)
52 return apr_pstrdup(pool, repos->db_path);
56 const char *
57 svn_repos_conf_dir(svn_repos_t *repos, apr_pool_t *pool)
59 return apr_pstrdup(pool, repos->conf_path);
63 const char *
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);
70 const char *
71 svn_repos_lock_dir(svn_repos_t *repos, apr_pool_t *pool)
73 return apr_pstrdup(pool, repos->lock_path);
77 const char *
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);
84 const char *
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);
90 const char *
91 svn_repos_hook_dir(svn_repos_t *repos, apr_pool_t *pool)
93 return apr_pstrdup(pool, repos->hook_path);
97 const char *
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);
104 const char *
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);
111 const char *
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);
118 const char *
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);
124 const char *
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);
131 const char *
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);
138 const char *
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);
145 const char *
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,
149 pool);
153 const char *
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,
157 pool);
161 static svn_error_t *
162 create_repos_dir(const char *path, apr_pool_t *pool)
164 svn_error_t *err;
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));
175 if (is_empty)
176 err = NULL;
177 else
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));
183 return err;
186 static const char * bdb_lock_file_contents =
187 "DB lock file, representing locks on the versioned filesystem." NL
188 "" 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
195 "" 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
200 "" 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
204 "" 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. */
213 static svn_error_t *
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;
221 else
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"));
227 return SVN_NO_ERROR;
230 /* Create the DB lockfile. */
231 static svn_error_t *
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;
239 else
240 contents = pre12_compat_unneeded_file_contents;
242 SVN_ERR_W(svn_io_file_create(lockfile_path, contents, pool),
243 _("Creating db lock file"));
245 return SVN_NO_ERROR;
248 static svn_error_t *
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));
258 return SVN_NO_ERROR;
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
277 static svn_error_t *
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
296 contents =
297 "#!/bin/sh" NL
298 "" NL
299 "# START-COMMIT HOOK" NL
300 "#" 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
306 "#" 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
311 "#" 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
318 "#" 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
322 "#" 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
325 "#" 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
329 "#" 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
333 "#" 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
337 "#" 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
341 "# " NL
342 HOOKS_ENVIRONMENT_TEXT
343 "# " NL
344 "# Here is an example hook script, for a Unix /bin/sh interpreter." NL
345 PREWRITTEN_HOOKS_TEXT
346 "" NL
347 "" NL
348 "REPOS=\"$1\"" NL
349 "USER=\"$2\"" NL
350 "" NL
351 "commit-allower.pl --repository \"$REPOS\" --user \"$USER\" || exit 1" NL
352 "special-auth-check.py --user \"$USER\" --auth-level 3 || exit 1" NL
353 "" NL
354 "# All checks passed, so allow the commit." NL
355 "exit 0" NL;
357 #undef SCRIPT_NAME
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
371 contents =
372 "#!/bin/sh" NL
373 "" NL
374 "# PRE-COMMIT HOOK" NL
375 "#" 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
380 "#" 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
383 "#" NL
384 "# The default working directory for the invocation is undefined, so" NL
385 "# the program should set one explicitly if it cares." NL
386 "#" 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
391 "#" 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
395 "#" NL
396 "# *** NOTE: THE HOOK PROGRAM MUST NOT MODIFY THE TXN, EXCEPT ***" NL
397 "# *** FOR REVISION PROPERTIES (like svn:log or svn:author). ***" NL
398 "#" 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
405 "#" 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
409 "#" 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
413 "#" NL
414 HOOKS_ENVIRONMENT_TEXT
415 "# " NL
416 "# Here is an example hook script, for a Unix /bin/sh interpreter." NL
417 PREWRITTEN_HOOKS_TEXT
418 "" NL
419 "" NL
420 "REPOS=\"$1\"" NL
421 "TXN=\"$2\"" NL
422 "" NL
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
427 "" 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"
432 "" NL
433 "# All checks passed, so allow the commit." NL
434 "exit 0" NL;
436 #undef SCRIPT_NAME
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
451 contents =
452 "#!/bin/sh" NL
453 "" NL
454 "# PRE-REVPROP-CHANGE HOOK" NL
455 "#" 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
460 "# arguments:" NL
461 "#" 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)"
468 "#" NL
469 "# [STDIN] PROPVAL ** the new property value is passed via STDIN." NL
470 "#" 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
475 "#" 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
482 "#" 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
486 "#" 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
490 "#" 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
494 "#" NL
495 HOOKS_ENVIRONMENT_TEXT
496 "# " NL
497 "# Here is an example hook script, for a Unix /bin/sh interpreter." NL
498 PREWRITTEN_HOOKS_TEXT
499 "" NL
500 "" NL
501 "REPOS=\"$1\"" NL
502 "REV=\"$2\"" NL
503 "USER=\"$3\"" NL
504 "PROPNAME=\"$4\"" NL
505 "ACTION=\"$5\"" NL
506 "" NL
507 "if [ \"$ACTION\" = \"M\" -a \"$PROPNAME\" = \"svn:log\" ]; then exit 0; fi" NL
508 "" NL
509 "echo \"Changing revision properties other than svn:log is prohibited\" >&2" NL
510 "exit 1" NL;
512 #undef SCRIPT_NAME
514 SVN_ERR_W(svn_io_file_create(this_path, contents, pool),
515 _("Creating pre-revprop-change hook"));
516 } /* end pre-revprop-change hook */
519 /* Pre-lock 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
527 contents =
528 "#!/bin/sh" NL
529 "" NL
530 "# PRE-LOCK HOOK" NL
531 "#" NL
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
536 "#" 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
540 "#" NL
541 "# The default working directory for the invocation is undefined, so" NL
542 "# the program should set one explicitly if it cares." NL
543 "#" 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
547 "" 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
551 "#" 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
555 "#" 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
559 "#" NL
560 "# Here is an example hook script, for a Unix /bin/sh interpreter:" NL
561 "" NL
562 "REPOS=\"$1\"" NL
563 "PATH=\"$2\"" NL
564 "USER=\"$3\"" NL
565 "" 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
568 "" NL
569 "# (Maybe this script could send email to the lock owner?)" NL
570 "SVNLOOK=" SVN_BINDIR "/svnlook" NL
571 "GREP=/bin/grep" NL
572 "SED=/bin/sed" NL
573 "" NL
574 "LOCK_OWNER=`$SVNLOOK lock \"$REPOS\" \"$PATH\" | \\" NL
575 " $GREP '^Owner: ' | $SED 's/Owner: //'`" NL
576 "" NL
577 "# If we get no result from svnlook, there's no lock, allow the lock to" NL
578 "# happen:" NL
579 "if [ \"$LOCK_OWNER\" = \"\" ]; then" NL
580 " exit 0" NL
581 "fi" NL
582 "" NL
583 "# If the person locking matches the lock's owner, allow the lock to" NL
584 "# happen:" NL
585 "if [ \"$LOCK_OWNER\" = \"$USER\" ]; then" NL
586 " exit 0" NL
587 "fi" NL
588 "" 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
591 "exit 1" NL;
593 #undef SCRIPT_NAME
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
608 contents =
609 "#!/bin/sh" NL
610 "" NL
611 "# PRE-UNLOCK HOOK" NL
612 "#" 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
617 "#" 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
621 "#" NL
622 "# The default working directory for the invocation is undefined, so" NL
623 "# the program should set one explicitly if it cares." NL
624 "#" 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
628 "" 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
632 "#" 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
636 "#" 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
640 "#" NL
641 "# Here is an example hook script, for a Unix /bin/sh interpreter:" NL
642 "" NL
643 "REPOS=\"$1\"" NL
644 "PATH=\"$2\"" NL
645 "USER=\"$3\"" NL
646 "" 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
649 "" NL
650 "SVNLOOK=" SVN_BINDIR "/svnlook" NL
651 "GREP=/bin/grep" NL
652 "SED=/bin/sed" NL
653 "" NL
654 "LOCK_OWNER=`$SVNLOOK lock \"$REPOS\" \"$PATH\" | \\" NL
655 " $GREP '^Owner: ' | $SED 's/Owner: //'`" NL
656 "" NL
657 "# If we get no result from svnlook, there's no lock, return success:" NL
658 "if [ \"$LOCK_OWNER\" = \"\" ]; then" NL
659 " exit 0" NL
660 "fi" NL
661 "" NL
662 "# If the person unlocking matches the lock's owner, return success:" NL
663 "if [ \"$LOCK_OWNER\" = \"$USER\" ]; then" NL
664 " exit 0" NL
665 "fi" NL
666 "" NL
667 "# Otherwise, we've got an owner mismatch, so return failure:" NL
668 "echo \"Error: $PATH locked by ${LOCK_OWNER}.\" 1>&2" NL
669 "exit 1" NL;
671 #undef SCRIPT_NAME
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
687 contents =
688 "#!/bin/sh" NL
689 "" NL
690 "# POST-COMMIT HOOK" NL
691 "#" 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
696 "#" NL
697 "# [1] REPOS-PATH (the path to this repository)" NL
698 "# [2] REV (the number of the revision just committed)" NL
699 "#" NL
700 "# The default working directory for the invocation is undefined, so" NL
701 "# the program should set one explicitly if it cares." NL
702 "#" 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
707 "#" 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
711 "#" 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
715 "#" 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
719 "# " NL
720 HOOKS_ENVIRONMENT_TEXT
721 "# " NL
722 "# Here is an example hook script, for a Unix /bin/sh interpreter." NL
723 PREWRITTEN_HOOKS_TEXT
724 "" NL
725 "" NL
726 "REPOS=\"$1\"" NL
727 "REV=\"$2\"" NL
729 "commit-email.pl \"$REPOS\" \"$REV\" commit-watchers@example.org" NL
730 "log-commit.py --repository \"$REPOS\" --revision \"$REV\"" NL;
732 #undef SCRIPT_NAME
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
747 contents =
748 "#!/bin/sh" NL
749 "" NL
750 "# POST-LOCK HOOK" NL
751 "#" 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
756 "#" NL
757 "# [1] REPOS-PATH (the path to this repository)" NL
758 "# [2] USER (the user who created the lock)" NL
759 "#" 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
764 "#" NL
765 "# The default working directory for the invocation is undefined, so" NL
766 "# the program should set one explicitly if it cares." NL
767 "#" 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
772 "#" 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
776 "#" 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
780 "#" 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
784 "# " NL
785 "# Here is an example hook script, for a Unix /bin/sh interpreter:" NL
786 "" NL
787 "REPOS=\"$1\"" NL
788 "USER=\"$2\"" NL
789 "" 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;
793 #undef SCRIPT_NAME
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
808 contents =
809 "#!/bin/sh" NL
810 "" NL
811 "# POST-UNLOCK HOOK" NL
812 "#" 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
817 "#" NL
818 "# [1] REPOS-PATH (the path to this repository)" NL
819 "# [2] USER (the user who destroyed the lock)" NL
820 "#" 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
825 "#" NL
826 "# The default working directory for the invocation is undefined, so" NL
827 "# the program should set one explicitly if it cares." NL
828 "#" 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
831 "#" 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
835 "#" 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
839 "#" 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
843 "# " NL
844 "# Here is an example hook script, for a Unix /bin/sh interpreter:" NL
845 "" NL
846 "REPOS=\"$1\"" NL
847 "USER=\"$2\"" NL
848 "" 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;
852 #undef SCRIPT_NAME
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
867 contents =
868 "#!/bin/sh" NL
869 "" NL
870 "# POST-REVPROP-CHANGE HOOK" NL
871 "#" 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
877 "#" 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
883 "#" NL
884 "# [STDIN] PROPVAL ** the old property value is passed via STDIN." NL
885 "#" 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
890 "#" 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
894 "#" 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
898 "#" 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
902 "# " NL
903 HOOKS_ENVIRONMENT_TEXT
904 "# " NL
905 "# Here is an example hook script, for a Unix /bin/sh interpreter." NL
906 PREWRITTEN_HOOKS_TEXT
907 "" NL
908 "" NL
909 "REPOS=\"$1\"" NL
910 "REV=\"$2\"" NL
911 "USER=\"$3\"" NL
912 "PROPNAME=\"$4\"" NL
913 "ACTION=\"$5\"" NL
914 "" NL
915 "commit-email.pl --revprop-change \"$REPOS\" \"$REV\" \"$USER\" \"$PROPNAME\" "
916 "watchers@example.org" NL;
918 #undef SCRIPT_NAME
920 SVN_ERR_W(svn_io_file_create(this_path, contents, pool),
921 _("Creating post-revprop-change hook"));
922 } /* end post-revprop-change hook */
924 return SVN_NO_ERROR;
927 static svn_error_t *
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
940 "" NL
941 "### Visit http://subversion.tigris.org/ for more information." NL
942 "" NL
943 "[general]" 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
953 "### If SASL is enabled (see below), this file will NOT be used." NL
954 "### Uncomment the line below to use the default password file." NL
955 "# password-db = passwd" NL
956 "### The authz-db option controls the location of the authorization" NL
957 "### rules for path-based access control. Unless you specify a path" NL
958 "### starting with a /, the file's location is relative to the the" NL
959 "### directory containing this file. If you don't specify an" NL
960 "### authz-db, no path-based access control is done." NL
961 "### Uncomment the line below to use the default authorization file." NL
962 "# authz-db = " SVN_REPOS__CONF_AUTHZ NL
963 "### This option specifies the authentication realm of the repository." NL
964 "### If two repositories have the same authentication realm, they should" NL
965 "### have the same password database, and vice versa. The default realm" NL
966 "### is repository's uuid." NL
967 "# realm = My First Repository" NL
968 "" NL
969 "[sasl]" NL
970 "### This option specifies whether you want to use the Cyrus SASL" NL
971 "### library for authentication. Default is false." NL
972 "### This section will be ignored if svnserve is not built with Cyrus" NL
973 "### SASL support; to check, run 'svnserve --version' and look for a line" NL
974 "### reading 'Cyrus SASL authentication is available.'" NL
975 "# use-sasl = true" NL
976 "### These options specify the desired strength of the security layer" NL
977 "### that you want SASL to provide. 0 means no encryption, 1 means" NL
978 "### integrity-checking only, values larger than 1 are correlated" NL
979 "### to the effective key length for encryption (e.g. 128 means 128-bit" NL
980 "### encryption). The values below are the defaults." NL
981 "# min-encryption = 0" NL
982 "# max-encryption = 256" NL;
984 SVN_ERR_W(svn_io_file_create(svn_repos_svnserve_conf(repos, pool),
985 svnserve_conf_contents, pool),
986 _("Creating svnserve.conf file"));
990 static const char * const passwd_contents =
991 "### This file is an example password file for svnserve." NL
992 "### Its format is similar to that of svnserve.conf. As shown in the" NL
993 "### example below it contains one section labelled [users]." NL
994 "### The name and password for each user follow, one account per line." NL
995 "" NL
996 "[users]" NL
997 "# harry = harryssecret" NL
998 "# sally = sallyssecret" NL;
1000 SVN_ERR_W(svn_io_file_create(svn_path_join(repos->conf_path,
1001 SVN_REPOS__CONF_PASSWD,
1002 pool),
1003 passwd_contents, pool),
1004 _("Creating passwd file"));
1008 static const char * const authz_contents =
1009 "### This file is an example authorization file for svnserve." NL
1010 "### Its format is identical to that of mod_authz_svn authorization" NL
1011 "### files." NL
1012 "### As shown below each section defines authorizations for the path and" NL
1013 "### (optional) repository specified by the section name." NL
1014 "### The authorizations follow. An authorization line can refer to:" NL
1015 "### - a single user," NL
1016 "### - a group of users defined in a special [groups] section," NL
1017 "### - an alias defined in a special [aliases] section," NL
1018 "### - all authenticated users, using the '$authenticated' token," NL
1019 "### - only anonymous users, using the '$anonymous' token," NL
1020 "### - anyone, using the '*' wildcard." NL
1021 "###" NL
1022 "### A match can be inverted by prefixing the rule with '~'. Rules can" NL
1023 "### grant read ('r') access, read-write ('rw') access, or no access" NL
1024 "### ('')." NL
1025 "" NL
1026 "[aliases]" NL
1027 "# joe = /C=XZ/ST=Dessert/L=Snake City/O=Snake Oil, Ltd./OU=Research Institute/CN=Joe Average" NL
1028 "" NL
1029 "[groups]" NL
1030 "# harry_and_sally = harry,sally" NL
1031 "# harry_sally_and_joe = harry,sally,&joe" NL
1032 "" NL
1033 "# [/foo/bar]" NL
1034 "# harry = rw" NL
1035 "# &joe = r" NL
1036 "# * =" NL
1037 "" NL
1038 "# [repository:/baz/fuz]" NL
1039 "# @harry_and_sally = rw" NL
1040 "# * = r" NL;
1042 SVN_ERR_W(svn_io_file_create(svn_path_join(repos->conf_path,
1043 SVN_REPOS__CONF_AUTHZ,
1044 pool),
1045 authz_contents, pool),
1046 _("Creating authz file"));
1049 return SVN_NO_ERROR;
1052 /* Allocate and return a new svn_repos_t * object, initializing the
1053 directory pathname members based on PATH, and initializing the
1054 REPOSITORY_CAPABILITIES member.
1055 The members FS, FORMAT, and FS_TYPE are *not* initialized (they are null),
1056 and it is the caller's responsibility to fill them in if needed. */
1057 static svn_repos_t *
1058 create_svn_repos_t(const char *path, apr_pool_t *pool)
1060 svn_repos_t *repos = apr_pcalloc(pool, sizeof(*repos));
1062 repos->path = apr_pstrdup(pool, path);
1063 repos->db_path = svn_path_join(path, SVN_REPOS__DB_DIR, pool);
1064 repos->conf_path = svn_path_join(path, SVN_REPOS__CONF_DIR, pool);
1065 repos->hook_path = svn_path_join(path, SVN_REPOS__HOOK_DIR, pool);
1066 repos->lock_path = svn_path_join(path, SVN_REPOS__LOCK_DIR, pool);
1067 repos->repository_capabilities = apr_hash_make(pool);
1069 return repos;
1073 static svn_error_t *
1074 create_repos_structure(svn_repos_t *repos,
1075 const char *path,
1076 apr_hash_t *fs_config,
1077 apr_pool_t *pool)
1079 /* Create the top-level repository directory. */
1080 SVN_ERR_W(create_repos_dir(path, pool),
1081 _("Could not create top-level directory"));
1083 /* Create the DAV sandbox directory if pre-1.4 or pre-1.5-compatible. */
1084 if (fs_config
1085 && (apr_hash_get(fs_config, SVN_FS_CONFIG_PRE_1_4_COMPATIBLE,
1086 APR_HASH_KEY_STRING)
1087 || apr_hash_get(fs_config, SVN_FS_CONFIG_PRE_1_5_COMPATIBLE,
1088 APR_HASH_KEY_STRING)))
1090 const char *dav_path = svn_path_join(repos->path,
1091 SVN_REPOS__DAV_DIR, pool);
1092 SVN_ERR_W(create_repos_dir(dav_path, pool),
1093 _("Creating DAV sandbox dir"));
1096 /* Create the lock directory. */
1097 SVN_ERR(create_locks(repos, pool));
1099 /* Create the hooks directory. */
1100 SVN_ERR(create_hooks(repos, pool));
1102 /* Create the conf directory. */
1103 SVN_ERR(create_conf(repos, pool));
1105 /* Write the top-level README file. */
1107 const char * const readme_header =
1108 "This is a Subversion repository; use the 'svnadmin' tool to examine" NL
1109 "it. Do not add, delete, or modify files here unless you know how" NL
1110 "to avoid corrupting the repository." NL
1111 "" NL;
1112 const char * const readme_bdb_insert =
1113 "The directory \"" SVN_REPOS__DB_DIR "\" contains a Berkeley DB environment." NL
1114 "you may need to tweak the values in \"" SVN_REPOS__DB_DIR "/DB_CONFIG\" to match the" NL
1115 "requirements of your site." NL
1116 "" NL;
1117 const char * const readme_footer =
1118 "Visit http://subversion.tigris.org/ for more information." NL;
1119 apr_file_t *f;
1120 apr_size_t written;
1122 SVN_ERR(svn_io_file_open(&f,
1123 svn_path_join(path, SVN_REPOS__README, pool),
1124 (APR_WRITE | APR_CREATE | APR_EXCL),
1125 APR_OS_DEFAULT, pool));
1127 SVN_ERR(svn_io_file_write_full(f, readme_header, strlen(readme_header),
1128 &written, pool));
1129 if (strcmp(repos->fs_type, SVN_FS_TYPE_BDB) == 0)
1130 SVN_ERR(svn_io_file_write_full(f, readme_bdb_insert,
1131 strlen(readme_bdb_insert),
1132 &written, pool));
1133 SVN_ERR(svn_io_file_write_full(f, readme_footer, strlen(readme_footer),
1134 &written, pool));
1136 SVN_ERR(svn_io_file_close(f, pool));
1139 return SVN_NO_ERROR;
1143 /* There is, at present, nothing within the direct responsibility
1144 of libsvn_repos which requires locking. For historical compatibility
1145 reasons, the BDB libsvn_fs backend does not do its own locking, expecting
1146 libsvn_repos to do the locking for it. Here we take care of that
1147 backend-specific requirement.
1148 The kind of lock is controlled by EXCLUSIVE and NONBLOCKING.
1149 The lock is scoped to POOL. */
1150 static svn_error_t *
1151 lock_repos(svn_repos_t *repos,
1152 svn_boolean_t exclusive,
1153 svn_boolean_t nonblocking,
1154 apr_pool_t *pool)
1156 if (strcmp(repos->fs_type, SVN_FS_TYPE_BDB) == 0)
1158 svn_error_t *err;
1159 const char *lockfile_path = svn_repos_db_lockfile(repos, pool);
1161 err = svn_io_file_lock2(lockfile_path, exclusive, nonblocking, pool);
1162 if (err != NULL && APR_STATUS_IS_EAGAIN(err->apr_err))
1163 return err;
1164 SVN_ERR_W(err, _("Error opening db lockfile"));
1166 return SVN_NO_ERROR;
1170 svn_error_t *
1171 svn_repos_create(svn_repos_t **repos_p,
1172 const char *path,
1173 const char *unused_1,
1174 const char *unused_2,
1175 apr_hash_t *config,
1176 apr_hash_t *fs_config,
1177 apr_pool_t *pool)
1179 svn_repos_t *repos;
1180 svn_error_t *err;
1182 /* Allocate a repository object, filling in the format we will create. */
1183 repos = create_svn_repos_t(path, pool);
1184 repos->format = SVN_REPOS__FORMAT_NUMBER;
1186 /* Discover the type of the filesystem we are about to create. */
1187 if (fs_config)
1189 repos->fs_type = apr_hash_get(fs_config, SVN_FS_CONFIG_FS_TYPE,
1190 APR_HASH_KEY_STRING);
1191 if (apr_hash_get(fs_config, SVN_FS_CONFIG_PRE_1_4_COMPATIBLE,
1192 APR_HASH_KEY_STRING))
1193 repos->format = SVN_REPOS__FORMAT_NUMBER_LEGACY;
1196 if (! repos->fs_type)
1197 repos->fs_type = DEFAULT_FS_TYPE;
1199 /* Create the various files and subdirectories for the repository. */
1200 SVN_ERR_W(create_repos_structure(repos, path, fs_config, pool),
1201 _("Repository creation failed"));
1203 /* Lock if needed. */
1204 SVN_ERR(lock_repos(repos, FALSE, FALSE, pool));
1206 /* Create an environment for the filesystem. */
1207 if ((err = svn_fs_create(&repos->fs, repos->db_path, fs_config, pool)))
1209 /* If there was an error making the filesytem, e.g. unknown/supported
1210 * filesystem type. Clean up after ourselves. Yes this is safe because
1211 * create_repos_structure will fail if the path existed before we started
1212 * so we can't accidentally remove a directory that previously existed. */
1213 svn_error_clear(svn_io_remove_dir2(path, FALSE, NULL, NULL, pool));
1214 return err;
1217 /* This repository is ready. Stamp it with a format number. */
1218 SVN_ERR(svn_io_write_version_file
1219 (svn_path_join(path, SVN_REPOS__FORMAT, pool),
1220 repos->format, pool));
1222 *repos_p = repos;
1223 return SVN_NO_ERROR;
1227 /* Check if @a path is the root of a repository by checking if the
1228 * path contains the expected files and directories. Return TRUE
1229 * on errors (which would be permission errors, probably) so that
1230 * we the user will see them after we try to open the repository
1231 * for real. */
1232 static svn_boolean_t
1233 check_repos_path(const char *path,
1234 apr_pool_t *pool)
1236 svn_node_kind_t kind;
1237 svn_error_t *err;
1239 err = svn_io_check_path(svn_path_join(path, SVN_REPOS__FORMAT, pool),
1240 &kind, pool);
1241 if (err)
1243 svn_error_clear(err);
1244 return TRUE;
1246 if (kind != svn_node_file)
1247 return FALSE;
1249 /* Check the db/ subdir, but allow it to be a symlink (Subversion
1250 works just fine if it's a symlink). */
1251 err = svn_io_check_resolved_path
1252 (svn_path_join(path, SVN_REPOS__DB_DIR, pool), &kind, pool);
1253 if (err)
1255 svn_error_clear(err);
1256 return TRUE;
1258 if (kind != svn_node_dir)
1259 return FALSE;
1261 return TRUE;
1265 /* Verify that REPOS's format is suitable.
1266 Use POOL for temporary allocation. */
1267 static svn_error_t *
1268 check_repos_format(svn_repos_t *repos,
1269 apr_pool_t *pool)
1271 int format;
1272 const char *format_path;
1274 format_path = svn_path_join(repos->path, SVN_REPOS__FORMAT, pool);
1275 SVN_ERR(svn_io_read_version_file(&format, format_path, pool));
1277 if (format != SVN_REPOS__FORMAT_NUMBER &&
1278 format != SVN_REPOS__FORMAT_NUMBER_LEGACY)
1280 return svn_error_createf
1281 (SVN_ERR_REPOS_UNSUPPORTED_VERSION, NULL,
1282 _("Expected repository format '%d' or '%d'; found format '%d'"),
1283 SVN_REPOS__FORMAT_NUMBER_LEGACY, SVN_REPOS__FORMAT_NUMBER,
1284 format);
1287 repos->format = format;
1289 return SVN_NO_ERROR;
1293 /* Set *REPOS_P to a repository at PATH which has been opened.
1294 See lock_repos() above regarding EXCLUSIVE and NONBLOCKING.
1295 OPEN_FS indicates whether the Subversion filesystem should be opened,
1296 the handle being placed into repos->fs.
1297 Do all allocation in POOL. */
1298 static svn_error_t *
1299 get_repos(svn_repos_t **repos_p,
1300 const char *path,
1301 svn_boolean_t exclusive,
1302 svn_boolean_t nonblocking,
1303 svn_boolean_t open_fs,
1304 apr_pool_t *pool)
1306 svn_repos_t *repos;
1308 /* Allocate a repository object. */
1309 repos = create_svn_repos_t(path, pool);
1311 /* Verify the validity of our repository format. */
1312 SVN_ERR(check_repos_format(repos, pool));
1314 /* Discover the FS type. */
1315 SVN_ERR(svn_fs_type(&repos->fs_type, repos->db_path, pool));
1317 /* Lock if needed. */
1318 SVN_ERR(lock_repos(repos, exclusive, nonblocking, pool));
1320 /* Open up the filesystem only after obtaining the lock. */
1321 if (open_fs)
1322 SVN_ERR(svn_fs_open(&repos->fs, repos->db_path, NULL, pool));
1324 *repos_p = repos;
1325 return SVN_NO_ERROR;
1330 const char *
1331 svn_repos_find_root_path(const char *path,
1332 apr_pool_t *pool)
1334 const char *candidate = path;
1335 const char *decoded;
1336 svn_error_t *err;
1338 while (1)
1340 /* Try to decode the path, so we don't fail if it contains characters
1341 that aren't supported by the OS filesystem. The subversion fs
1342 isn't restricted by the OS filesystem character set. */
1343 err = svn_utf_cstring_from_utf8(&decoded, candidate, pool);
1344 if (!err && check_repos_path(candidate, pool))
1345 break;
1346 svn_error_clear(err);
1348 if (candidate[0] == '\0' ||
1349 svn_dirent_is_root(candidate, strlen(candidate)))
1350 return NULL;
1352 candidate = svn_path_dirname(candidate, pool);
1355 return candidate;
1359 svn_error_t *
1360 svn_repos_open(svn_repos_t **repos_p,
1361 const char *path,
1362 apr_pool_t *pool)
1364 /* Fetch a repository object initialized with a shared read/write
1365 lock on the database. */
1367 SVN_ERR(get_repos(repos_p, path, FALSE, FALSE, TRUE, pool));
1369 return SVN_NO_ERROR;
1373 svn_error_t *
1374 svn_repos_upgrade(const char *path,
1375 svn_boolean_t nonblocking,
1376 svn_error_t *(*start_callback)(void *baton),
1377 void *start_callback_baton,
1378 apr_pool_t *pool)
1380 svn_repos_t *repos;
1381 const char *format_path;
1382 int format;
1383 apr_pool_t *subpool = svn_pool_create(pool);
1385 /* Fetch a repository object; for the Berkeley DB backend, it is
1386 initialized with an EXCLUSIVE lock on the database. This will at
1387 least prevent others from trying to read or write to it while we
1388 run recovery. (Other backends should do their own locking; see
1389 lock_repos.) */
1390 SVN_ERR(get_repos(&repos, path, TRUE, nonblocking, FALSE, subpool));
1392 if (start_callback)
1393 SVN_ERR(start_callback(start_callback_baton));
1395 /* Try to overwrite with its own contents. We do this only to
1396 verify that we can, because we don't want to actually bump the
1397 format of the repository until our underlying filesystem claims
1398 to have been upgraded correctly. */
1399 format_path = svn_path_join(repos->path, SVN_REPOS__FORMAT, subpool);
1400 SVN_ERR(svn_io_read_version_file(&format, format_path, subpool));
1401 SVN_ERR(svn_io_write_version_file(format_path, format, subpool));
1403 /* Try to upgrade the filesystem. */
1404 SVN_ERR(svn_fs_upgrade(repos->db_path, subpool));
1406 /* Now overwrite our format file with the latest version. */
1407 SVN_ERR(svn_io_write_version_file(format_path, SVN_REPOS__FORMAT_NUMBER,
1408 subpool));
1410 /* Close shop and free the subpool, to release the exclusive lock. */
1411 svn_pool_destroy(subpool);
1413 return SVN_NO_ERROR;
1417 svn_error_t *
1418 svn_repos_delete(const char *path,
1419 apr_pool_t *pool)
1421 const char *db_path = svn_path_join(path, SVN_REPOS__DB_DIR, pool);
1423 /* Delete the filesystem environment... */
1424 SVN_ERR(svn_fs_delete_fs(db_path, pool));
1426 /* ...then blow away everything else. */
1427 SVN_ERR(svn_io_remove_dir2(path, FALSE, NULL, NULL, pool));
1429 return SVN_NO_ERROR;
1433 /* Repository supports the capability. */
1434 static const char *capability_yes = "yes";
1435 /* Repository does not support the capability. */
1436 static const char *capability_no = "no";
1438 svn_error_t *
1439 svn_repos_has_capability(svn_repos_t *repos,
1440 svn_boolean_t *has,
1441 const char *capability,
1442 apr_pool_t *pool)
1444 const char *val = apr_hash_get(repos->repository_capabilities,
1445 capability, APR_HASH_KEY_STRING);
1447 if (val == capability_yes)
1449 *has = TRUE;
1451 else if (val == capability_no)
1453 *has = FALSE;
1455 /* Else don't know, so investigate. */
1456 else if (strcmp(capability, SVN_REPOS_CAPABILITY_MERGEINFO) == 0)
1458 svn_error_t *err;
1459 svn_fs_root_t *root;
1460 svn_mergeinfo_catalog_t ignored;
1461 apr_array_header_t *paths = apr_array_make(pool, 1,
1462 sizeof(char *));
1464 SVN_ERR(svn_fs_revision_root(&root, repos->fs, 0, pool));
1465 APR_ARRAY_PUSH(paths, const char *) = "";
1466 err = svn_fs_get_mergeinfo(&ignored, root, paths, FALSE, FALSE, pool);
1468 if (err)
1470 if (err->apr_err == SVN_ERR_UNSUPPORTED_FEATURE)
1472 svn_error_clear(err);
1473 apr_hash_set(repos->repository_capabilities,
1474 SVN_REPOS_CAPABILITY_MERGEINFO,
1475 APR_HASH_KEY_STRING, capability_no);
1476 *has = FALSE;
1478 else if (err->apr_err == SVN_ERR_FS_NOT_FOUND)
1480 /* Mergeinfo requests use relative paths, and anyway we're
1481 in r0, so we're likely to get this error -- but it
1482 means the repository supports mergeinfo! */
1483 svn_error_clear(err);
1484 apr_hash_set(repos->repository_capabilities,
1485 SVN_REPOS_CAPABILITY_MERGEINFO,
1486 APR_HASH_KEY_STRING, capability_yes);
1487 *has = TRUE;
1489 else
1491 return err;
1494 else
1496 apr_hash_set(repos->repository_capabilities,
1497 SVN_REPOS_CAPABILITY_MERGEINFO,
1498 APR_HASH_KEY_STRING, capability_yes);
1499 *has = TRUE;
1502 else
1504 return svn_error_createf(SVN_ERR_UNKNOWN_CAPABILITY, 0,
1505 _("unknown capability '%s'"), capability);
1508 return SVN_NO_ERROR;
1512 svn_fs_t *
1513 svn_repos_fs(svn_repos_t *repos)
1515 if (! repos)
1516 return NULL;
1517 return repos->fs;
1521 /* For historical reasons, for the Berkeley DB backend, this code uses
1522 * repository locking, which is motivated by the need to support the
1523 * Berkeley DB error DB_RUN_RECOVERY. (FSFS takes care of locking
1524 * itself, inside its implementation of svn_fs_recover.) Here's how
1525 * it works:
1527 * Every accessor of a repository's database takes out a shared lock
1528 * on the repository -- both readers and writers get shared locks, and
1529 * there can be an unlimited number of shared locks simultaneously.
1531 * Sometimes, a db access returns the error DB_RUN_RECOVERY. When
1532 * this happens, we need to run svn_fs_recover() on the db
1533 * with no other accessors present. So we take out an exclusive lock
1534 * on the repository. From the moment we request the exclusive lock,
1535 * no more shared locks are granted, and when the last shared lock
1536 * disappears, the exclusive lock is granted. As soon as we get it,
1537 * we can run recovery.
1539 * We assume that once any berkeley call returns DB_RUN_RECOVERY, they
1540 * all do, until recovery is run.
1543 svn_error_t *
1544 svn_repos_recover3(const char *path,
1545 svn_boolean_t nonblocking,
1546 svn_error_t *(*start_callback)(void *baton),
1547 void *start_callback_baton,
1548 svn_cancel_func_t cancel_func, void *cancel_baton,
1549 apr_pool_t *pool)
1551 svn_repos_t *repos;
1552 apr_pool_t *subpool = svn_pool_create(pool);
1554 /* Fetch a repository object; for the Berkeley DB backend, it is
1555 initialized with an EXCLUSIVE lock on the database. This will at
1556 least prevent others from trying to read or write to it while we
1557 run recovery. (Other backends should do their own locking; see
1558 lock_repos.) */
1559 SVN_ERR(get_repos(&repos, path, TRUE, nonblocking,
1560 FALSE, /* don't try to open the db yet. */
1561 subpool));
1563 if (start_callback)
1564 SVN_ERR(start_callback(start_callback_baton));
1566 /* Recover the database to a consistent state. */
1567 SVN_ERR(svn_fs_recover(repos->db_path, cancel_func, cancel_baton, subpool));
1569 /* Close shop and free the subpool, to release the exclusive lock. */
1570 svn_pool_destroy(subpool);
1572 return SVN_NO_ERROR;
1576 svn_error_t *
1577 svn_repos_recover2(const char *path,
1578 svn_boolean_t nonblocking,
1579 svn_error_t *(*start_callback)(void *baton),
1580 void *start_callback_baton,
1581 apr_pool_t *pool)
1583 return svn_repos_recover3(path, nonblocking,
1584 start_callback, start_callback_baton,
1585 NULL, NULL,
1586 pool);
1589 svn_error_t *
1590 svn_repos_recover(const char *path,
1591 apr_pool_t *pool)
1593 return svn_repos_recover2(path, FALSE, NULL, NULL, pool);
1596 svn_error_t *svn_repos_db_logfiles(apr_array_header_t **logfiles,
1597 const char *path,
1598 svn_boolean_t only_unused,
1599 apr_pool_t *pool)
1601 svn_repos_t *repos;
1602 int i;
1604 SVN_ERR(get_repos(&repos, path,
1605 FALSE, FALSE,
1606 FALSE, /* Do not open fs. */
1607 pool));
1609 SVN_ERR(svn_fs_berkeley_logfiles(logfiles,
1610 svn_repos_db_env(repos, pool),
1611 only_unused,
1612 pool));
1614 /* Loop, printing log files. */
1615 for (i = 0; i < (*logfiles)->nelts; i++)
1617 const char ** log_file = &(APR_ARRAY_IDX(*logfiles, i, const char *));
1618 *log_file = svn_path_join(SVN_REPOS__DB_DIR, *log_file, pool);
1621 return SVN_NO_ERROR;
1624 /** Hot copy structure copy context.
1626 struct hotcopy_ctx_t {
1627 const char *dest; /* target location to construct */
1628 unsigned int src_len; /* len of the source path*/
1631 /** Called by (svn_io_dir_walk).
1632 * Copies the repository structure with exception of @c SVN_REPOS__DB_DIR,
1633 * @c SVN_REPOS__LOCK_DIR and @c SVN_REPOS__FORMAT.
1634 * Those directories and files are handled separetly.
1635 * @a baton is a pointer to (struct hotcopy_ctx_t) specifying
1636 * destination path to copy to and the length of the source path.
1638 * @copydoc svn_io_dir_walk()
1640 static svn_error_t *hotcopy_structure(void *baton,
1641 const char *path,
1642 const apr_finfo_t *finfo,
1643 apr_pool_t *pool)
1645 const struct hotcopy_ctx_t *ctx = ((struct hotcopy_ctx_t *) baton);
1646 const char *sub_path;
1647 const char *target;
1649 if (strlen(path) == ctx->src_len)
1651 sub_path = "";
1653 else
1655 sub_path = &path[ctx->src_len+1];
1657 /* Check if we are inside db directory and if so skip it */
1658 if (svn_path_compare_paths
1659 (svn_path_get_longest_ancestor(SVN_REPOS__DB_DIR, sub_path, pool),
1660 SVN_REPOS__DB_DIR) == 0)
1661 return SVN_NO_ERROR;
1663 if (svn_path_compare_paths
1664 (svn_path_get_longest_ancestor(SVN_REPOS__LOCK_DIR, sub_path, pool),
1665 SVN_REPOS__LOCK_DIR) == 0)
1666 return SVN_NO_ERROR;
1668 if (svn_path_compare_paths
1669 (svn_path_get_longest_ancestor(SVN_REPOS__FORMAT, sub_path, pool),
1670 SVN_REPOS__FORMAT) == 0)
1671 return SVN_NO_ERROR;
1674 target = svn_path_join(ctx->dest, sub_path, pool);
1676 if (finfo->filetype == APR_DIR)
1678 SVN_ERR(create_repos_dir(target, pool));
1680 else if (finfo->filetype == APR_REG)
1683 SVN_ERR(svn_io_copy_file(path, target, TRUE, pool));
1686 return SVN_NO_ERROR;
1690 /** Obtain a lock on db logs lock file. Create one if it does not exist.
1692 static svn_error_t *
1693 lock_db_logs_file(svn_repos_t *repos,
1694 svn_boolean_t exclusive,
1695 apr_pool_t *pool)
1697 const char * lock_file = svn_repos_db_logs_lockfile(repos, pool);
1699 /* Try to create a lock file, in case if it is missing. As in case of the
1700 repositories created before hotcopy functionality. */
1701 svn_error_clear(create_db_logs_lock(repos, pool));
1703 SVN_ERR(svn_io_file_lock2(lock_file, exclusive, FALSE, pool));
1705 return SVN_NO_ERROR;
1709 /* Make a copy of a repository with hot backup of fs. */
1710 svn_error_t *
1711 svn_repos_hotcopy(const char *src_path,
1712 const char *dst_path,
1713 svn_boolean_t clean_logs,
1714 apr_pool_t *pool)
1716 svn_repos_t *src_repos;
1717 svn_repos_t *dst_repos;
1718 struct hotcopy_ctx_t hotcopy_context;
1720 /* Try to open original repository */
1721 SVN_ERR(get_repos(&src_repos, src_path,
1722 FALSE, FALSE,
1723 FALSE, /* don't try to open the db yet. */
1724 pool));
1726 /* If we are going to clean logs, then get an exclusive lock on
1727 db-logs.lock, to ensure that no one else will work with logs.
1729 If we are just copying, then get a shared lock to ensure that
1730 no one else will clean logs while we copying them */
1732 SVN_ERR(lock_db_logs_file(src_repos, clean_logs, pool));
1734 /* Copy the repository to a new path, with exception of
1735 specially handled directories */
1737 hotcopy_context.dest = dst_path;
1738 hotcopy_context.src_len = strlen(src_path);
1739 SVN_ERR(svn_io_dir_walk(src_path,
1741 hotcopy_structure,
1742 &hotcopy_context,
1743 pool));
1745 /* Prepare dst_repos object so that we may create locks,
1746 so that we may open repository */
1748 dst_repos = create_svn_repos_t(dst_path, pool);
1749 dst_repos->fs_type = src_repos->fs_type;
1750 dst_repos->format = src_repos->format;
1752 SVN_ERR(create_locks(dst_repos, pool));
1754 SVN_ERR(svn_io_dir_make_sgid(dst_repos->db_path, APR_OS_DEFAULT, pool));
1756 /* Exclusively lock the new repository.
1757 No one should be accessing it at the moment */
1758 SVN_ERR(lock_repos(dst_repos, TRUE, FALSE, pool));
1760 SVN_ERR(svn_fs_hotcopy(src_repos->db_path, dst_repos->db_path,
1761 clean_logs, pool));
1763 /* Destination repository is ready. Stamp it with a format number. */
1764 SVN_ERR(svn_io_write_version_file
1765 (svn_path_join(dst_repos->path, SVN_REPOS__FORMAT, pool),
1766 dst_repos->format, pool));
1768 return SVN_NO_ERROR;
1771 /* Return the library version number. */
1772 const svn_version_t *
1773 svn_repos_version(void)
1775 SVN_VERSION_BODY;
1780 svn_error_t *
1781 svn_repos_stat(svn_dirent_t **dirent,
1782 svn_fs_root_t *root,
1783 const char *path,
1784 apr_pool_t *pool)
1786 svn_node_kind_t kind;
1787 svn_dirent_t *ent;
1788 const char *datestring;
1789 apr_hash_t *prophash;
1791 SVN_ERR(svn_fs_check_path(&kind, root, path, pool));
1793 if (kind == svn_node_none)
1795 *dirent = NULL;
1796 return SVN_NO_ERROR;
1799 ent = apr_pcalloc(pool, sizeof(*ent));
1800 ent->kind = kind;
1802 if (kind == svn_node_file)
1803 SVN_ERR(svn_fs_file_length(&(ent->size), root, path, pool));
1805 SVN_ERR(svn_fs_node_proplist(&prophash, root, path, pool));
1806 if (apr_hash_count(prophash) > 0)
1807 ent->has_props = TRUE;
1809 SVN_ERR(svn_repos_get_committed_info(&(ent->created_rev),
1810 &datestring,
1811 &(ent->last_author),
1812 root, path, pool));
1813 if (datestring)
1814 SVN_ERR(svn_time_from_cstring(&(ent->time), datestring, pool));
1816 *dirent = ent;
1817 return SVN_NO_ERROR;
1820 svn_error_t *
1821 svn_repos_remember_client_capabilities(svn_repos_t *repos,
1822 apr_array_header_t *capabilities)
1824 repos->client_capabilities = capabilities;
1825 return SVN_NO_ERROR;