Skip to content

Commit 2d85c7e

Browse files
committed
posix: remove p_fallocate abstraction
By now, we have repeatedly failed to provide a nice cross-platform implementation of `p_fallocate`. Recent tries to do that escalated quite fast to a set of different CMake checks, implementations, fallbacks, etc., which started to look real awkward to maintain. In fact, `p_fallocate` had only been introduced in commit 4e3949b (tests: test that largefiles can be read through the tree API, 2019-01-30) to support a test with large files, but given the maintenance costs it just seems not to be worht it. As we have removed the sole user of `p_fallocate` in the previous commit, let's drop it altogether.
1 parent 0c2d0d4 commit 2d85c7e

File tree

4 files changed

+0
-132
lines changed

4 files changed

+0
-132
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-
}

0 commit comments

Comments
 (0)