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.
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.)
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.
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.
27 #include "test_helpers.h"
30 int write_object_data(char *file
, void *data
, size_t len
)
35 if ((fd
= p_creat(file
, S_IREAD
| S_IWRITE
)) < 0)
37 ret
= p_write(fd
, data
, len
);
43 int write_object_files(const char *odb_dir
, object_data
*d
)
45 if (p_mkdir(odb_dir
, GIT_OBJECT_DIR_MODE
) < 0) {
47 fprintf(stderr
, "can't make directory \"%s\"", odb_dir
);
49 fprintf(stderr
, " (already exists)");
50 fprintf(stderr
, "\n");
54 if ((p_mkdir(d
->dir
, GIT_OBJECT_DIR_MODE
) < 0) && (errno
!= EEXIST
)) {
55 fprintf(stderr
, "can't make object directory \"%s\"\n", d
->dir
);
58 if (write_object_data(d
->file
, d
->bytes
, d
->blen
) < 0) {
59 fprintf(stderr
, "can't write object file \"%s\"\n", d
->file
);
66 int remove_object_files(const char *odb_dir
, object_data
*d
)
68 if (p_unlink(d
->file
) < 0) {
69 fprintf(stderr
, "can't delete object file \"%s\"\n", d
->file
);
72 if ((p_rmdir(d
->dir
) < 0) && (errno
!= ENOTEMPTY
)) {
73 fprintf(stderr
, "can't remove object directory \"%s\"\n", d
->dir
);
77 if (p_rmdir(odb_dir
) < 0) {
78 fprintf(stderr
, "can't remove directory \"%s\"\n", odb_dir
);
85 void locate_loose_object(const char *repository_folder
, git_object
*object
, char **out
, char **out_folder
)
87 static const char *objects_folder
= "objects/";
89 char *ptr
, *full_path
, *top_folder
;
90 int path_length
, objects_length
;
92 assert(repository_folder
&& object
);
94 objects_length
= strlen(objects_folder
);
95 path_length
= strlen(repository_folder
);
96 ptr
= full_path
= git__malloc(path_length
+ objects_length
+ GIT_OID_HEXSZ
+ 3);
98 strcpy(ptr
, repository_folder
);
99 strcpy(ptr
+ path_length
, objects_folder
);
101 ptr
= top_folder
= ptr
+ path_length
+ objects_length
;
103 git_oid_pathfmt(ptr
, git_object_id(object
));
104 ptr
+= GIT_OID_HEXSZ
+ 1;
110 *out_folder
= top_folder
;
113 int loose_object_dir_mode(const char *repository_folder
, git_object
*object
)
119 locate_loose_object(repository_folder
, object
, &object_path
, NULL
);
121 pos
= strlen(object_path
);
123 if (object_path
[pos
] == '/') {
124 object_path
[pos
] = 0;
129 assert(p_stat(object_path
, &st
) == 0);
135 int remove_loose_object(const char *repository_folder
, git_object
*object
)
137 char *full_path
, *top_folder
;
139 locate_loose_object(repository_folder
, object
, &full_path
, &top_folder
);
141 if (p_unlink(full_path
) < 0) {
142 fprintf(stderr
, "can't delete object file \"%s\"\n", full_path
);
148 if ((p_rmdir(full_path
) < 0) && (errno
!= ENOTEMPTY
)) {
149 fprintf(stderr
, "can't remove object directory \"%s\"\n", full_path
);
158 int cmp_objects(git_rawobj
*o
, object_data
*d
)
160 if (o
->type
!= git_object_string2type(d
->type
))
162 if (o
->len
!= d
->dlen
)
164 if ((o
->len
> 0) && (memcmp(o
->data
, d
->data
, o
->len
) != 0))
169 int copy_file(const char *src
, const char *dst
)
171 git_fbuffer source_buf
;
173 int error
= GIT_ERROR
;
175 if (git_futils_readbuffer(&source_buf
, src
) < GIT_SUCCESS
)
176 return GIT_ENOTFOUND
;
178 dst_fd
= git_futils_creat_withpath(dst
, 0777, 0644);
182 error
= p_write(dst_fd
, source_buf
.data
, source_buf
.len
);
185 git_futils_freebuffer(&source_buf
);
191 int cmp_files(const char *a
, const char *b
)
193 git_fbuffer buf_a
, buf_b
;
194 int error
= GIT_ERROR
;
196 if (git_futils_readbuffer(&buf_a
, a
) < GIT_SUCCESS
)
199 if (git_futils_readbuffer(&buf_b
, b
) < GIT_SUCCESS
) {
200 git_futils_freebuffer(&buf_a
);
204 if (buf_a
.len
== buf_b
.len
&& !memcmp(buf_a
.data
, buf_b
.data
, buf_a
.len
))
207 git_futils_freebuffer(&buf_a
);
208 git_futils_freebuffer(&buf_b
);
214 size_t src_len
, dst_len
;
218 static int copy_filesystem_element_recurs(void *_data
, char *source
)
220 copydir_data
*data
= (copydir_data
*)_data
;
222 data
->dst
[data
->dst_len
] = 0;
223 git_path_join(data
->dst
, data
->dst
, source
+ data
->src_len
);
225 if (git_futils_isdir(source
) == GIT_SUCCESS
)
226 return git_futils_direach(source
, GIT_PATH_MAX
, copy_filesystem_element_recurs
, _data
);
228 return copy_file(source
, data
->dst
);
231 int copydir_recurs(const char *source_directory_path
, const char *destination_directory_path
)
233 char source_buffer
[GIT_PATH_MAX
];
234 char dest_buffer
[GIT_PATH_MAX
];
237 /* Source has to exist, Destination hast to _not_ exist */
238 if (git_futils_isdir(source_directory_path
) != GIT_SUCCESS
||
239 git_futils_isdir(destination_directory_path
) == GIT_SUCCESS
)
240 return GIT_EINVALIDPATH
;
242 git_path_join(source_buffer
, source_directory_path
, "");
243 data
.src_len
= strlen(source_buffer
);
245 git_path_join(dest_buffer
, destination_directory_path
, "");
246 data
.dst
= dest_buffer
;
247 data
.dst_len
= strlen(dest_buffer
);
249 return copy_filesystem_element_recurs(&data
, source_buffer
);
252 int open_temp_repo(git_repository
**repo
, const char *path
)
254 if (copydir_recurs(path
, TEMP_REPO_FOLDER
) < GIT_SUCCESS
) {
255 printf("\nFailed to create temporary folder. Aborting test suite.\n");
259 return git_repository_open(repo
, TEMP_REPO_FOLDER
);
262 void close_temp_repo(git_repository
*repo
)
264 git_repository_free(repo
);
265 if (git_futils_rmdir_r(TEMP_REPO_FOLDER
, 1) < GIT_SUCCESS
) {
266 printf("\nFailed to remove temporary folder. Aborting test suite.\n");
271 static int remove_placeholders_recurs(void *filename
, char *path
)
273 char passed_filename
[GIT_PATH_MAX
];
274 char *data
= (char *)filename
;
276 if (!git_futils_isdir(path
))
277 return git_futils_direach(path
, GIT_PATH_MAX
, remove_placeholders_recurs
, data
);
279 if (git_path_basename_r(passed_filename
, sizeof(passed_filename
), path
) < GIT_SUCCESS
)
280 return GIT_EINVALIDPATH
;
282 if (!strcmp(data
, passed_filename
))
283 return p_unlink(path
);
288 int remove_placeholders(char *directory_path
, char *filename
)
290 char buffer
[GIT_PATH_MAX
];
292 if (git_futils_isdir(directory_path
))
293 return GIT_EINVALIDPATH
;
295 strcpy(buffer
, directory_path
);
296 return remove_placeholders_recurs(filename
, buffer
);