Skip to content

Commit f0a720d

Browse files
authored
Merge pull request libgit2#5114 from pks-t/pks/bigfile-refactoring
Removal of `p_fallocate`
2 parents c3179ef + 2d85c7e commit f0a720d

File tree

5 files changed

+15
-157
lines changed

5 files changed

+15
-157
lines changed

src/posix.c

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -155,44 +155,6 @@ int p_rename(const char *from, const char *to)
155155
return -1;
156156
}
157157

158-
int p_fallocate(int fd, off_t offset, off_t len)
159-
{
160-
#if defined (__APPLE__) || (defined (__NetBSD__) && __NetBSD_Version__ < 700000000)
161-
fstore_t prealloc;
162-
struct stat st;
163-
size_t newsize;
164-
int error;
165-
166-
if ((error = p_fstat(fd, &st)) < 0)
167-
return error;
168-
169-
if (git__add_sizet_overflow(&newsize, offset, len)) {
170-
errno = EINVAL;
171-
return -1;
172-
}
173-
174-
if (newsize < (unsigned long long)st.st_size)
175-
return 0;
176-
177-
memset(&prealloc, 0, sizeof(prealloc));
178-
prealloc.fst_flags = F_ALLOCATEALL;
179-
prealloc.fst_posmode = F_PEOFPOSMODE;
180-
prealloc.fst_offset = offset;
181-
prealloc.fst_length = len;
182-
183-
/*
184-
* fcntl will often error when the file already exists; ignore
185-
* this error since ftruncate will also resize the file (although
186-
* likely slower).
187-
*/
188-
fcntl(fd, F_PREALLOCATE, &prealloc);
189-
190-
return ftruncate(fd, (offset + len));
191-
#else
192-
return posix_fallocate(fd, offset, len);
193-
#endif
194-
}
195-
196158
#endif /* GIT_WIN32 */
197159

198160
ssize_t p_read(git_file fd, void *buf, size_t cnt)

src/posix.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ extern int p_open(const char *path, int flags, ...);
115115
extern int p_creat(const char *path, mode_t mode);
116116
extern int p_getcwd(char *buffer_out, size_t size);
117117
extern int p_rename(const char *from, const char *to);
118-
extern int p_fallocate(int fd, off_t offset, off_t len);
119118

120119
extern int git__page_size(size_t *page_size);
121120
extern int git__mmap_alignment(size_t *page_size);

src/win32/posix_w32.c

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -525,58 +525,6 @@ int p_creat(const char *path, mode_t mode)
525525
return p_open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
526526
}
527527

528-
int p_fallocate(int fd, off_t offset, off_t len)
529-
{
530-
HANDLE fh = (HANDLE)_get_osfhandle(fd);
531-
LARGE_INTEGER zero, position, oldsize, newsize;
532-
size_t size;
533-
534-
if (fh == INVALID_HANDLE_VALUE) {
535-
errno = EBADF;
536-
return -1;
537-
}
538-
539-
if (offset < 0 || len <= 0) {
540-
errno = EINVAL;
541-
return -1;
542-
}
543-
544-
if (git__add_sizet_overflow(&size, offset, len)) {
545-
errno = EINVAL;
546-
return -1;
547-
}
548-
549-
zero.u.LowPart = 0;
550-
zero.u.HighPart = 0;
551-
552-
newsize.u.LowPart = (size & 0xffffffff);
553-
554-
#if (SIZE_MAX > UINT32_MAX)
555-
newsize.u.HighPart = size >> 32;
556-
#else
557-
newsize.u.HighPart = 0;
558-
#endif
559-
560-
if (!GetFileSizeEx(fh, &oldsize)) {
561-
set_errno();
562-
return -1;
563-
}
564-
565-
/* POSIX emulation: attempting to shrink the file is ignored */
566-
if (oldsize.QuadPart >= newsize.QuadPart)
567-
return 0;
568-
569-
if (!SetFilePointerEx(fh, zero, &position, FILE_CURRENT) ||
570-
!SetFilePointerEx(fh, newsize, NULL, FILE_BEGIN) ||
571-
!SetEndOfFile(fh) ||
572-
!SetFilePointerEx(fh, position, 0, FILE_BEGIN)) {
573-
set_errno();
574-
return -1;
575-
}
576-
577-
return 0;
578-
}
579-
580528
int p_utimes(const char *path, const struct p_timeval times[2])
581529
{
582530
git_win32_path wpath;

tests/core/posix.c

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ void test_core_posix__initialize(void)
3535
#endif
3636
}
3737

38-
void test_core_posix__cleanup(void)
39-
{
40-
p_unlink("fallocate_test");
41-
}
42-
4338
static bool supports_ipv6(void)
4439
{
4540
#ifdef GIT_WIN32
@@ -268,39 +263,3 @@ void test_core_posix__p_regcomp_compile_userdiff_regexps(void)
268263
cl_assert(!error);
269264
}
270265
}
271-
272-
void test_core_posix__fallocate(void)
273-
{
274-
int fd;
275-
struct stat st;
276-
277-
/* fallocate a new file succeeds */
278-
cl_must_pass(fd = p_open("fallocate_test", O_RDWR|O_CREAT, 0666));
279-
cl_must_pass(p_fallocate(fd, 0, 42));
280-
cl_must_pass(p_fstat(fd, &st));
281-
cl_assert_equal_i(42, st.st_size);
282-
p_close(fd);
283-
284-
/* fallocate an existing file succeeds */
285-
cl_must_pass(fd = p_open("fallocate_test", O_RDWR, 0666));
286-
cl_must_pass(p_fallocate(fd, 90, 9));
287-
cl_must_pass(p_fstat(fd, &st));
288-
cl_assert_equal_i(99, st.st_size);
289-
p_close(fd);
290-
291-
/* fallocate doesn't shrink */
292-
cl_must_pass(fd = p_open("fallocate_test", O_RDWR, 0666));
293-
cl_must_pass(p_fallocate(fd, 0, 14));
294-
cl_must_pass(p_fstat(fd, &st));
295-
cl_assert_equal_i(99, st.st_size);
296-
p_close(fd);
297-
298-
/* fallocate doesn't move the cursor */
299-
cl_must_pass(fd = p_open("fallocate_test", O_RDWR, 0666));
300-
cl_must_pass(p_fallocate(fd, 0, 100));
301-
cl_assert_equal_i(0, p_lseek(fd, 0, SEEK_CUR));
302-
cl_must_pass(p_lseek(fd, 42, SEEK_SET));
303-
cl_must_pass(p_fallocate(fd, 0, 200));
304-
cl_assert_equal_i(42, p_lseek(fd, 0, SEEK_CUR));
305-
p_close(fd);
306-
}

tests/object/tree/read.c

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -79,46 +79,36 @@ void test_object_tree_read__two(void)
7979

8080
void test_object_tree_read__largefile(void)
8181
{
82-
git_reference *ref;
82+
const git_tree_entry *entry;
83+
git_index_entry ie;
8384
git_commit *commit;
85+
git_object *object;
86+
git_index *index;
8487
git_tree *tree;
8588
git_oid oid;
86-
const git_tree_entry *entry;
87-
git_object *object;
88-
git_buf file = GIT_BUF_INIT;
89-
int fd;
90-
git_index *idx;
89+
char *buf;
9190

9291
if (!cl_is_env_set("GITTEST_INVASIVE_FS_SIZE"))
9392
cl_skip();
9493

95-
cl_git_pass(git_reference_lookup(&ref, g_repo, "refs/heads/master"));
96-
cl_git_pass(git_repository_index(&idx, g_repo));
94+
cl_assert(buf = git__calloc(1, BIGFILE_SIZE));
9795

98-
cl_git_pass(git_buf_puts(&file, git_repository_workdir(g_repo)));
99-
cl_git_pass(git_buf_joinpath(&file, file.ptr, BIGFILE));
96+
memset(&ie, 0, sizeof(ie));
97+
ie.mode = GIT_FILEMODE_BLOB;
98+
ie.path = BIGFILE;
10099

101-
fd = p_open(git_buf_cstr(&file), O_CREAT|O_RDWR, 0644);
102-
cl_assert_(fd >= 0, "invalid file descriptor");
103-
104-
cl_must_pass(p_fallocate(fd, 0, BIGFILE_SIZE));
105-
cl_must_pass(p_close(fd));
106-
107-
cl_git_pass(git_index_add_bypath(idx, BIGFILE));
108-
cl_repo_commit_from_index(&oid, g_repo, NULL, 0, "bigfile");
100+
cl_git_pass(git_repository_index(&index, g_repo));
101+
cl_git_pass(git_index_add_frombuffer(index, &ie, buf, BIGFILE_SIZE));
102+
cl_repo_commit_from_index(&oid, g_repo, NULL, 0, BIGFILE);
109103

110104
cl_git_pass(git_commit_lookup(&commit, g_repo, &oid));
111105
cl_git_pass(git_commit_tree(&tree, commit));
112-
113-
entry = git_tree_entry_byname(tree, BIGFILE);
114-
cl_assert_(entry, "entry was NULL");
115-
106+
cl_assert(entry = git_tree_entry_byname(tree, BIGFILE));
116107
cl_git_pass(git_tree_entry_to_object(&object, g_repo, entry));
117108

118-
git_buf_dispose(&file);
119109
git_object_free(object);
120110
git_tree_free(tree);
121-
git_index_free(idx);
111+
git_index_free(index);
122112
git_commit_free(commit);
123-
git_reference_free(ref);
113+
git__free(buf);
124114
}

0 commit comments

Comments
 (0)