short i/o: fix config updates to use write_in_full
We need to check that the writes we perform during the update of the users configuration work. Convert to using write_in_full(). Signed-off-by: Andy Whitcroft <apw@shadowen.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Andy Whitcroft committed
Jan 8, 2007 at 15:58 UTC
480c9e521bdc0b6f7e90d3901d4ffc1f57f57a5e
1 file changed
+77
-29
config.c
+77
-29
@@ -464,7 +464,15 @@ static int store_aux(const char* key, const char* value)
464
return 0;
465
}
466
467
-static void store_write_section(int fd, const char* key)
467
+static int write_error()
468
+{
469
+ fprintf(stderr, "Failed to write new configuration file\n");
470
+
471
+ /* Same error code as "failed to rename". */
472
+ return 4;
473
+}
474
+
475
+static int store_write_section(int fd, const char* key)
476
{
477
const char *dot = strchr(key, '.');
478
int len1 = store.baselen, len2 = -1;
@@ -478,37 +486,60 @@ static void store_write_section(int fd, const char* key)
486
}
487
}
488
481
- write(fd, "[", 1);
482
- write(fd, key, len1);
489
+ if (write_in_full(fd, "[", 1) != 1 ||
490
+ write_in_full(fd, key, len1) != len1)
491
+ return 0;
492
if (len2 >= 0) {
484
- write(fd, " \"", 2);
493
+ if (write_in_full(fd, " \"", 2) != 2)
494
+ return 0;
495
while (--len2 >= 0) {
496
unsigned char c = *++dot;
497
if (c == '"')
488
- write(fd, "\\", 1);
489
- write(fd, &c, 1);
498
+ if (write_in_full(fd, "\\", 1) != 1)
499
+ return 0;
500
+ if (write_in_full(fd, &c, 1) != 1)
501
+ return 0;
502
}
491
- write(fd, "\"", 1);
503
+ if (write_in_full(fd, "\"", 1) != 1)
504
+ return 0;
505
}
493
- write(fd, "]\n", 2);
506
+ if (write_in_full(fd, "]\n", 2) != 2)
507
+ return 0;
508
+
509
+ return 1;
510
}
511
496
-static void store_write_pair(int fd, const char* key, const char* value)
512
+static int store_write_pair(int fd, const char* key, const char* value)
513
{
514
int i;
515
+ int length = strlen(key+store.baselen+1);
516
500
- write(fd, "\t", 1);
501
- write(fd, key+store.baselen+1,
502
- strlen(key+store.baselen+1));
503
- write(fd, " = ", 3);
517
+ if (write_in_full(fd, "\t", 1) != 1 ||
518
+ write_in_full(fd, key+store.baselen+1, length) != length ||
519
+ write_in_full(fd, " = ", 3) != 3)
520
+ return 0;
521
for (i = 0; value[i]; i++)
522
switch (value[i]) {
506
- case '\n': write(fd, "\\n", 2); break;
507
- case '\t': write(fd, "\\t", 2); break;
508
- case '"': case '\\': write(fd, "\\", 1);
509
- default: write(fd, value+i, 1);
510
- }
511
- write(fd, "\n", 1);
523
+ case '\n':
524
+ if (write_in_full(fd, "\\n", 2) != 2)
525
+ return 0;
526
+ break;
527
+ case '\t':
528
+ if (write_in_full(fd, "\\t", 2) != 2)
529
+ return 0;
530
+ break;
531
+ case '"':
532
+ case '\\':
533
+ if (write_in_full(fd, "\\", 1) != 1)
534
+ return 0;
535
+ default:
536
+ if (write_in_full(fd, value+i, 1) != 1)
537
+ return 0;
538
+ break;
539
+ }
540
+ if (write_in_full(fd, "\n", 1) != 1)
541
+ return 0;
542
+ return 1;
543
}
544
545
static int find_beginning_of_line(const char* contents, int size,
@@ -648,8 +679,11 @@ int git_config_set_multivar(const char* key, const char* value,
679
}
680
681
store.key = (char*)key;
651
- store_write_section(fd, key);
652
- store_write_pair(fd, key, value);
682
+ if (!store_write_section(fd, key) ||
683
+ !store_write_pair(fd, key, value)) {
684
+ ret = write_error();
685
+ goto out_free;
686
+ }
687
} else{
688
struct stat st;
689
char* contents;
@@ -729,10 +763,10 @@ int git_config_set_multivar(const char* key, const char* value,
763
764
/* write the first part of the config */
765
if (copy_end > copy_begin) {
732
- write(fd, contents + copy_begin,
766
+ write_in_full(fd, contents + copy_begin,
767
copy_end - copy_begin);
768
if (new_line)
735
- write(fd, "\n", 1);
769
+ write_in_full(fd, "\n", 1);
770
}
771
copy_begin = store.offset[i];
772
}
@@ -740,13 +774,19 @@ int git_config_set_multivar(const char* key, const char* value,
774
/* write the pair (value == NULL means unset) */
775
if (value != NULL) {
776
if (store.state == START)
743
- store_write_section(fd, key);
744
- store_write_pair(fd, key, value);
777
+ if (!store_write_section(fd, key)) {
778
+ ret = write_error();
779
+ goto out_free;
780
+ }
781
+ if (!store_write_pair(fd, key, value)) {
782
+ ret = write_error();
783
+ goto out_free;
784
+ }
785
}
786
787
/* write the rest of the config */
788
if (copy_begin < st.st_size)
749
- write(fd, contents + copy_begin,
789
+ write_in_full(fd, contents + copy_begin,
790
st.st_size - copy_begin);
791
792
munmap(contents, st.st_size);
@@ -800,6 +840,7 @@ int git_config_rename_section(const char *old_name, const char *new_name)
840
841
while (fgets(buf, sizeof(buf), config_file)) {
842
int i;
843
+ int length;
844
for (i = 0; buf[i] && isspace(buf[i]); i++)
845
; /* do nothing */
846
if (buf[i] == '[') {
@@ -830,15 +871,22 @@ int git_config_rename_section(const char *old_name, const char *new_name)
871
/* old_name matches */
872
ret++;
873
store.baselen = strlen(new_name);
833
- store_write_section(out_fd, new_name);
874
+ if (!store_write_section(out_fd, new_name)) {
875
+ ret = write_error();
876
+ goto out;
877
+ }
878
continue;
879
}
880
}
837
- write(out_fd, buf, strlen(buf));
881
+ length = strlen(buf);
882
+ if (write_in_full(out_fd, buf, length) != length) {
883
+ ret = write_error();
884
+ goto out;
885
+ }
886
}
887
fclose(config_file);
888
if (close(out_fd) || commit_lock_file(lock) < 0)
841
- ret = error("Cannot commit config file!");
889
+ ret = error("Cannot commit config file!");
890
out:
891
free(config_filename);
892
return ret;