MCPcopy Create free account
hub / github.com/git/git / try_create_file

Function try_create_file

apply.c:4500–4538  ·  view source on GitHub ↗

* Returns: * -1 if an unrecoverable error happened * 0 if everything went well * 1 if a recoverable error happened */

Source from the content-addressed store, hash-verified

4498 * 1 if a recoverable error happened
4499 */
4500static int try_create_file(struct apply_state *state, const char *path,
4501 unsigned int mode, const char *buf,
4502 unsigned long size)
4503{
4504 int fd, res;
4505 struct strbuf nbuf = STRBUF_INIT;
4506
4507 if (S_ISGITLINK(mode)) {
4508 struct stat st;
4509 if (!lstat(path, &st) && S_ISDIR(st.st_mode))
4510 return 0;
4511 return !!mkdir(path, 0777);
4512 }
4513
4514 if (has_symlinks && S_ISLNK(mode))
4515 /* Although buf:size is counted string, it also is NUL
4516 * terminated.
4517 */
4518 return !!symlink(buf, path);
4519
4520 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);
4521 if (fd < 0)
4522 return 1;
4523
4524 if (convert_to_working_tree(state->repo->index, path, buf, size, &nbuf, NULL)) {
4525 size = nbuf.len;
4526 buf = nbuf.buf;
4527 }
4528
4529 res = write_in_full(fd, buf, size) < 0;
4530 if (res)
4531 error_errno(_("failed to write to '%s'"), path);
4532 strbuf_release(&nbuf);
4533
4534 if (close(fd) < 0 && !res)
4535 return error_errno(_("closing file '%s'"), path);
4536
4537 return res ? -1 : 0;
4538}
4539
4540/*
4541 * We optimistically assume that the directories exist,

Callers 1

create_one_fileFunction · 0.85

Calls 5

symlinkFunction · 0.85
convert_to_working_treeFunction · 0.85
write_in_fullFunction · 0.85
error_errnoFunction · 0.85
strbuf_releaseFunction · 0.85

Tested by

no test coverage detected