From: Christopher Brannon <chris@the-brannons.com>
To: edbrowse-dev@lists.the-brannons.com
Subject: [Edbrowse-dev] [PATCH] Use stdio when saving files.
Date: Wed, 23 Mar 2011 03:28:59 +0000 [thread overview]
Message-ID: <1300850939-18162-1-git-send-email-chris@the-brannons.com> (raw)
Tyler S tells me that writeFile is slow when writing to a file on a
networked filesystem. Now, we write them using stdio, instead of Unix
system calls. The buffering should improve performance.
---
src/buffers.c | 33 +++++++++++++++++++++------------
1 files changed, 21 insertions(+), 12 deletions(-)
diff --git a/src/buffers.c b/src/buffers.c
index 4a37d58..7710c2d 100644
--- a/src/buffers.c
+++ b/src/buffers.c
@@ -1327,7 +1327,10 @@ readFile(const char *filename, const char *post)
static bool
writeFile(const char *name, int mode)
{
- int i, fh;
+ int i;
+ FILE *fh;
+ char *modeString;
+ int modeString_l;
if(memEqualCI(name, "file://", 7))
name += 7;
@@ -1353,12 +1356,18 @@ writeFile(const char *name, int mode)
}
/* mode should be TRUNC or APPEND */
- mode |= O_WRONLY | O_CREAT;
+ modeString = initString(&modeString_l);
+ if(mode & O_APPEND)
+ stringAndChar(&modeString, &modeString_l, 'a');
+ else
+ stringAndChar(&modeString, &modeString_l, 'w');
if(cw->binMode)
- mode |= O_BINARY;
+ stringAndChar(&modeString, &modeString_l, 'b');
+
+ fh = fopen(name, modeString);
+ nzFree(modeString);
- fh = open(name, mode, 0666);
- if(fh < 0) {
+ if(fh == NULL) {
setError(MSG_NoCreate2, name);
return false;
}
@@ -1394,7 +1403,7 @@ writeFile(const char *name, int mode)
free(p);
alloc_p = true;
p = (pst) tp;
- if(write(fh, p, tlen) < tlen)
+ if(fwrite(p, 1, tlen, fh) < tlen)
rc = false;
goto endline;
}
@@ -1405,20 +1414,20 @@ writeFile(const char *name, int mode)
free(p);
alloc_p = true;
p = (pst) tp;
- if(write(fh, p, tlen) < tlen)
+ if(fwrite(p, 1, tlen, fh) < tlen)
rc = false;
goto endline;
}
}
- if(write(fh, p, len) < len)
+ if(fwrite(p, 1, len, fh) < len)
rc = false;
goto endline;
}
/* must write this line with the suffix on the end */
--len;
- if(write(fh, p, len) < len) {
+ if(fwrite(p, 1, len, fh) < len) {
badwrite:
rc = false;
goto endline;
@@ -1426,7 +1435,7 @@ writeFile(const char *name, int mode)
fileSize += len;
strcat(suf, "\n");
len = strlen(suf);
- if(write(fh, suf, len) < len)
+ if(fwrite(suf, 1, len, fh) < len)
goto badwrite;
endline:
@@ -1434,13 +1443,13 @@ writeFile(const char *name, int mode)
free(p);
if(!rc) {
setError(MSG_NoWrite2, name);
- close(fh);
+ fclose(fh);
return false;
}
fileSize += len;
} /* loop over lines */
- close(fh);
+ fclose(fh);
/* This is not an undoable operation, nor does it change data.
* In fact the data is "no longer modified" if we have written all of it. */
if(startRange == 1 && endRange == cw->dol)
--
1.7.3.4
reply other threads:[~2011-03-23 3:43 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1300850939-18162-1-git-send-email-chris@the-brannons.com \
--to=chris@the-brannons.com \
--cc=edbrowse-dev@lists.the-brannons.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).