@matheusbsbs / matheus-bagliano / commits / 6015c28b1d
Fork 0

read-cache: tweak racy-git delay logic

Instead of looping over the entries and writing out, use a separate loop after all entries have been written out to check how many entries are racily clean. Make sure that the newly created index file gets the right timestamp when we check by flushing the buffered data by ce_write(). Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Aug 8, 2006 at 14:47 UTC 6015c28b1d6163f124332769989326ee470afbb6
1 file changed +52 -21
read-cache.c
+52 -21
@@ -841,6 +841,18 @@ unmap:
841 static unsigned char write_buffer[WRITE_BUFFER_SIZE];
842 static unsigned long write_buffer_len;
843
844 +static int ce_write_flush(SHA_CTX *context, int fd)
845 +{
846 + unsigned int buffered = write_buffer_len;
847 + if (buffered) {
848 + SHA1_Update(context, write_buffer, buffered);
849 + if (write(fd, write_buffer, buffered) != buffered)
850 + return -1;
851 + write_buffer_len = 0;
852 + }
853 + return 0;
854 +}
855 +
856 static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len)
857 {
858 while (len) {
@@ -851,8 +863,8 @@ static int ce_write(SHA_CTX *context, int fd, void *data, unsigned int len)
863 memcpy(write_buffer + buffered, data, partial);
864 buffered += partial;
865 if (buffered == WRITE_BUFFER_SIZE) {
854 - SHA1_Update(context, write_buffer, WRITE_BUFFER_SIZE);
855 - if (write(fd, write_buffer, WRITE_BUFFER_SIZE) != WRITE_BUFFER_SIZE)
866 + write_buffer_len = buffered;
867 + if (ce_write_flush(context, fd))
868 return -1;
869 buffered = 0;
870 }
@@ -962,19 +974,15 @@ int write_cache(int newfd, struct cache_entry **cache, int entries)
974 if (ce_write(&c, newfd, &hdr, sizeof(hdr)) < 0)
975 return -1;
976
965 - now = fstat(newfd, &st) ? 0 : st.st_mtime;
966 - recent = 0;
977 for (i = 0; i < entries; i++) {
978 struct cache_entry *ce = cache[i];
969 - time_t entry_time = (time_t) ntohl(ce->ce_mtime.sec);
979 if (!ce->ce_mode)
980 continue;
972 - if (index_file_timestamp && index_file_timestamp <= entry_time)
981 + if (index_file_timestamp &&
982 + index_file_timestamp <= ntohl(ce->ce_mtime.sec))
983 ce_smudge_racily_clean_entry(ce);
984 if (ce_write(&c, newfd, ce, ce_size(ce)) < 0)
985 return -1;
976 - if (now && now <= entry_time)
977 - recent++;
986 }
987
988 /* Write extension data here */
@@ -1004,19 +1012,42 @@ int write_cache(int newfd, struct cache_entry **cache, int entries)
1012 * This assumes that nobody is touching the working tree while
1013 * we are updating the index.
1014 */
1007 - if (20 < recent || entries <= recent * 4) {
1008 - now = fstat(newfd, &st) ? 0 : st.st_mtime;
1009 - while (now && !fstat(newfd, &st) && st.st_mtime <= now) {
1010 - struct timespec rq, rm;
1011 - off_t where = lseek(newfd, 0, SEEK_CUR);
1012 - rq.tv_sec = 0;
1013 - rq.tv_nsec = 250000000;
1014 - nanosleep(&rq, &rm);
1015 - if ((where == (off_t) -1) ||
1016 - (write(newfd, "", 1) != 1) ||
1017 - (lseek(newfd, -1, SEEK_CUR) != where) ||
1018 - ftruncate(newfd, where))
1019 - break;
1015 +
1016 + /* Make sure that the new index file has st_mtime
1017 + * that is current enough -- ce_write() batches the data
1018 + * so it might not have written anything yet.
1019 + */
1020 + ce_write_flush(&c, newfd);
1021 +
1022 + now = fstat(newfd, &st) ? 0 : st.st_mtime;
1023 + if (now) {
1024 + recent = 0;
1025 + for (i = 0; i < entries; i++) {
1026 + struct cache_entry *ce = cache[i];
1027 + time_t entry_time = (time_t) ntohl(ce->ce_mtime.sec);
1028 + if (!ce->ce_mode)
1029 + continue;
1030 + if (now && now <= entry_time)
1031 + recent++;
1032 + }
1033 + if (20 < recent && entries <= recent * 4) {
1034 +#if 0
1035 + fprintf(stderr, "entries %d\n", entries);
1036 + fprintf(stderr, "recent %d\n", recent);
1037 + fprintf(stderr, "now %lu\n", now);
1038 +#endif
1039 + while (!fstat(newfd, &st) && st.st_mtime <= now) {
1040 + struct timespec rq, rm;
1041 + off_t where = lseek(newfd, 0, SEEK_CUR);
1042 + rq.tv_sec = 0;
1043 + rq.tv_nsec = 250000000;
1044 + nanosleep(&rq, &rm);
1045 + if ((where == (off_t) -1) ||
1046 + (write(newfd, "", 1) != 1) ||
1047 + (lseek(newfd, -1, SEEK_CUR) != where) ||
1048 + ftruncate(newfd, where))
1049 + break;
1050 + }
1051 }
1052 }
1053 return ce_flush(&c, newfd);