Blob


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