safe_fgets() - even more anal fgets()
[git.git] / pack-objects.c
index 5e1e14c..7d62477 100644 (file)
@@ -53,6 +53,7 @@ static int nr_objects = 0, nr_alloc = 0;
 static const char *base_name;
 static unsigned char pack_file_sha1[20];
 static int progress = 1;
+static volatile sig_atomic_t progress_update = 0;
 
 /*
  * The object names in objects array are hashed with this hashtable,
@@ -323,18 +324,38 @@ static void write_pack_file(void)
        struct sha1file *f;
        unsigned long offset;
        struct pack_header hdr;
+       unsigned last_percent = 999;
+       int do_progress = 0;
 
        if (!base_name)
                f = sha1fd(1, "<stdout>");
-       else
-               f = sha1create("%s-%s.%s", base_name, sha1_to_hex(object_list_sha1), "pack");
+       else {
+               f = sha1create("%s-%s.%s", base_name,
+                              sha1_to_hex(object_list_sha1), "pack");
+               do_progress = progress;
+       }
+       if (do_progress)
+               fprintf(stderr, "Writing %d objects.\n", nr_objects);
+
        hdr.hdr_signature = htonl(PACK_SIGNATURE);
        hdr.hdr_version = htonl(PACK_VERSION);
        hdr.hdr_entries = htonl(nr_objects);
        sha1write(f, &hdr, sizeof(hdr));
        offset = sizeof(hdr);
-       for (i = 0; i < nr_objects; i++)
+       for (i = 0; i < nr_objects; i++) {
                offset = write_one(f, objects + i, offset);
+               if (do_progress) {
+                       unsigned percent = written * 100 / nr_objects;
+                       if (progress_update || percent != last_percent) {
+                               fprintf(stderr, "%4u%% (%u/%u) done\r",
+                                       percent, written, nr_objects);
+                               progress_update = 0;
+                               last_percent = percent;
+                       }
+               }
+       }
+       if (do_progress)
+               fputc('\n', stderr);
 
        sha1close(f, pack_file_sha1, 1);
 }
@@ -662,10 +683,8 @@ static int try_delta(struct unpacked *cur, struct unpacked *old, unsigned max_de
        return 0;
 }
 
-static volatile int progress_update = 0;
 static void progress_interval(int signum)
 {
-       signal(SIGALRM, progress_interval);
        progress_update = 1;
 }
 
@@ -674,10 +693,14 @@ static void find_deltas(struct object_entry **list, int window, int depth)
        int i, idx;
        unsigned int array_size = window * sizeof(struct unpacked);
        struct unpacked *array = xmalloc(array_size);
+       unsigned processed = 0;
+       unsigned last_percent = 999;
 
        memset(array, 0, array_size);
        i = nr_objects;
        idx = 0;
+       if (progress)
+               fprintf(stderr, "Deltifying %d objects.\n", nr_objects);
 
        while (--i >= 0) {
                struct object_entry *entry = list[i];
@@ -686,10 +709,15 @@ static void find_deltas(struct object_entry **list, int window, int depth)
                char type[10];
                int j;
 
-               if (progress_update || i == 0) {
-                       fprintf(stderr, "Deltifying (%d %d%%)\r",
-                               nr_objects-i, (nr_objects-i) * 100/nr_objects);
-                       progress_update = 0;
+               processed++;
+               if (progress) {
+                       unsigned percent = processed * 100 / nr_objects;
+                       if (percent != last_percent || progress_update) {
+                               fprintf(stderr, "%4u%% (%u/%u) done\r",
+                                       percent, processed, nr_objects);
+                               progress_update = 0;
+                               last_percent = percent;
+                       }
                }
 
                if (entry->delta)
@@ -735,7 +763,6 @@ static void prepare_pack(int window, int depth)
        sorted_by_type = create_sorted_list(type_size_sort);
        if (window && depth)
                find_deltas(sorted_by_type, window+1, depth);
-       write_pack_file();
 }
 
 static int reuse_cached_pack(unsigned char *sha1, int pack_to_stdout)
@@ -792,6 +819,23 @@ static int reuse_cached_pack(unsigned char *sha1, int pack_to_stdout)
        return 1;
 }
 
+static void setup_progress_signal(void)
+{
+       struct sigaction sa;
+       struct itimerval v;
+
+       memset(&sa, 0, sizeof(sa));
+       sa.sa_handler = progress_interval;
+       sigemptyset(&sa.sa_mask);
+       sa.sa_flags = SA_RESTART;
+       sigaction(SIGALRM, &sa, NULL);
+
+       v.it_interval.tv_sec = 1;
+       v.it_interval.tv_usec = 0;
+       v.it_value = v.it_interval;
+       setitimer(ITIMER_REAL, &v, NULL);
+}
+
 int main(int argc, char **argv)
 {
        SHA_CTX ctx;
@@ -857,20 +901,26 @@ int main(int argc, char **argv)
        prepare_packed_git();
 
        if (progress) {
-               struct itimerval v;
-               v.it_interval.tv_sec = 1;
-               v.it_interval.tv_usec = 0;
-               v.it_value = v.it_interval;
-               signal(SIGALRM, progress_interval);
-               setitimer(ITIMER_REAL, &v, NULL);
                fprintf(stderr, "Generating pack...\n");
+               setup_progress_signal();
        }
 
-       while (fgets(line, sizeof(line), stdin) != NULL) {
+       for (;;) {
                unsigned int hash;
                char *p;
                unsigned char sha1[20];
 
+               if (!fgets(line, sizeof(line), stdin)) {
+                       if (feof(stdin))
+                               break;
+                       if (!ferror(stdin))
+                               die("fgets returned NULL, not EOF, not error!");
+                       if (errno != EINTR)
+                               die("fgets: %s", strerror(errno));
+                       clearerr(stdin);
+                       continue;
+               }
+
                if (progress_update) {
                        fprintf(stderr, "Counting objects...%d\r", nr_objects);
                        progress_update = 0;
@@ -905,6 +955,14 @@ int main(int argc, char **argv)
                ;
        else {
                prepare_pack(window, depth);
+               if (progress && pack_to_stdout) {
+                       /* the other end usually displays progress itself */
+                       struct itimerval v = {{0,},};
+                       setitimer(ITIMER_REAL, &v, NULL);
+                       signal(SIGALRM, SIG_IGN );
+                       progress_update = 0;
+               }
+               write_pack_file();
                if (!pack_to_stdout) {
                        write_index_file();
                        puts(sha1_to_hex(object_list_sha1));