| 209 | } |
| 210 | |
| 211 | static int odb_source_files_write_alternate(struct odb_source *source, |
| 212 | const char *alternate) |
| 213 | { |
| 214 | struct lock_file lock = LOCK_INIT; |
| 215 | char *path = xstrfmt("%s/%s", source->path, "info/alternates"); |
| 216 | FILE *in, *out; |
| 217 | int found = 0; |
| 218 | int ret; |
| 219 | |
| 220 | hold_lock_file_for_update(&lock, path, LOCK_DIE_ON_ERROR); |
| 221 | out = fdopen_lock_file(&lock, "w"); |
| 222 | if (!out) { |
| 223 | ret = error_errno(_("unable to fdopen alternates lockfile")); |
| 224 | goto out; |
| 225 | } |
| 226 | |
| 227 | in = fopen(path, "r"); |
| 228 | if (in) { |
| 229 | struct strbuf line = STRBUF_INIT; |
| 230 | |
| 231 | while (strbuf_getline(&line, in) != EOF) { |
| 232 | if (!strcmp(alternate, line.buf)) { |
| 233 | found = 1; |
| 234 | break; |
| 235 | } |
| 236 | fprintf_or_die(out, "%s\n", line.buf); |
| 237 | } |
| 238 | |
| 239 | strbuf_release(&line); |
| 240 | fclose(in); |
| 241 | } else if (errno != ENOENT) { |
| 242 | ret = error_errno(_("unable to read alternates file")); |
| 243 | goto out; |
| 244 | } |
| 245 | |
| 246 | if (found) { |
| 247 | rollback_lock_file(&lock); |
| 248 | } else { |
| 249 | fprintf_or_die(out, "%s\n", alternate); |
| 250 | if (commit_lock_file(&lock)) { |
| 251 | ret = error_errno(_("unable to move new alternates file into place")); |
| 252 | goto out; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | ret = 0; |
| 257 | |
| 258 | out: |
| 259 | free(path); |
| 260 | return ret; |
| 261 | } |
| 262 | |
| 263 | struct odb_source_files *odb_source_files_new(struct object_database *odb, |
| 264 | const char *path, |
nothing calls this directly
no test coverage detected