| 3905 | } |
| 3906 | |
| 3907 | __attribute__((format (printf, 2, 3))) |
| 3908 | static int safe_append(const char *filename, const char *fmt, ...) |
| 3909 | { |
| 3910 | va_list ap; |
| 3911 | struct lock_file lock = LOCK_INIT; |
| 3912 | int fd = hold_lock_file_for_update(&lock, filename, |
| 3913 | LOCK_REPORT_ON_ERROR); |
| 3914 | struct strbuf buf = STRBUF_INIT; |
| 3915 | |
| 3916 | if (fd < 0) |
| 3917 | return -1; |
| 3918 | |
| 3919 | if (strbuf_read_file(&buf, filename, 0) < 0 && errno != ENOENT) { |
| 3920 | error_errno(_("could not read '%s'"), filename); |
| 3921 | rollback_lock_file(&lock); |
| 3922 | return -1; |
| 3923 | } |
| 3924 | strbuf_complete(&buf, '\n'); |
| 3925 | va_start(ap, fmt); |
| 3926 | strbuf_vaddf(&buf, fmt, ap); |
| 3927 | va_end(ap); |
| 3928 | |
| 3929 | if (write_in_full(fd, buf.buf, buf.len) < 0) { |
| 3930 | error_errno(_("could not write to '%s'"), filename); |
| 3931 | strbuf_release(&buf); |
| 3932 | rollback_lock_file(&lock); |
| 3933 | return -1; |
| 3934 | } |
| 3935 | if (commit_lock_file(&lock) < 0) { |
| 3936 | strbuf_release(&buf); |
| 3937 | return error(_("failed to finalize '%s'"), filename); |
| 3938 | } |
| 3939 | |
| 3940 | strbuf_release(&buf); |
| 3941 | return 0; |
| 3942 | } |
| 3943 | |
| 3944 | static int do_label(struct repository *r, const char *name, int len) |
| 3945 | { |
no test coverage detected