Blame


1 c48c4a9c 2018-03-11 stsp /*
2 c48c4a9c 2018-03-11 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 c48c4a9c 2018-03-11 stsp *
4 c48c4a9c 2018-03-11 stsp * Permission to use, copy, modify, and distribute this software for any
5 c48c4a9c 2018-03-11 stsp * purpose with or without fee is hereby granted, provided that the above
6 c48c4a9c 2018-03-11 stsp * copyright notice and this permission notice appear in all copies.
7 c48c4a9c 2018-03-11 stsp *
8 c48c4a9c 2018-03-11 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 c48c4a9c 2018-03-11 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 c48c4a9c 2018-03-11 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 c48c4a9c 2018-03-11 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 c48c4a9c 2018-03-11 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 c48c4a9c 2018-03-11 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 c48c4a9c 2018-03-11 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 c48c4a9c 2018-03-11 stsp */
16 c48c4a9c 2018-03-11 stsp
17 c48c4a9c 2018-03-11 stsp #include <sys/queue.h>
18 c48c4a9c 2018-03-11 stsp #include <sys/stat.h>
19 c48c4a9c 2018-03-11 stsp
20 c48c4a9c 2018-03-11 stsp #include <stdio.h>
21 c48c4a9c 2018-03-11 stsp #include <stdlib.h>
22 c48c4a9c 2018-03-11 stsp #include <string.h>
23 c48c4a9c 2018-03-11 stsp #include <sha1.h>
24 c48c4a9c 2018-03-11 stsp
25 c48c4a9c 2018-03-11 stsp #include "got_error.h"
26 c48c4a9c 2018-03-11 stsp
27 c48c4a9c 2018-03-11 stsp #include "got_fileindex_priv.h"
28 c48c4a9c 2018-03-11 stsp
29 c48c4a9c 2018-03-11 stsp const struct got_error *
30 c48c4a9c 2018-03-11 stsp got_fileindex_entry_open(struct got_fileindex_entry **entry, const char *path,
31 c48c4a9c 2018-03-11 stsp uint8_t *blob_sha1)
32 c48c4a9c 2018-03-11 stsp {
33 c48c4a9c 2018-03-11 stsp struct stat sb;
34 c48c4a9c 2018-03-11 stsp size_t len;
35 c48c4a9c 2018-03-11 stsp
36 c48c4a9c 2018-03-11 stsp if (lstat(path, &sb) != 0)
37 c48c4a9c 2018-03-11 stsp return got_error_from_errno();
38 c48c4a9c 2018-03-11 stsp
39 c48c4a9c 2018-03-11 stsp *entry = calloc(1, sizeof(**entry));
40 c48c4a9c 2018-03-11 stsp if (*entry == NULL)
41 c48c4a9c 2018-03-11 stsp return got_error(GOT_ERR_NO_MEM);
42 c48c4a9c 2018-03-11 stsp
43 c48c4a9c 2018-03-11 stsp (*entry)->path = strdup(path);
44 c48c4a9c 2018-03-11 stsp if ((*entry)->path == NULL) {
45 c48c4a9c 2018-03-11 stsp free(*entry);
46 c48c4a9c 2018-03-11 stsp *entry = NULL;
47 c48c4a9c 2018-03-11 stsp return got_error(GOT_ERR_NO_MEM);
48 c48c4a9c 2018-03-11 stsp }
49 c48c4a9c 2018-03-11 stsp
50 c48c4a9c 2018-03-11 stsp (*entry)->ctime_sec = sb.st_ctime;
51 c48c4a9c 2018-03-11 stsp (*entry)->ctime_nsec = sb.st_ctimensec;
52 c48c4a9c 2018-03-11 stsp (*entry)->mtime_sec = sb.st_mtime;
53 c48c4a9c 2018-03-11 stsp (*entry)->mtime_nsec = sb.st_mtimensec;
54 c48c4a9c 2018-03-11 stsp (*entry)->uid = sb.st_uid;
55 c48c4a9c 2018-03-11 stsp (*entry)->gid = sb.st_gid;
56 c48c4a9c 2018-03-11 stsp (*entry)->size = (sb.st_size & 0xffffffff);
57 c48c4a9c 2018-03-11 stsp if (sb.st_mode & S_IFLNK)
58 c48c4a9c 2018-03-11 stsp (*entry)->mode = GOT_INDEX_ENTRY_MODE_SYMLINK;
59 c48c4a9c 2018-03-11 stsp else
60 c48c4a9c 2018-03-11 stsp (*entry)->mode = GOT_INDEX_ENTRY_MODE_REGULAR_FILE;
61 c48c4a9c 2018-03-11 stsp (*entry)->mode |= ((sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) <<
62 c48c4a9c 2018-03-11 stsp GOT_INDEX_ENTRY_MODE_PERMS_SHIFT);
63 c48c4a9c 2018-03-11 stsp memcpy((*entry)->blob_sha1, blob_sha1, SHA1_DIGEST_LENGTH);
64 c48c4a9c 2018-03-11 stsp len = strlen(path);
65 c48c4a9c 2018-03-11 stsp if (len > GOT_INDEX_ENTRY_F_PATH_LEN)
66 c48c4a9c 2018-03-11 stsp len = GOT_INDEX_ENTRY_F_PATH_LEN;
67 c48c4a9c 2018-03-11 stsp (*entry)->flags |= len;
68 c48c4a9c 2018-03-11 stsp
69 c48c4a9c 2018-03-11 stsp return NULL;
70 c48c4a9c 2018-03-11 stsp }
71 c48c4a9c 2018-03-11 stsp
72 c48c4a9c 2018-03-11 stsp void
73 c48c4a9c 2018-03-11 stsp got_fileindex_entry_close(struct got_fileindex_entry *entry)
74 c48c4a9c 2018-03-11 stsp {
75 c48c4a9c 2018-03-11 stsp free(entry->path);
76 c48c4a9c 2018-03-11 stsp free(entry);
77 c48c4a9c 2018-03-11 stsp }