| 927 | } |
| 928 | |
| 929 | int should_prune_worktree(const char *id, struct strbuf *reason, char **wtpath, timestamp_t expire) |
| 930 | { |
| 931 | struct stat st; |
| 932 | struct strbuf dotgit = STRBUF_INIT; |
| 933 | struct strbuf gitdir = STRBUF_INIT; |
| 934 | struct strbuf repo = STRBUF_INIT; |
| 935 | struct strbuf file = STRBUF_INIT; |
| 936 | char *path = NULL; |
| 937 | int rc = 0; |
| 938 | int fd; |
| 939 | size_t len; |
| 940 | ssize_t read_result; |
| 941 | |
| 942 | *wtpath = NULL; |
| 943 | |
| 944 | path = repo_common_path(the_repository, "worktrees/%s", id); |
| 945 | strbuf_realpath(&repo, path, 1); |
| 946 | FREE_AND_NULL(path); |
| 947 | |
| 948 | strbuf_addf(&gitdir, "%s/gitdir", repo.buf); |
| 949 | if (!is_directory(repo.buf)) { |
| 950 | strbuf_addstr(reason, _("not a valid directory")); |
| 951 | rc = 1; |
| 952 | goto done; |
| 953 | } |
| 954 | strbuf_addf(&file, "%s/locked", repo.buf); |
| 955 | if (file_exists(file.buf)) { |
| 956 | goto done; |
| 957 | } |
| 958 | if (stat(gitdir.buf, &st)) { |
| 959 | strbuf_addstr(reason, _("gitdir file does not exist")); |
| 960 | rc = 1; |
| 961 | goto done; |
| 962 | } |
| 963 | fd = open(gitdir.buf, O_RDONLY); |
| 964 | if (fd < 0) { |
| 965 | strbuf_addf(reason, _("unable to read gitdir file (%s)"), |
| 966 | strerror(errno)); |
| 967 | rc = 1; |
| 968 | goto done; |
| 969 | } |
| 970 | len = xsize_t(st.st_size); |
| 971 | path = xmallocz(len); |
| 972 | |
| 973 | read_result = read_in_full(fd, path, len); |
| 974 | close(fd); |
| 975 | if (read_result < 0) { |
| 976 | strbuf_addf(reason, _("unable to read gitdir file (%s)"), |
| 977 | strerror(errno)); |
| 978 | rc = 1; |
| 979 | goto done; |
| 980 | } else if (read_result != len) { |
| 981 | strbuf_addf(reason, |
| 982 | _("short read (expected %"PRIuMAX" bytes, read %"PRIuMAX")"), |
| 983 | (uintmax_t)len, (uintmax_t)read_result); |
| 984 | rc = 1; |
| 985 | goto done; |
| 986 | } |
no test coverage detected