AmendHub

Download:

jcs

/

subtext

/

amendments

/

453

folder: Print progress every 10KB while checksumming files


jcs made amendment 453 about 1 year ago
--- folder.c Mon Mar 27 15:11:30 2023 +++ folder.c Mon Mar 27 16:00:56 2023 @@ -114,7 +114,8 @@ bool folder_file_valid_filename(struct session *sessio struct folder *folder, struct folder_file *file, char **error); bool folder_edit_file(struct session *s, struct folder *folder, struct folder_file *file, char *file_path); -bool folder_file_checksum(struct folder_file *file, char *file_path); +bool folder_file_checksum(struct session *s, struct folder_file *file, + char *file_path); void folder_list(struct session *s) @@ -460,14 +461,14 @@ folder_upload(struct session *s, struct folder *folder ZDestroy(zs); zs = NULL; - session_printf(s, "Calculating SHA1 checksum of uploaded file..."); + session_printf(s, "Calculating SHA1 checksum of file... "); session_flush(s); - if (!folder_file_checksum(&file, upload_path)) { + if (!folder_file_checksum(s, &file, upload_path)) { session_printf(s, "failed reading file!\r\n"); session_flush(s); goto file_upload_cancel; } - session_printf(s, "done.\r\n\r\n"); + session_output(s, "\r\n\r\n", 4); session_flush(s); if (folder_edit_file(s, folder, &file, upload_path)) @@ -546,8 +547,8 @@ folder_file_view(struct session *s, struct folder *fol if (!(s->user && (s->user->is_sysop || s->user->id == file.uploader_user_id))) { /* disable deleting and editing */ + dopts[1].key[0] = '\0'; dopts[2].key[0] = '\0'; - dopts[3].key[0] = '\0'; } uploader = user_username(file.uploader_user_id); @@ -1080,15 +1081,16 @@ file_annotate: session_printf(s, "%c\r\n", c); session_flush(s); - session_printf(s, "Calculating SHA1 checksum of file..."); + session_printf(s, "Calculating SHA1 checksum... "); session_flush(s); - if (!folder_file_checksum(file, file_path)) { + if (!folder_file_checksum(s, file, file_path)) { session_printf(s, "failed reading %s!\r\n", file_path); session_flush(s); return false; } - session_printf(s, "done:\r\n%s\r\n", file->sha1_checksum); + session_printf(s, "\r\nNew checksum: %s\r\n", + file->sha1_checksum); session_flush(s); } break; @@ -1134,14 +1136,16 @@ file_annotate: } bool -folder_file_checksum(struct folder_file *file, char *file_path) +folder_file_checksum(struct session *s, struct folder_file *file, + char *file_path) { SHA1_CTX sha1; FILE *fp; char *data; - size_t rsize, size, n; + char fmt[12]; + size_t rsize, size, dg, olen, tsize; - data = xmalloc(1024); + data = xmalloc(512); if (data == NULL) return false; @@ -1152,13 +1156,30 @@ folder_file_checksum(struct folder_file *file, char *f return false; } + tsize = file->size; + dg = 1; + while (tsize >= 10) { + dg++; + tsize /= 10; + } + /* 123456 -> "% 6lu/%lu" */ + snprintf(fmt, sizeof(fmt), "%% %lulu/%%lu", dg); + rsize = 0; - for (n = 1; fp && !feof(fp); n++) { - size = fread(data, 1, 1024, fp); + olen = session_printf(s, fmt, rsize, file->size); + session_flush(s); + while (!feof(fp)) { + size = fread(data, 1, 512, fp); rsize += size; SHA1Update(&sha1, (const u_int8_t *)data, size); - if (n % 2 == 0) + if (rsize % 2048 == 0) uthread_yield(); + if (rsize % 10240 == 0 || rsize == file->size) { + session_printf(s, "%s", ansi(s, ANSI_BACK_N, (short)olen, + ANSI_END)); + session_printf(s, fmt, rsize, file->size); + session_flush(s); + } } fclose(fp); xfree(&data);