Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ec50b23

Browse files
author
Edward Thomson
committed
filebuf: detect directories in our way
When creating a filebuf, detect a directory that exists in our target file location. This prevents a failure later, when we try to move the lock file to the destination.
1 parent 2d556f3 commit ec50b23

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/filebuf.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,12 @@ int git_filebuf_open(git_filebuf *file, const char *path, int flags, mode_t mode
357357
memcpy(file->path_lock, file->path_original, path_len);
358358
memcpy(file->path_lock + path_len, GIT_FILELOCK_EXTENSION, GIT_FILELOCK_EXTLENGTH);
359359

360+
if (git_path_isdir(file->path_original)) {
361+
giterr_set(GITERR_FILESYSTEM, "path '%s' is a directory", file->path_original);
362+
error = GIT_EDIRECTORY;
363+
goto cleanup;
364+
}
365+
360366
/* open the file for locking */
361367
if ((error = lock_file(file, flags, mode)) < 0)
362368
goto cleanup;

tests/core/filebuf.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,12 @@ void test_core_filebuf__hidden_file(void)
230230
git_filebuf_cleanup(&file);
231231
#endif
232232
}
233+
234+
void test_core_filebuf__detects_directory(void)
235+
{
236+
git_filebuf file = GIT_FILEBUF_INIT, fail = GIT_FILEBUF_INIT;
237+
238+
cl_must_pass(p_mkdir("foo", 0777));
239+
cl_git_fail_with(GIT_EDIRECTORY, git_filebuf_open(&file, "foo", 0, 0666));
240+
cl_must_pass(p_rmdir("foo"));
241+
}

0 commit comments

Comments
 (0)