path.h 1.88 KB
Newer Older
Vicent Marti committed
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
Vicent Marti committed
3 4 5
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
Vicent Marti committed
6 7 8 9 10
 */
#ifndef INCLUDE_path_h__
#define INCLUDE_path_h__

#include "common.h"
11

12 13
#include "fs_path.h"
#include <git2/sys/path.h>
Vicent Marti committed
14

15 16 17 18
#define GIT_PATH_REJECT_DOT_GIT            (GIT_FS_PATH_REJECT_MAX << 1)
#define GIT_PATH_REJECT_DOT_GIT_LITERAL    (GIT_FS_PATH_REJECT_MAX << 2)
#define GIT_PATH_REJECT_DOT_GIT_HFS        (GIT_FS_PATH_REJECT_MAX << 3)
#define GIT_PATH_REJECT_DOT_GIT_NTFS       (GIT_FS_PATH_REJECT_MAX << 4)
19

20
/* Paths that should never be written into the working directory. */
21
#define GIT_PATH_REJECT_WORKDIR_DEFAULTS \
22
	GIT_FS_PATH_REJECT_FILESYSTEM_DEFAULTS | GIT_PATH_REJECT_DOT_GIT
23 24 25

/* Paths that should never be written to the index. */
#define GIT_PATH_REJECT_INDEX_DEFAULTS \
26
	GIT_FS_PATH_REJECT_TRAVERSAL | GIT_PATH_REJECT_DOT_GIT
27

28
extern bool git_path_str_is_valid(
29
	git_repository *repo,
30
	const git_str *path,
31
	uint16_t file_mode,
32 33
	unsigned int flags);

34 35 36 37 38 39 40 41 42 43
GIT_INLINE(bool) git_path_is_valid(
	git_repository *repo,
	const char *path,
	uint16_t file_mode,
	unsigned int flags)
{
	git_str str = GIT_STR_INIT_CONST(path, SIZE_MAX);
	return git_path_str_is_valid(repo, &str, file_mode, flags);
}

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
GIT_INLINE(int) git_path_validate_str_length(
	git_repository *repo,
	const git_str *path)
{
	if (!git_path_str_is_valid(repo, path, 0, GIT_FS_PATH_REJECT_LONG_PATHS)) {
		if (path->size == SIZE_MAX)
			git_error_set(GIT_ERROR_FILESYSTEM, "path too long: '%s'", path->ptr);
		else
			git_error_set(GIT_ERROR_FILESYSTEM, "path too long: '%.*s'", (int)path->size, path->ptr);

		return -1;
	}

	return 0;
}

GIT_INLINE(int) git_path_validate_length(
	git_repository *repo,
	const char *path)
{
	git_str str = GIT_STR_INIT_CONST(path, SIZE_MAX);
	return git_path_validate_str_length(repo, &str);
}

Vicent Marti committed
68
#endif