Hash :
ef99fdb1
Author :
Date :
2018-03-11T02:31:27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
/*
* Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/stat.h>
#include <sys/limits.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include "got_error.h"
#include "got_repository.h"
#include "got_refs.h"
#include "got_worktree.h"
#include "got_worktree_priv.h"
#include "got_path_priv.h"
static const struct got_error *
create_meta_file(const char *gotpath, const char *name, const char *content)
{
const struct got_error *err = NULL;
char *path;
int fd = -1;
char buf[4];
ssize_t n;
if (asprintf(&path, "%s/%s", gotpath, name) == -1) {
err = got_error(GOT_ERR_NO_MEM);
path = NULL;
goto done;
}
fd = open(path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
GOT_DEFAULT_FILE_MODE);
if (fd == -1) {
err = got_error_from_errno();
goto done;
}
/* The file should be empty. */
n = read(fd, buf, sizeof(buf));
if (n != 0) {
err = (n == -1 ? got_error_from_errno() :
got_error(GOT_ERR_WORKTREE_EXISTS));
goto done;
}
if (content) {
int len = dprintf(fd, "%s\n", content);
if (len != strlen(content) + 1) {
err = got_error_from_errno();
goto done;
}
}
done:
if (fd != -1 && close(fd) == -1 && err == NULL)
err = got_error_from_errno();
free(path);
return err;
}
static const struct got_error *
read_meta_file(char **content, const char *gotpath, const char *name)
{
const struct got_error *err = NULL;
char *path;
int fd = -1;
ssize_t n;
struct stat sb;
*content = NULL;
if (asprintf(&path, "%s/%s", gotpath, name) == -1) {
err = got_error(GOT_ERR_NO_MEM);
path = NULL;
goto done;
}
fd = open(path, O_RDONLY | O_NOFOLLOW);
if (fd == -1) {
err = got_error_from_errno();
goto done;
}
if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
: got_error_from_errno());
goto done;
}
stat(path, &sb);
*content = calloc(1, sb.st_size);
if (*content == NULL) {
err = got_error(GOT_ERR_NO_MEM);
goto done;
}
n = read(fd, *content, sb.st_size);
if (n != sb.st_size) {
err = got_error_from_errno();
goto done;
}
if ((*content)[sb.st_size - 1] != '\n') {
err = got_error(GOT_ERR_WORKTREE_META);
goto done;
}
(*content)[sb.st_size - 1] = '\0';
done:
if (fd != -1 && close(fd) == -1 && err == NULL)
err = got_error_from_errno();
free(path);
if (err) {
free(*content);
*content = NULL;
}
return err;
}
const struct got_error *
got_worktree_init(const char *path, struct got_reference *head_ref,
const char *prefix, struct got_repository *repo)
{
const struct got_error *err = NULL;
char *gotpath = NULL;
char *refstr = NULL;
char *path_repos = NULL;
char *formatstr = NULL;
if (!got_path_is_absolute(prefix))
return got_error(GOT_ERR_BAD_PATH);
/* Create top-level directory (may already exist). */
if (mkdir(path, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
err = got_error_from_errno();
goto done;
}
/* Create .got directory (may already exist). */
if (asprintf(&gotpath, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
err = got_error(GOT_ERR_NO_MEM);
goto done;
}
if (mkdir(gotpath, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
err = got_error_from_errno();
goto done;
}
/* Create an empty file index. */
err = create_meta_file(gotpath, GOT_WORKTREE_FILE_INDEX, NULL);
if (err)
goto done;
/* Write the HEAD reference. */
refstr = got_ref_to_str(head_ref);
if (refstr == NULL) {
err = got_error(GOT_ERR_NO_MEM);
goto done;
}
err = create_meta_file(gotpath, GOT_REF_HEAD, refstr);
if (err)
goto done;
/* Store path to repository. */
path_repos = got_repo_get_path(repo);
if (path_repos == NULL) {
err = got_error(GOT_ERR_NO_MEM);
goto done;
}
err = create_meta_file(gotpath, GOT_WORKTREE_REPOSITORY, path_repos);
if (err)
goto done;
/* Store in-repository path prefix. */
err = create_meta_file(gotpath, GOT_WORKTREE_PATH_PREFIX, prefix);
if (err)
goto done;
/* Stamp work tree with format file. */
if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
err = got_error(GOT_ERR_NO_MEM);
goto done;
}
err = create_meta_file(gotpath, GOT_WORKTREE_FORMAT, formatstr);
if (err)
goto done;
done:
free(gotpath);
free(formatstr);
free(refstr);
free(path_repos);
return err;
}
const struct got_error *
got_worktree_open(struct got_worktree **worktree, const char *path)
{
const struct got_error *err = NULL;
char *gotpath;
char *refstr = NULL;
char *path_repos = NULL;
char *formatstr = NULL;
char *path_fileindex = NULL;
int version, fd = -1;
const char *errstr;
*worktree = NULL;
if (asprintf(&gotpath, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
err = got_error(GOT_ERR_NO_MEM);
gotpath = NULL;
goto done;
}
if (asprintf(&path_fileindex, "%s/%s", gotpath,
GOT_WORKTREE_FILE_INDEX) == -1) {
err = got_error(GOT_ERR_NO_MEM);
path_fileindex = NULL;
goto done;
}
fd = open(path_fileindex, O_RDWR | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
if (fd == -1) {
err = got_error_from_errno();
goto done;
}
if (flock(fd, LOCK_SH | LOCK_NB) == -1) {
err = (errno == EWOULDBLOCK ? got_error(GOT_ERR_WORKTREE_BUSY)
: got_error_from_errno());
goto done;
}
err = read_meta_file(&formatstr, gotpath, GOT_WORKTREE_FORMAT);
if (err)
goto done;
version = strtonum(formatstr, 1, INT_MAX, &errstr);
if (errstr) {
err = got_error(GOT_ERR_WORKTREE_META);
goto done;
}
if (version != GOT_WORKTREE_FORMAT_VERSION) {
err = got_error(GOT_ERR_WORKTREE_VERS);
goto done;
}
*worktree = calloc(1, sizeof(**worktree));
if (*worktree == NULL) {
err = got_error(GOT_ERR_NO_MEM);
goto done;
}
(*worktree)->fd_fileindex = -1;
(*worktree)->path_worktree_root = strdup(path);
if ((*worktree)->path_worktree_root == NULL) {
err = got_error(GOT_ERR_NO_MEM);
goto done;
}
err = read_meta_file(&(*worktree)->path_repo, gotpath,
GOT_WORKTREE_REPOSITORY);
if (err)
goto done;
err = read_meta_file(&(*worktree)->path_prefix, gotpath,
GOT_WORKTREE_PATH_PREFIX);
goto done;
done:
free(gotpath);
free(path_fileindex);
if (err) {
if (fd != -1)
close(fd);
if (*worktree != NULL)
got_worktree_close(*worktree);
*worktree = NULL;
} else
(*worktree)->fd_fileindex = fd;
return err;
}
void
got_worktree_close(struct got_worktree *worktree)
{
free(worktree->path_worktree_root);
free(worktree->path_repo);
free(worktree->path_prefix);
if (worktree->fd_fileindex != -1)
close(worktree->fd_fileindex);
free(worktree);
}
char *
got_worktree_get_repo_path(struct got_worktree *worktree)
{
return strdup(worktree->path_repo);
}
struct got_reference *
got_worktree_get_head(struct got_worktree *worktree)
{
return NULL;
}
const struct got_error *
got_worktree_change_head(struct got_worktree *worktree, struct got_reference *head,
struct got_repository *repo)
{
return NULL;
}
const struct got_error *
got_worktree_checkout_files(struct got_worktree *worktree,
struct got_repository *repo)
{
return NULL;
}