* We optimistically assume that the directories exist, * which is true 99% of the time anyway. If they don't, * we create them and try again. * * Returns: * -1 on error * 0 otherwise */
| 4547 | * 0 otherwise |
| 4548 | */ |
| 4549 | static int create_one_file(struct apply_state *state, |
| 4550 | char *path, |
| 4551 | unsigned mode, |
| 4552 | const char *buf, |
| 4553 | unsigned long size) |
| 4554 | { |
| 4555 | char *newpath = NULL; |
| 4556 | int res; |
| 4557 | |
| 4558 | if (state->cached) |
| 4559 | return 0; |
| 4560 | |
| 4561 | /* |
| 4562 | * We already try to detect whether files are beyond a symlink in our |
| 4563 | * up-front checks. But in the case where symlinks are created by any |
| 4564 | * of the intermediate hunks it can happen that our up-front checks |
| 4565 | * didn't yet see the symlink, but at the point of arriving here there |
| 4566 | * in fact is one. We thus repeat the check for symlinks here. |
| 4567 | * |
| 4568 | * Note that this does not make the up-front check obsolete as the |
| 4569 | * failure mode is different: |
| 4570 | * |
| 4571 | * - The up-front checks cause us to abort before we have written |
| 4572 | * anything into the working directory. So when we exit this way the |
| 4573 | * working directory remains clean. |
| 4574 | * |
| 4575 | * - The checks here happen in the middle of the action where we have |
| 4576 | * already started to apply the patch. The end result will be a dirty |
| 4577 | * working directory. |
| 4578 | * |
| 4579 | * Ideally, we should update the up-front checks to catch what would |
| 4580 | * happen when we apply the patch before we damage the working tree. |
| 4581 | * We have all the information necessary to do so. But for now, as a |
| 4582 | * part of embargoed security work, having this check would serve as a |
| 4583 | * reasonable first step. |
| 4584 | */ |
| 4585 | if (path_is_beyond_symlink(state, path)) |
| 4586 | return error(_("affected file '%s' is beyond a symbolic link"), path); |
| 4587 | |
| 4588 | res = try_create_file(state, path, mode, buf, size); |
| 4589 | if (res < 0) |
| 4590 | return -1; |
| 4591 | if (!res) |
| 4592 | return 0; |
| 4593 | |
| 4594 | if (errno == ENOENT) { |
| 4595 | if (safe_create_leading_directories_no_share(path)) |
| 4596 | return 0; |
| 4597 | res = try_create_file(state, path, mode, buf, size); |
| 4598 | if (res < 0) |
| 4599 | return -1; |
| 4600 | if (!res) |
| 4601 | return 0; |
| 4602 | } |
| 4603 | |
| 4604 | if (errno == EEXIST || errno == EACCES) { |
| 4605 | /* We may be trying to create a file where a directory |
| 4606 | * used to be. |
no test coverage detected