{"thread":{"id":"7533","subject":"[PATCH 04/13] support rollback on writes to checksummed files","startedAt":"2007-04-05T22:29:32Z","lastAt":"2007-04-05T22:58:22Z","messageCount":2,"participants":["Dana How","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":13},"messages":[{"id":"38745","messageId":"56b7f5510704051529k55a96bdfy5b072b220f8269a7@mail.gmail.com","threadId":"7533","inReplyTo":null,"subject":"[PATCH 04/13] support rollback on writes to checksummed files","fromName":"Dana How","fromEmail":"danahow@gmail.com","sentAt":"2007-04-05T22:29:32Z","receivedAt":"2007-04-05T22:29:32Z","isPatch":true,"sender":{"key":"danahow@gmail.com","avatar":null},"body":"add struct sha1posn, sha1mark(), sha1undo() to csum-file.[ch]\n---\n csum-file.c |   38 ++++++++++++++++++++++++++++++++++++++\n csum-file.h |    6 ++++++\n 2 files changed, 44 insertions(+), 0 deletions(-)\n\n-- \nDana L. How  danahow@gmail.com  +1 650 804 5991 cell\n\n\nFrom 1df9e443ce11365f955b83e70365670ff9ada8d6 Mon Sep 17 00:00:00 2001\nFrom: Dana How <how@deathvalley.cswitch.com>\nDate: Thu, 5 Apr 2007 13:13:42 -0700\nSubject: [PATCH 04/13] support rollback on writes to checksummed files\nadd struct sha1posn, sha1mark(), sha1undo() to csum-file.[ch]\n---\n csum-file.c |   38 ++++++++++++++++++++++++++++++++++++++\n csum-file.h |    6 ++++++\n 2 files changed, 44 insertions(+), 0 deletions(-)\n\ndiff --git a/csum-file.c b/csum-file.c\nindex e1ff769..e2bef75 100644\n--- a/csum-file.c\n+++ b/csum-file.c\n@@ -72,6 +72,44 @@ int sha1write(struct sha1file *f, void *buf, unsigned int count)\n \treturn 0;\n }\n \n+/*\n+ * Save current position/state in output file\n+ * (needs to be fast -- no system calls!)\n+ */\n+void sha1mark(struct sha1file *f, struct sha1posn *p)\n+{\n+\tp->offset = f->offset;\n+\tp->ctx = f->ctx;\t/* larger than I'd like */\n+}\n+\n+/*\n+ * Restore previous position/state in output file\n+ * (can be slow)\n+ */\n+void sha1undo(struct sha1file *f, struct sha1posn *p, long new, long old)\n+{\n+\tif (new - old == (long)f->offset - (long)p->offset) {\n+\t\tf->ctx = p->ctx;\n+\t\tf->offset = p->offset;\n+\t\treturn;\n+\t}\n+\tif (lseek(f->fd, (off_t)old - (off_t)p->offset, SEEK_SET) == (off_t)-1)\n+\t\tdie(\"sha1 file '%s' undo seekback error (%s)\", f->name, strerror(errno));\n+\tf->ctx = p->ctx;\n+\twhile (p->offset) {\n+\t\tint ret = xread(f->fd, f->buffer, p->offset);\n+\t\tif (!ret)\n+\t\t\tdie(\"sha1 file '%s' undo readback error. No data\", f->name);\n+\t\tif (ret < 0)\n+\t\t\tdie(\"sha1 file '%s' undo readback error (%s)\", f->name, strerror(errno));\n+\t\tSHA1_Update(&f->ctx, f->buffer, ret);\n+\t\tp->offset -= ret;\n+\t}\n+\tif (ftruncate(f->fd, (off_t)old))\n+\t\tdie(\"sha1 file '%s' undo truncate error (%s)\", f->name, strerror(errno));\n+\tf->offset = 0;\n+}\n+\n struct sha1file *sha1create(const char *fmt, ...)\n {\n \tstruct sha1file *f;\ndiff --git a/csum-file.h b/csum-file.h\nindex 3ad1a99..780df17 100644\n--- a/csum-file.h\n+++ b/csum-file.h\n@@ -9,11 +9,17 @@ struct sha1file {\n \tchar name[PATH_MAX];\n \tunsigned char buffer[8192];\n };\n+struct sha1posn {\n+\tunsigned int offset;\n+\tSHA_CTX ctx;\n+};\n \n extern struct sha1file *sha1fd(int fd, const char *name);\n extern struct sha1file *sha1create(const char *fmt, ...) __attribute__((format (printf, 1, 2)));\n extern int sha1close(struct sha1file *, unsigned char *, int);\n extern int sha1write(struct sha1file *, void *, unsigned int);\n extern int sha1write_compressed(struct sha1file *, void *, unsigned int);\n+extern void sha1mark(struct sha1file *, struct sha1posn *);\n+extern void sha1undo(struct sha1file *, struct sha1posn *, long, long);\n \n #endif\n-- \n1.5.1.rc2.18.g9c88-dirty\n\n"},{"id":"38757","messageId":"7vhcrumnsh.fsf@assigned-by-dhcp.cox.net","threadId":"7533","inReplyTo":"56b7f5510704051529k55a96bdfy5b072b220f8269a7@mail.gmail.com","subject":"Re: [PATCH 04/13] support rollback on writes to checksummed files","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2007-04-05T22:58:22Z","receivedAt":"2007-04-05T22:58:22Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Dana How\" <danahow@gmail.com> writes:\n\n> From 1df9e443ce11365f955b83e70365670ff9ada8d6 Mon Sep 17 00:00:00 2001\n> From: Dana How <how@deathvalley.cswitch.com>\n> Date: Thu, 5 Apr 2007 13:13:42 -0700\n> Subject: [PATCH 04/13] support rollback on writes to checksummed files\n> add struct sha1posn, sha1mark(), sha1undo() to csum-file.[ch]\n\nHave an empty line after \"Subject:\".  Begin a sentence with a\ncapital (i.e. \"Add struct...\").\n\nExplain *why* these changes are needed, not just *what* the\npatch does -- the latter can be read from the patch text.\n"}]}