Blame


1 93658fb9 2020-03-18 stsp /*
2 93658fb9 2020-03-18 stsp * Copyright (c) 2018, 2019 Ori Bernstein <ori@eigenstate.org>
3 93658fb9 2020-03-18 stsp *
4 93658fb9 2020-03-18 stsp * Permission to use, copy, modify, and distribute this software for any
5 93658fb9 2020-03-18 stsp * purpose with or without fee is hereby granted, provided that the above
6 93658fb9 2020-03-18 stsp * copyright notice and this permission notice appear in all copies.
7 93658fb9 2020-03-18 stsp *
8 93658fb9 2020-03-18 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 93658fb9 2020-03-18 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 93658fb9 2020-03-18 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 93658fb9 2020-03-18 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 93658fb9 2020-03-18 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 93658fb9 2020-03-18 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 93658fb9 2020-03-18 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 93658fb9 2020-03-18 stsp */
16 93658fb9 2020-03-18 stsp
17 93658fb9 2020-03-18 stsp #include <sys/types.h>
18 93658fb9 2020-03-18 stsp #include <sys/stat.h>
19 93658fb9 2020-03-18 stsp #include <sys/queue.h>
20 93658fb9 2020-03-18 stsp #include <sys/uio.h>
21 93658fb9 2020-03-18 stsp #include <sys/socket.h>
22 93658fb9 2020-03-18 stsp #include <sys/wait.h>
23 93658fb9 2020-03-18 stsp #include <sys/syslimits.h>
24 93658fb9 2020-03-18 stsp #include <sys/resource.h>
25 93658fb9 2020-03-18 stsp
26 93658fb9 2020-03-18 stsp #include <errno.h>
27 93658fb9 2020-03-18 stsp #include <fcntl.h>
28 93658fb9 2020-03-18 stsp #include <stdio.h>
29 93658fb9 2020-03-18 stsp #include <stdlib.h>
30 93658fb9 2020-03-18 stsp #include <string.h>
31 93658fb9 2020-03-18 stsp #include <stdint.h>
32 93658fb9 2020-03-18 stsp #include <sha1.h>
33 93658fb9 2020-03-18 stsp #include <zlib.h>
34 93658fb9 2020-03-18 stsp #include <ctype.h>
35 93658fb9 2020-03-18 stsp #include <limits.h>
36 93658fb9 2020-03-18 stsp #include <imsg.h>
37 93658fb9 2020-03-18 stsp #include <time.h>
38 93658fb9 2020-03-18 stsp #include <uuid.h>
39 93658fb9 2020-03-18 stsp
40 93658fb9 2020-03-18 stsp #include "got_error.h"
41 93658fb9 2020-03-18 stsp #include "got_reference.h"
42 93658fb9 2020-03-18 stsp #include "got_repository.h"
43 93658fb9 2020-03-18 stsp #include "got_path.h"
44 93658fb9 2020-03-18 stsp #include "got_cancel.h"
45 93658fb9 2020-03-18 stsp #include "got_worktree.h"
46 93658fb9 2020-03-18 stsp #include "got_object.h"
47 93658fb9 2020-03-18 stsp
48 93658fb9 2020-03-18 stsp #include "got_lib_delta.h"
49 93658fb9 2020-03-18 stsp #include "got_lib_inflate.h"
50 93658fb9 2020-03-18 stsp #include "got_lib_object.h"
51 93658fb9 2020-03-18 stsp #include "got_lib_object_parse.h"
52 93658fb9 2020-03-18 stsp #include "got_lib_object_create.h"
53 93658fb9 2020-03-18 stsp #include "got_lib_pack.h"
54 93658fb9 2020-03-18 stsp #include "got_lib_sha1.h"
55 93658fb9 2020-03-18 stsp #include "got_lib_privsep.h"
56 93658fb9 2020-03-18 stsp #include "got_lib_object_cache.h"
57 93658fb9 2020-03-18 stsp #include "got_lib_repository.h"
58 93658fb9 2020-03-18 stsp
59 93658fb9 2020-03-18 stsp static int
60 93658fb9 2020-03-18 stsp hassuffix(char *base, char *suf)
61 93658fb9 2020-03-18 stsp {
62 93658fb9 2020-03-18 stsp int nb, ns;
63 93658fb9 2020-03-18 stsp
64 93658fb9 2020-03-18 stsp nb = strlen(base);
65 93658fb9 2020-03-18 stsp ns = strlen(suf);
66 93658fb9 2020-03-18 stsp if(ns <= nb && strcmp(base + (nb - ns), suf) == 0)
67 93658fb9 2020-03-18 stsp return 1;
68 93658fb9 2020-03-18 stsp return 0;
69 93658fb9 2020-03-18 stsp }
70 93658fb9 2020-03-18 stsp
71 93658fb9 2020-03-18 stsp static int
72 93658fb9 2020-03-18 stsp got_make_index_path(char *idxpath, size_t idxpathsz, char *path)
73 93658fb9 2020-03-18 stsp {
74 93658fb9 2020-03-18 stsp size_t len;
75 93658fb9 2020-03-18 stsp
76 93658fb9 2020-03-18 stsp len = strlen(path);
77 93658fb9 2020-03-18 stsp if(hassuffix(path, ".pack"))
78 93658fb9 2020-03-18 stsp len -= strlen(".pack");
79 93658fb9 2020-03-18 stsp if (strlcpy(idxpath, path, idxpathsz) >= idxpathsz)
80 93658fb9 2020-03-18 stsp return -1;
81 93658fb9 2020-03-18 stsp if (strlcpy(idxpath + len, ".idx", idxpathsz - len) >= idxpathsz - len)
82 93658fb9 2020-03-18 stsp return -1;
83 93658fb9 2020-03-18 stsp return 0;
84 93658fb9 2020-03-18 stsp }
85 93658fb9 2020-03-18 stsp
86 93658fb9 2020-03-18 stsp const struct got_error*
87 93658fb9 2020-03-18 stsp got_index_pack(char *path)
88 93658fb9 2020-03-18 stsp {
89 93658fb9 2020-03-18 stsp int packfd, idxfd;
90 93658fb9 2020-03-18 stsp char idxpath[PATH_MAX];
91 93658fb9 2020-03-18 stsp
92 93658fb9 2020-03-18 stsp got_make_index_path(idxpath, sizeof(idxpath), path);
93 93658fb9 2020-03-18 stsp printf("index path %s\n", idxpath);
94 93658fb9 2020-03-18 stsp if ((fd = open(path)) == -1)
95 93658fb9 2020-03-18 stsp return got_error_from_errno("open pack");
96 93658fb9 2020-03-18 stsp
97 93658fb9 2020-03-18 stsp pid = fork();
98 93658fb9 2020-03-18 stsp if (pid == -1)
99 93658fb9 2020-03-18 stsp return got_error_from_errno("fork");
100 93658fb9 2020-03-18 stsp else if (pid == 0)
101 93658fb9 2020-03-18 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_INDEX_PACK, ".");
102 93658fb9 2020-03-18 stsp
103 93658fb9 2020-03-18 stsp if (close(imsg_fds[1]) != 0)
104 93658fb9 2020-03-18 stsp return got_error_from_errno("close");
105 93658fb9 2020-03-18 stsp err = got_privsep_send_index_pack_req(&ibuf, fetchfd);
106 93658fb9 2020-03-18 stsp if (err != NULL)
107 93658fb9 2020-03-18 stsp return err;
108 93658fb9 2020-03-18 stsp }