Disable the new test, which covers only symbolic links.
[git/mingw/4msysgit/wingit-dll.git] / config.c
blob1c87e8e3f941036a1f73e7924462136d83f15fa9
1 /*
2 * GIT - The information manager from hell
4 * Copyright (C) Linus Torvalds, 2005
5 * Copyright (C) Johannes Schindelin, 2005
7 */
8 #include "cache.h"
9 #include "exec_cmd.h"
11 #define MAXNAME (256)
13 static FILE *config_file;
14 static const char *config_file_name;
15 static int config_linenr;
16 static int get_next_char(void)
18 int c;
19 FILE *f;
21 c = '\n';
22 if ((f = config_file) != NULL) {
23 c = fgetc(f);
24 if (c == '\r') {
25 /* DOS like systems */
26 c = fgetc(f);
27 if (c != '\n') {
28 ungetc(c, f);
29 c = '\r';
32 if (c == '\n')
33 config_linenr++;
34 if (c == EOF) {
35 config_file = NULL;
36 c = '\n';
39 return c;
42 static char *parse_value(void)
44 static char value[1024];
45 int quote = 0, comment = 0, len = 0, space = 0;
47 for (;;) {
48 int c = get_next_char();
49 if (len >= sizeof(value))
50 return NULL;
51 if (c == '\n') {
52 if (quote)
53 return NULL;
54 value[len] = 0;
55 return value;
57 if (comment)
58 continue;
59 if (isspace(c) && !quote) {
60 space = 1;
61 continue;
63 if (!quote) {
64 if (c == ';' || c == '#') {
65 comment = 1;
66 continue;
69 if (space) {
70 if (len)
71 value[len++] = ' ';
72 space = 0;
74 if (c == '\\') {
75 c = get_next_char();
76 switch (c) {
77 case '\n':
78 continue;
79 case 't':
80 c = '\t';
81 break;
82 case 'b':
83 c = '\b';
84 break;
85 case 'n':
86 c = '\n';
87 break;
88 /* Some characters escape as themselves */
89 case '\\': case '"':
90 break;
91 /* Reject unknown escape sequences */
92 default:
93 return NULL;
95 value[len++] = c;
96 continue;
98 if (c == '"') {
99 quote = 1-quote;
100 continue;
102 value[len++] = c;
106 static inline int iskeychar(int c)
108 return isalnum(c) || c == '-';
111 static int get_value(config_fn_t fn, char *name, unsigned int len)
113 int c;
114 char *value;
116 /* Get the full name */
117 for (;;) {
118 c = get_next_char();
119 if (c == EOF)
120 break;
121 if (!iskeychar(c))
122 break;
123 name[len++] = tolower(c);
124 if (len >= MAXNAME)
125 return -1;
127 name[len] = 0;
128 while (c == ' ' || c == '\t')
129 c = get_next_char();
131 value = NULL;
132 if (c != '\n') {
133 if (c != '=')
134 return -1;
135 value = parse_value();
136 if (!value)
137 return -1;
139 return fn(name, value);
142 static int get_extended_base_var(char *name, int baselen, int c)
144 do {
145 if (c == '\n')
146 return -1;
147 c = get_next_char();
148 } while (isspace(c));
150 /* We require the format to be '[base "extension"]' */
151 if (c != '"')
152 return -1;
153 name[baselen++] = '.';
155 for (;;) {
156 int c = get_next_char();
157 if (c == '\n')
158 return -1;
159 if (c == '"')
160 break;
161 if (c == '\\') {
162 c = get_next_char();
163 if (c == '\n')
164 return -1;
166 name[baselen++] = c;
167 if (baselen > MAXNAME / 2)
168 return -1;
171 /* Final ']' */
172 if (get_next_char() != ']')
173 return -1;
174 return baselen;
177 static int get_base_var(char *name)
179 int baselen = 0;
181 for (;;) {
182 int c = get_next_char();
183 if (c == EOF)
184 return -1;
185 if (c == ']')
186 return baselen;
187 if (isspace(c))
188 return get_extended_base_var(name, baselen, c);
189 if (!iskeychar(c) && c != '.')
190 return -1;
191 if (baselen > MAXNAME / 2)
192 return -1;
193 name[baselen++] = tolower(c);
197 static int git_parse_file(config_fn_t fn)
199 int comment = 0;
200 int baselen = 0;
201 static char var[MAXNAME];
203 for (;;) {
204 int c = get_next_char();
205 if (c == '\n') {
206 /* EOF? */
207 if (!config_file)
208 return 0;
209 comment = 0;
210 continue;
212 if (comment || isspace(c))
213 continue;
214 if (c == '#' || c == ';') {
215 comment = 1;
216 continue;
218 if (c == '[') {
219 baselen = get_base_var(var);
220 if (baselen <= 0)
221 break;
222 var[baselen++] = '.';
223 var[baselen] = 0;
224 continue;
226 if (!isalpha(c))
227 break;
228 var[baselen] = tolower(c);
229 if (get_value(fn, var, baselen+1) < 0)
230 break;
232 die("bad config file line %d in %s", config_linenr, config_file_name);
235 int git_config_int(const char *name, const char *value)
237 if (value && *value) {
238 char *end;
239 int val = strtol(value, &end, 0);
240 if (!*end)
241 return val;
242 if (!strcasecmp(end, "k"))
243 return val * 1024;
244 if (!strcasecmp(end, "m"))
245 return val * 1024 * 1024;
246 if (!strcasecmp(end, "g"))
247 return val * 1024 * 1024 * 1024;
249 die("bad config value for '%s' in %s", name, config_file_name);
252 int git_config_bool(const char *name, const char *value)
254 if (!value)
255 return 1;
256 if (!*value)
257 return 0;
258 if (!strcasecmp(value, "true") || !strcasecmp(value, "yes"))
259 return 1;
260 if (!strcasecmp(value, "false") || !strcasecmp(value, "no"))
261 return 0;
262 return git_config_int(name, value) != 0;
265 int git_default_config(const char *var, const char *value)
267 /* This needs a better name */
268 if (!strcmp(var, "core.filemode")) {
269 trust_executable_bit = git_config_bool(var, value);
270 return 0;
273 if (!strcmp(var, "core.symlinks")) {
274 has_symlinks = git_config_bool(var, value);
275 return 0;
278 if (!strcmp(var, "core.bare")) {
279 is_bare_repository_cfg = git_config_bool(var, value);
280 return 0;
283 if (!strcmp(var, "core.ignorestat")) {
284 assume_unchanged = git_config_bool(var, value);
285 return 0;
288 if (!strcmp(var, "core.prefersymlinkrefs")) {
289 prefer_symlink_refs = git_config_bool(var, value);
290 return 0;
293 if (!strcmp(var, "core.logallrefupdates")) {
294 log_all_ref_updates = git_config_bool(var, value);
295 return 0;
298 if (!strcmp(var, "core.warnambiguousrefs")) {
299 warn_ambiguous_refs = git_config_bool(var, value);
300 return 0;
303 if (!strcmp(var, "core.legacyheaders")) {
304 use_legacy_headers = git_config_bool(var, value);
305 return 0;
308 if (!strcmp(var, "core.compression")) {
309 int level = git_config_int(var, value);
310 if (level == -1)
311 level = Z_DEFAULT_COMPRESSION;
312 else if (level < 0 || level > Z_BEST_COMPRESSION)
313 die("bad zlib compression level %d", level);
314 zlib_compression_level = level;
315 return 0;
318 if (!strcmp(var, "core.packedgitwindowsize")) {
319 int pgsz_x2 = getpagesize() * 2;
320 packed_git_window_size = git_config_int(var, value);
322 /* This value must be multiple of (pagesize * 2) */
323 packed_git_window_size /= pgsz_x2;
324 if (packed_git_window_size < 1)
325 packed_git_window_size = 1;
326 packed_git_window_size *= pgsz_x2;
327 return 0;
330 if (!strcmp(var, "core.packedgitlimit")) {
331 packed_git_limit = git_config_int(var, value);
332 return 0;
335 if (!strcmp(var, "core.deltabasecachelimit")) {
336 delta_base_cache_limit = git_config_int(var, value);
337 return 0;
340 if (!strcmp(var, "core.autocrlf")) {
341 if (value && !strcasecmp(value, "input")) {
342 auto_crlf = -1;
343 return 0;
345 auto_crlf = git_config_bool(var, value);
346 return 0;
349 if (!strcmp(var, "user.name")) {
350 strlcpy(git_default_name, value, sizeof(git_default_name));
351 return 0;
354 if (!strcmp(var, "user.email")) {
355 strlcpy(git_default_email, value, sizeof(git_default_email));
356 return 0;
359 if (!strcmp(var, "i18n.commitencoding")) {
360 git_commit_encoding = xstrdup(value);
361 return 0;
364 if (!strcmp(var, "i18n.logoutputencoding")) {
365 git_log_output_encoding = xstrdup(value);
366 return 0;
370 if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) {
371 pager_use_color = git_config_bool(var,value);
372 return 0;
375 /* Add other config variables here and to Documentation/config.txt. */
376 return 0;
379 int git_config_from_file(config_fn_t fn, const char *filename)
381 int ret;
382 FILE *f = fopen(filename, "r");
384 ret = -1;
385 if (f) {
386 config_file = f;
387 config_file_name = filename;
388 config_linenr = 1;
389 ret = git_parse_file(fn);
390 fclose(f);
391 config_file_name = NULL;
393 return ret;
396 const char *git_etc_gitconfig(void)
398 static const char *system_wide;
399 if (!system_wide) {
400 system_wide = ETC_GITCONFIG;
401 /* interpret path relative to exec-dir */
402 if (system_wide[0] != '/' && system_wide[1] != ':') {
403 const char *exec_path = git_exec_path();
404 system_wide = prefix_path(exec_path, strlen(exec_path),
405 system_wide);
408 return system_wide;
411 int git_config(config_fn_t fn)
413 int ret = 0;
414 char *repo_config = NULL;
415 const char *home = NULL, *filename;
417 /* $GIT_CONFIG makes git read _only_ the given config file,
418 * $GIT_CONFIG_LOCAL will make it process it in addition to the
419 * global config file, the same way it would the per-repository
420 * config file otherwise. */
421 filename = getenv(CONFIG_ENVIRONMENT);
422 if (!filename) {
423 if (!access(git_etc_gitconfig(), R_OK))
424 ret += git_config_from_file(fn, git_etc_gitconfig());
425 home = getenv("HOME");
426 filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
427 if (!filename)
428 filename = repo_config = xstrdup(git_path("config"));
431 if (home) {
432 char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
433 if (!access(user_config, R_OK))
434 ret = git_config_from_file(fn, user_config);
435 free(user_config);
438 ret += git_config_from_file(fn, filename);
439 free(repo_config);
440 return ret;
444 * Find all the stuff for git_config_set() below.
447 #define MAX_MATCHES 512
449 static struct {
450 int baselen;
451 char* key;
452 int do_not_match;
453 regex_t* value_regex;
454 int multi_replace;
455 size_t offset[MAX_MATCHES];
456 enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
457 int seen;
458 } store;
460 static int matches(const char* key, const char* value)
462 return !strcmp(key, store.key) &&
463 (store.value_regex == NULL ||
464 (store.do_not_match ^
465 !regexec(store.value_regex, value, 0, NULL, 0)));
468 static int store_aux(const char* key, const char* value)
470 const char *ep;
471 size_t section_len;
473 switch (store.state) {
474 case KEY_SEEN:
475 if (matches(key, value)) {
476 if (store.seen == 1 && store.multi_replace == 0) {
477 fprintf(stderr,
478 "Warning: %s has multiple values\n",
479 key);
480 } else if (store.seen >= MAX_MATCHES) {
481 fprintf(stderr, "Too many matches\n");
482 return 1;
485 store.offset[store.seen] = ftell(config_file);
486 store.seen++;
488 break;
489 case SECTION_SEEN:
491 * What we are looking for is in store.key (both
492 * section and var), and its section part is baselen
493 * long. We found key (again, both section and var).
494 * We would want to know if this key is in the same
495 * section as what we are looking for. We already
496 * know we are in the same section as what should
497 * hold store.key.
499 ep = strrchr(key, '.');
500 section_len = ep - key;
502 if ((section_len != store.baselen) ||
503 memcmp(key, store.key, section_len+1)) {
504 store.state = SECTION_END_SEEN;
505 break;
509 * Do not increment matches: this is no match, but we
510 * just made sure we are in the desired section.
512 store.offset[store.seen] = ftell(config_file);
513 /* fallthru */
514 case SECTION_END_SEEN:
515 case START:
516 if (matches(key, value)) {
517 store.offset[store.seen] = ftell(config_file);
518 store.state = KEY_SEEN;
519 store.seen++;
520 } else {
521 if (strrchr(key, '.') - key == store.baselen &&
522 !strncmp(key, store.key, store.baselen)) {
523 store.state = SECTION_SEEN;
524 store.offset[store.seen] = ftell(config_file);
528 return 0;
531 static int write_error()
533 fprintf(stderr, "Failed to write new configuration file\n");
535 /* Same error code as "failed to rename". */
536 return 4;
539 static int store_write_section(int fd, const char* key)
541 const char *dot = strchr(key, '.');
542 int len1 = store.baselen, len2 = -1;
544 dot = strchr(key, '.');
545 if (dot) {
546 int dotlen = dot - key;
547 if (dotlen < len1) {
548 len2 = len1 - dotlen - 1;
549 len1 = dotlen;
553 if (write_in_full(fd, "[", 1) != 1 ||
554 write_in_full(fd, key, len1) != len1)
555 return 0;
556 if (len2 >= 0) {
557 if (write_in_full(fd, " \"", 2) != 2)
558 return 0;
559 while (--len2 >= 0) {
560 unsigned char c = *++dot;
561 if (c == '"')
562 if (write_in_full(fd, "\\", 1) != 1)
563 return 0;
564 if (write_in_full(fd, &c, 1) != 1)
565 return 0;
567 if (write_in_full(fd, "\"", 1) != 1)
568 return 0;
570 if (write_in_full(fd, "]\n", 2) != 2)
571 return 0;
573 return 1;
576 static int store_write_pair(int fd, const char* key, const char* value)
578 int i;
579 int length = strlen(key+store.baselen+1);
580 int quote = 0;
582 /* Check to see if the value needs to be quoted. */
583 if (value[0] == ' ')
584 quote = 1;
585 for (i = 0; value[i]; i++)
586 if (value[i] == ';' || value[i] == '#')
587 quote = 1;
588 if (value[i-1] == ' ')
589 quote = 1;
591 if (write_in_full(fd, "\t", 1) != 1 ||
592 write_in_full(fd, key+store.baselen+1, length) != length ||
593 write_in_full(fd, " = ", 3) != 3)
594 return 0;
595 if (quote && write_in_full(fd, "\"", 1) != 1)
596 return 0;
597 for (i = 0; value[i]; i++)
598 switch (value[i]) {
599 case '\n':
600 if (write_in_full(fd, "\\n", 2) != 2)
601 return 0;
602 break;
603 case '\t':
604 if (write_in_full(fd, "\\t", 2) != 2)
605 return 0;
606 break;
607 case '"':
608 case '\\':
609 if (write_in_full(fd, "\\", 1) != 1)
610 return 0;
611 default:
612 if (write_in_full(fd, value+i, 1) != 1)
613 return 0;
614 break;
616 if (quote && write_in_full(fd, "\"", 1) != 1)
617 return 0;
618 if (write_in_full(fd, "\n", 1) != 1)
619 return 0;
620 return 1;
623 static ssize_t find_beginning_of_line(const char* contents, size_t size,
624 size_t offset_, int* found_bracket)
626 size_t equal_offset = size, bracket_offset = size;
627 ssize_t offset;
629 for (offset = offset_-2; offset > 0
630 && contents[offset] != '\n'; offset--)
631 switch (contents[offset]) {
632 case '=': equal_offset = offset; break;
633 case ']': bracket_offset = offset; break;
635 if (bracket_offset < equal_offset) {
636 *found_bracket = 1;
637 offset = bracket_offset+1;
638 } else
639 offset++;
641 return offset;
644 int git_config_set(const char* key, const char* value)
646 return git_config_set_multivar(key, value, NULL, 0);
650 * If value==NULL, unset in (remove from) config,
651 * if value_regex!=NULL, disregard key/value pairs where value does not match.
652 * if multi_replace==0, nothing, or only one matching key/value is replaced,
653 * else all matching key/values (regardless how many) are removed,
654 * before the new pair is written.
656 * Returns 0 on success.
658 * This function does this:
660 * - it locks the config file by creating ".git/config.lock"
662 * - it then parses the config using store_aux() as validator to find
663 * the position on the key/value pair to replace. If it is to be unset,
664 * it must be found exactly once.
666 * - the config file is mmap()ed and the part before the match (if any) is
667 * written to the lock file, then the changed part and the rest.
669 * - the config file is removed and the lock file rename()d to it.
672 int git_config_set_multivar(const char* key, const char* value,
673 const char* value_regex, int multi_replace)
675 int i, dot;
676 int fd = -1, in_fd;
677 int ret;
678 char* config_filename;
679 char* lock_file;
680 const char* last_dot = strrchr(key, '.');
682 config_filename = getenv(CONFIG_ENVIRONMENT);
683 if (!config_filename) {
684 config_filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
685 if (!config_filename)
686 config_filename = git_path("config");
688 config_filename = xstrdup(config_filename);
689 lock_file = xstrdup(mkpath("%s.lock", config_filename));
692 * Since "key" actually contains the section name and the real
693 * key name separated by a dot, we have to know where the dot is.
696 if (last_dot == NULL) {
697 fprintf(stderr, "key does not contain a section: %s\n", key);
698 ret = 2;
699 goto out_free;
701 store.baselen = last_dot - key;
703 store.multi_replace = multi_replace;
706 * Validate the key and while at it, lower case it for matching.
708 store.key = xmalloc(strlen(key) + 1);
709 dot = 0;
710 for (i = 0; key[i]; i++) {
711 unsigned char c = key[i];
712 if (c == '.')
713 dot = 1;
714 /* Leave the extended basename untouched.. */
715 if (!dot || i > store.baselen) {
716 if (!iskeychar(c) || (i == store.baselen+1 && !isalpha(c))) {
717 fprintf(stderr, "invalid key: %s\n", key);
718 free(store.key);
719 ret = 1;
720 goto out_free;
722 c = tolower(c);
723 } else if (c == '\n') {
724 fprintf(stderr, "invalid key (newline): %s\n", key);
725 free(store.key);
726 ret = 1;
727 goto out_free;
729 store.key[i] = c;
731 store.key[i] = 0;
734 * The lock_file serves a purpose in addition to locking: the new
735 * contents of .git/config will be written into it.
737 fd = open(lock_file, O_WRONLY | O_CREAT | O_EXCL, 0666);
738 if (fd < 0 || adjust_shared_perm(lock_file)) {
739 fprintf(stderr, "could not lock config file\n");
740 free(store.key);
741 ret = -1;
742 goto out_free;
746 * If .git/config does not exist yet, write a minimal version.
748 in_fd = open(config_filename, O_RDONLY);
749 if ( in_fd < 0 ) {
750 free(store.key);
752 if ( ENOENT != errno ) {
753 error("opening %s: %s", config_filename,
754 strerror(errno));
755 ret = 3; /* same as "invalid config file" */
756 goto out_free;
758 /* if nothing to unset, error out */
759 if (value == NULL) {
760 ret = 5;
761 goto out_free;
764 store.key = (char*)key;
765 if (!store_write_section(fd, key) ||
766 !store_write_pair(fd, key, value))
767 goto write_err_out;
768 } else {
769 struct stat st;
770 char* contents;
771 size_t contents_sz, copy_begin, copy_end;
772 int i, new_line = 0;
774 if (value_regex == NULL)
775 store.value_regex = NULL;
776 else {
777 if (value_regex[0] == '!') {
778 store.do_not_match = 1;
779 value_regex++;
780 } else
781 store.do_not_match = 0;
783 store.value_regex = (regex_t*)xmalloc(sizeof(regex_t));
784 if (regcomp(store.value_regex, value_regex,
785 REG_EXTENDED)) {
786 fprintf(stderr, "Invalid pattern: %s\n",
787 value_regex);
788 free(store.value_regex);
789 ret = 6;
790 goto out_free;
794 store.offset[0] = 0;
795 store.state = START;
796 store.seen = 0;
799 * After this, store.offset will contain the *end* offset
800 * of the last match, or remain at 0 if no match was found.
801 * As a side effect, we make sure to transform only a valid
802 * existing config file.
804 if (git_config_from_file(store_aux, config_filename)) {
805 fprintf(stderr, "invalid config file\n");
806 free(store.key);
807 if (store.value_regex != NULL) {
808 regfree(store.value_regex);
809 free(store.value_regex);
811 ret = 3;
812 goto out_free;
815 free(store.key);
816 if (store.value_regex != NULL) {
817 regfree(store.value_regex);
818 free(store.value_regex);
821 /* if nothing to unset, or too many matches, error out */
822 if ((store.seen == 0 && value == NULL) ||
823 (store.seen > 1 && multi_replace == 0)) {
824 ret = 5;
825 goto out_free;
828 fstat(in_fd, &st);
829 contents_sz = xsize_t(st.st_size);
830 contents = xmmap(NULL, contents_sz, PROT_READ,
831 MAP_PRIVATE, in_fd, 0);
832 close(in_fd);
834 if (store.seen == 0)
835 store.seen = 1;
837 for (i = 0, copy_begin = 0; i < store.seen; i++) {
838 if (store.offset[i] == 0) {
839 store.offset[i] = copy_end = contents_sz;
840 } else if (store.state != KEY_SEEN) {
841 copy_end = store.offset[i];
842 } else
843 copy_end = find_beginning_of_line(
844 contents, contents_sz,
845 store.offset[i]-2, &new_line);
847 /* write the first part of the config */
848 if (copy_end > copy_begin) {
849 if (write_in_full(fd, contents + copy_begin,
850 copy_end - copy_begin) <
851 copy_end - copy_begin)
852 goto write_err_out;
853 if (new_line &&
854 write_in_full(fd, "\n", 1) != 1)
855 goto write_err_out;
857 copy_begin = store.offset[i];
860 /* write the pair (value == NULL means unset) */
861 if (value != NULL) {
862 if (store.state == START) {
863 if (!store_write_section(fd, key))
864 goto write_err_out;
866 if (!store_write_pair(fd, key, value))
867 goto write_err_out;
870 /* write the rest of the config */
871 if (copy_begin < contents_sz)
872 if (write_in_full(fd, contents + copy_begin,
873 contents_sz - copy_begin) <
874 contents_sz - copy_begin)
875 goto write_err_out;
877 munmap(contents, contents_sz);
878 unlink(config_filename);
881 close(fd);
882 fd = -1;
883 if (rename(lock_file, config_filename) < 0) {
884 fprintf(stderr, "Could not rename the lock file?\n");
885 ret = 4;
886 goto out_free;
889 ret = 0;
891 out_free:
892 if (0 <= fd)
893 close(fd);
894 free(config_filename);
895 if (lock_file) {
896 unlink(lock_file);
897 free(lock_file);
899 return ret;
901 write_err_out:
902 ret = write_error();
903 goto out_free;
907 static int section_name_match (const char *buf, const char *name)
909 int i = 0, j = 0, dot = 0;
910 for (; buf[i] && buf[i] != ']'; i++) {
911 if (!dot && isspace(buf[i])) {
912 dot = 1;
913 if (name[j++] != '.')
914 break;
915 for (i++; isspace(buf[i]); i++)
916 ; /* do nothing */
917 if (buf[i] != '"')
918 break;
919 continue;
921 if (buf[i] == '\\' && dot)
922 i++;
923 else if (buf[i] == '"' && dot) {
924 for (i++; isspace(buf[i]); i++)
925 ; /* do_nothing */
926 break;
928 if (buf[i] != name[j++])
929 break;
931 return (buf[i] == ']' && name[j] == 0);
934 /* if new_name == NULL, the section is removed instead */
935 int git_config_rename_section(const char *old_name, const char *new_name)
937 int ret = 0, remove = 0;
938 char *config_filename;
939 struct lock_file *lock = xcalloc(sizeof(struct lock_file), 1);
940 int out_fd;
941 char buf[1024];
943 config_filename = getenv(CONFIG_ENVIRONMENT);
944 if (!config_filename) {
945 config_filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
946 if (!config_filename)
947 config_filename = git_path("config");
949 config_filename = xstrdup(config_filename);
950 out_fd = hold_lock_file_for_update(lock, config_filename, 0);
951 if (out_fd < 0) {
952 ret = error("Could not lock config file!");
953 goto out;
956 if (!(config_file = fopen(config_filename, "rb"))) {
957 /* no config file means nothing to rename, no error */
958 goto unlock_and_out;
961 while (fgets(buf, sizeof(buf), config_file)) {
962 int i;
963 int length;
964 for (i = 0; buf[i] && isspace(buf[i]); i++)
965 ; /* do nothing */
966 if (buf[i] == '[') {
967 /* it's a section */
968 if (section_name_match (&buf[i+1], old_name)) {
969 ret++;
970 if (new_name == NULL) {
971 remove = 1;
972 continue;
974 store.baselen = strlen(new_name);
975 if (!store_write_section(out_fd, new_name)) {
976 ret = write_error();
977 goto out;
979 continue;
981 remove = 0;
983 if (remove)
984 continue;
985 length = strlen(buf);
986 if (write_in_full(out_fd, buf, length) != length) {
987 ret = write_error();
988 goto out;
991 fclose(config_file);
992 unlock_and_out:
993 if (close(out_fd) || commit_lock_file(lock) < 0)
994 ret = error("Cannot commit config file!");
995 out:
996 free(config_filename);
997 return ret;