]> git.proxmox.com Git - libgit2.git/blame - src/indexer.c
Start the runner
[libgit2.git] / src / indexer.c
CommitLineData
3412391d
CMN
1/*
2 * This file is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License, version 2,
4 * as published by the Free Software Foundation.
5 *
6 * In addition to the permissions in the GNU General Public License,
7 * the authors give you unlimited permission to link the compiled
8 * version of this file into combinations with other programs,
9 * and to distribute those combinations without any restriction
10 * coming from the use of this file. (The General Public License
11 * restrictions do apply in other respects; for example, they cover
12 * modification of the file, and distribution when not linked into
13 * a combined executable.)
14 *
15 * This file is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; see the file COPYING. If not, write to
22 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
24 */
25
f23c4a66
CMN
26#include "git2/indexer.h"
27
3412391d
CMN
28#include "common.h"
29#include "pack.h"
f23c4a66 30#include "mwindow.h"
3412391d
CMN
31#include "posix.h"
32
f23c4a66
CMN
33typedef struct git_pack_indexer {
34 struct pack_file *pack;
35 git_vector objects;
36 git_vector deltas;
37 struct stat st;
38 git_indexer_stats stats;
39} git_pack_indexer;
40
3412391d
CMN
41static int parse_header(git_pack_indexer *idx)
42{
43 struct pack_header hdr;
44 int error;
45
46 /* Verify we recognize this pack file format. */
47 if ((error = p_read(idx->pack->pack_fd, &hdr, sizeof(hdr))) < GIT_SUCCESS)
48 goto cleanup;
49
50 if (hdr.hdr_signature != htonl(PACK_SIGNATURE)) {
51 error = git__throw(GIT_EOBJCORRUPTED, "Wrong pack signature");
52 goto cleanup;
53 }
54
55 if (!pack_version_ok(hdr.hdr_version)) {
56 error = git__throw(GIT_EOBJCORRUPTED, "Wrong pack version");
57 goto cleanup;
58 }
59
60 /*
61 * FIXME: At this point we have no idea how many of the are
62 * deltas, so assume all objects are both until we get a better
63 * idea
64 */
65 error = git_vector_init(&idx->objects, hdr.hdr_entries, NULL /* FIXME: probably need something */);
66 if (error < GIT_SUCCESS)
67 goto cleanup;
68
69 error = git_vector_init(&idx->deltas, hdr.hdr_entries, NULL /* FIXME: probably need something */);
70 if (error < GIT_SUCCESS)
71 goto cleanup;
72
f23c4a66
CMN
73 idx->stats.total = hdr.hdr_entries;
74
3412391d
CMN
75 return GIT_SUCCESS;
76
77cleanup:
78 git_vector_free(&idx->objects);
79 git_vector_free(&idx->deltas);
80
81 return error;
82}
83
84int git_pack_indexer_new(git_pack_indexer **out, const char *packname)
85{
86 struct git_pack_indexer *idx;
87 unsigned int namelen;
88 int ret, error;
89
90 idx = git__malloc(sizeof(struct git_pack_indexer));
91 if (idx == NULL)
92 return GIT_ENOMEM;
93
94 memset(idx, 0x0, sizeof(*idx));
95
96 namelen = strlen(packname);
97 idx->pack = git__malloc(sizeof(struct pack_file) + namelen + 1);
98 if (idx->pack == NULL)
99 goto cleanup;
100
101 memset(idx->pack, 0x0, sizeof(struct pack_file));
102 memcpy(idx->pack->pack_name, packname, namelen);
103
104 ret = p_stat(packname, &idx->st);
105 if (ret < 0) {
106 if (errno == ENOENT)
107 error = git__throw(GIT_ENOTFOUND, "Failed to stat packfile. File not found");
108 else
109 error = git__throw(GIT_EOSERR, "Failed to stat packfile.");
110
111 goto cleanup;
112 }
113
114 ret = p_open(idx->pack->pack_name, O_RDONLY);
115 if (ret < 0) {
116 error = git__throw(GIT_EOSERR, "Failed to open packfile");
117 goto cleanup;
118 }
119
120 idx->pack->pack_fd = ret;
121
122 error = parse_header(idx);
123 if (error < GIT_SUCCESS) {
124 error = git__rethrow(error, "Failed to parse packfile header");
125 goto cleanup;
126 }
127
128 *out = idx;
129
130 return GIT_SUCCESS;
131
132cleanup:
133 free(idx->pack);
134 free(idx);
135
136 return error;
137}
138
f23c4a66
CMN
139/*
140 * Create the index. Every time something interesting happens
141 * (something has been parse or resolved), the callback gets called
142 * with some stats so it can tell the user how hard we're working
143 */
144int git_pack_indexer_run(git_pack_indexer *idx, int (*cb)(const git_indexer_stats *, void *), void *data)
145{
146 git_mwindow_file *mwf = &idx->pack->mwf;
147 int error;
148
149 error = git_mwindow_file_register(mwf);
150 if (error < GIT_SUCCESS)
151 return git__rethrow(error, "Failed to register mwindow file");
152
153 /* notify early */
154 if (cb)
155 cb(&idx->stats, data);
156
157 return error;
158}
159
3412391d
CMN
160void git_pack_indexer_free(git_pack_indexer *idx)
161{
162 p_close(idx->pack->pack_fd);
163 git_vector_free(&idx->objects);
164 git_vector_free(&idx->deltas);
165 free(idx->pack);
166 free(idx);
167}