📄 repos.c
字号:
static svn_boolean_tcheck_repos_path(const char *path, apr_pool_t *pool){ svn_node_kind_t kind; svn_error_t *err; err = svn_io_check_path(svn_path_join(path, SVN_REPOS__FORMAT, pool), &kind, pool); if (err) { svn_error_clear(err); return TRUE; } if (kind != svn_node_file) return FALSE; /* Check the db/ subdir, but allow it to be a symlink (Subversion works just fine if it's a symlink). */ err = svn_io_check_resolved_path (svn_path_join(path, SVN_REPOS__DB_DIR, pool), &kind, pool); if (err) { svn_error_clear(err); return TRUE; } if (kind != svn_node_dir) return FALSE; return TRUE;}/* Verify that REPOS's format is suitable. Use POOL for temporary allocation. */static svn_error_t *check_repos_format(svn_repos_t *repos, apr_pool_t *pool){ int format; const char *format_path; format_path = svn_path_join(repos->path, SVN_REPOS__FORMAT, pool); SVN_ERR(svn_io_read_version_file(&format, format_path, pool)); if (format != SVN_REPOS__FORMAT_NUMBER && format != SVN_REPOS__FORMAT_NUMBER_LEGACY) { return svn_error_createf (SVN_ERR_REPOS_UNSUPPORTED_VERSION, NULL, _("Expected repository format '%d' or '%d'; found format '%d'"), SVN_REPOS__FORMAT_NUMBER_LEGACY, SVN_REPOS__FORMAT_NUMBER, format); } repos->format = format; return SVN_NO_ERROR;}/* Set *REPOS_P to a repository at PATH which has been opened. See lock_repos() above regarding EXCLUSIVE and NONBLOCKING. OPEN_FS indicates whether the Subversion filesystem should be opened, the handle being placed into repos->fs. Do all allocation in POOL. */static svn_error_t *get_repos(svn_repos_t **repos_p, const char *path, svn_boolean_t exclusive, svn_boolean_t nonblocking, svn_boolean_t open_fs, apr_pool_t *pool){ svn_repos_t *repos; /* Allocate a repository object. */ repos = create_svn_repos_t(path, pool); /* Verify the validity of our repository format. */ SVN_ERR(check_repos_format(repos, pool)); /* Discover the FS type. */ SVN_ERR(svn_fs_type(&repos->fs_type, repos->db_path, pool)); /* Lock if needed. */ SVN_ERR(lock_repos(repos, exclusive, nonblocking, pool)); /* Open up the filesystem only after obtaining the lock. */ if (open_fs) SVN_ERR(svn_fs_open(&repos->fs, repos->db_path, NULL, pool)); *repos_p = repos; return SVN_NO_ERROR;}const char *svn_repos_find_root_path(const char *path, apr_pool_t *pool){ const char *candidate = path; const char *decoded; svn_error_t *err; while (1) { /* Try to decode the path, so we don't fail if it contains characters that aren't supported by the OS filesystem. The subversion fs isn't restricted by the OS filesystem character set. */ err = svn_utf_cstring_from_utf8(&decoded, candidate, pool); if (!err && check_repos_path(candidate, pool)) break; svn_error_clear(err); if (candidate[0] == '\0' || strcmp(candidate, "/") == 0) return NULL; candidate = svn_path_dirname(candidate, pool); } return candidate;}svn_error_t *svn_repos_open(svn_repos_t **repos_p, const char *path, apr_pool_t *pool){ /* Fetch a repository object initialized with a shared read/write lock on the database. */ SVN_ERR(get_repos(repos_p, path, FALSE, FALSE, TRUE, pool)); return SVN_NO_ERROR;}svn_error_t *svn_repos_delete(const char *path, apr_pool_t *pool){ const char *db_path = svn_path_join(path, SVN_REPOS__DB_DIR, pool); /* Delete the filesystem environment... */ SVN_ERR(svn_fs_delete_fs(db_path, pool)); /* ...then blow away everything else. */ SVN_ERR(svn_io_remove_dir(path, pool)); return SVN_NO_ERROR;}svn_fs_t *svn_repos_fs(svn_repos_t *repos){ if (! repos) return NULL; return repos->fs;}/* This code uses repository locking, which is motivated by the * need to support DB_RUN_RECOVERY. Here's how it works: * * Every accessor of a repository's database takes out a shared lock * on the repository -- both readers and writers get shared locks, and * there can be an unlimited number of shared locks simultaneously. * * Sometimes, a db access returns the error DB_RUN_RECOVERY. When * this happens, we need to run svn_fs_berkeley_recover() on the db * with no other accessors present. So we take out an exclusive lock * on the repository. From the moment we request the exclusive lock, * no more shared locks are granted, and when the last shared lock * disappears, the exclusive lock is granted. As soon as we get it, * we can run recovery. * * We assume that once any berkeley call returns DB_RUN_RECOVERY, they * all do, until recovery is run. */svn_error_t *svn_repos_recover2(const char *path, svn_boolean_t nonblocking, svn_error_t *(*start_callback)(void *baton), void *start_callback_baton, apr_pool_t *pool){ svn_repos_t *repos; apr_pool_t *subpool = svn_pool_create(pool); /* Fetch a repository object initialized with an EXCLUSIVE lock on the database. This will at least prevent others from trying to read or write to it while we run recovery. */ SVN_ERR(get_repos(&repos, path, TRUE, nonblocking, FALSE, /* don't try to open the db yet. */ subpool)); if (start_callback) SVN_ERR(start_callback(start_callback_baton)); /* Recover the database to a consistent state. */ SVN_ERR(svn_fs_berkeley_recover(repos->db_path, subpool)); /* Close shop and free the subpool, to release the exclusive lock. */ svn_pool_destroy(subpool); return SVN_NO_ERROR;}svn_error_t *svn_repos_recover(const char *path, apr_pool_t *pool){ return svn_repos_recover2(path, FALSE, NULL, NULL, pool);}svn_error_t *svn_repos_db_logfiles(apr_array_header_t **logfiles, const char *path, svn_boolean_t only_unused, apr_pool_t *pool){ svn_repos_t *repos; int i; SVN_ERR(get_repos(&repos, path, FALSE, FALSE, FALSE, /* Do not open fs. */ pool)); SVN_ERR(svn_fs_berkeley_logfiles(logfiles, svn_repos_db_env(repos, pool), only_unused, pool)); /* Loop, printing log files. */ for (i = 0; i < (*logfiles)->nelts; i++) { const char ** log_file = &(APR_ARRAY_IDX(*logfiles, i, const char *)); *log_file = svn_path_join(SVN_REPOS__DB_DIR, *log_file, pool); } return SVN_NO_ERROR;}/** Hot copy structure copy context. */struct hotcopy_ctx_t { const char *dest; /* target location to construct */ unsigned int src_len; /* len of the source path*/};/** Called by (svn_io_dir_walk). * Copies the repository structure with exception of @c SVN_REPOS__DB_DIR, * @c SVN_REPOS__LOCK_DIR and @c SVN_REPOS__FORMAT. * Those directories and files are handled separetly. * @a baton is a pointer to (struct hotcopy_ctx_t) specifying * destination path to copy to and the length of the source path. * * @copydoc svn_io_dir_walk() */static svn_error_t *hotcopy_structure(void *baton, const char *path, const apr_finfo_t *finfo, apr_pool_t *pool){ const struct hotcopy_ctx_t *ctx = ((struct hotcopy_ctx_t *) baton); const char *sub_path; const char *target; if (strlen(path) == ctx->src_len) { sub_path = ""; } else { sub_path = &path[ctx->src_len+1]; /* Check if we are inside db directory and if so skip it */ if (svn_path_compare_paths (svn_path_get_longest_ancestor(SVN_REPOS__DB_DIR, sub_path, pool), SVN_REPOS__DB_DIR) == 0) return SVN_NO_ERROR; if (svn_path_compare_paths (svn_path_get_longest_ancestor(SVN_REPOS__LOCK_DIR, sub_path, pool), SVN_REPOS__LOCK_DIR) == 0) return SVN_NO_ERROR; if (svn_path_compare_paths (svn_path_get_longest_ancestor(SVN_REPOS__FORMAT, sub_path, pool), SVN_REPOS__FORMAT) == 0) return SVN_NO_ERROR; } target = svn_path_join(ctx->dest, sub_path, pool); if (finfo->filetype == APR_DIR) { SVN_ERR(create_repos_dir(target, pool)); } else if (finfo->filetype == APR_REG) { SVN_ERR(svn_io_copy_file(path, target, TRUE, pool)); } return SVN_NO_ERROR;}/** Obtain a lock on db logs lock file. Create one if it does not exist. */static svn_error_t *lock_db_logs_file(svn_repos_t *repos, svn_boolean_t exclusive, apr_pool_t *pool){ const char * lock_file = svn_repos_db_logs_lockfile(repos, pool); /* Try to create a lock file, in case if it is missing. As in case of the repositories created before hotcopy functionality. */ svn_error_clear(create_db_logs_lock(repos, pool)); SVN_ERR(svn_io_file_lock2(lock_file, exclusive, FALSE, pool)); return SVN_NO_ERROR;}/* Make a copy of a repository with hot backup of fs. */svn_error_t *svn_repos_hotcopy(const char *src_path, const char *dst_path, svn_boolean_t clean_logs, apr_pool_t *pool){ svn_repos_t *src_repos; svn_repos_t *dst_repos; struct hotcopy_ctx_t hotcopy_context; /* Try to open original repository */ SVN_ERR(get_repos(&src_repos, src_path, FALSE, FALSE, FALSE, /* don't try to open the db yet. */ pool)); /* If we are going to clean logs, then get an exclusive lock on db-logs.lock, to ensure that no one else will work with logs. If we are just copying, then get a shared lock to ensure that no one else will clean logs while we copying them */ SVN_ERR(lock_db_logs_file(src_repos, clean_logs, pool)); /* Copy the repository to a new path, with exception of specially handled directories */ hotcopy_context.dest = dst_path; hotcopy_context.src_len = strlen(src_path); SVN_ERR(svn_io_dir_walk(src_path, 0, hotcopy_structure, &hotcopy_context, pool)); /* Prepare dst_repos object so that we may create locks, so that we may open repository */ dst_repos = create_svn_repos_t(dst_path, pool); dst_repos->fs_type = src_repos->fs_type; dst_repos->format = src_repos->format; SVN_ERR(create_locks(dst_repos, pool)); SVN_ERR(svn_io_dir_make_sgid(dst_repos->db_path, APR_OS_DEFAULT, pool)); /* Exclusively lock the new repository. No one should be accessing it at the moment */ SVN_ERR(lock_repos(dst_repos, TRUE, FALSE, pool)); SVN_ERR(svn_fs_hotcopy(src_repos->db_path, dst_repos->db_path, clean_logs, pool)); /* Destination repository is ready. Stamp it with a format number. */ SVN_ERR(svn_io_write_version_file (svn_path_join(dst_repos->path, SVN_REPOS__FORMAT, pool), dst_repos->format, pool)); return SVN_NO_ERROR;}/* Return the library version number. */const svn_version_t *svn_repos_version(void){ SVN_VERSION_BODY;}svn_error_t *svn_repos_stat(svn_dirent_t **dirent, svn_fs_root_t *root, const char *path, apr_pool_t *pool){ svn_node_kind_t kind; svn_dirent_t *ent; const char *datestring; apr_hash_t *prophash; SVN_ERR(svn_fs_check_path(&kind, root, path, pool)); if (kind == svn_node_none) { *dirent = NULL; return SVN_NO_ERROR; } ent = apr_pcalloc(pool, sizeof(*ent)); en
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -