aboutsummaryrefslogtreecommitdiff
path: root/libsylph/utils.c
diff options
context:
space:
mode:
authorhiro <hiro@ee746299-78ed-0310-b773-934348b2243d>2010-07-12 09:33:27 +0000
committerhiro <hiro@ee746299-78ed-0310-b773-934348b2243d>2010-07-12 09:33:27 +0000
commitec14e81935047bd2b2182595b76650b5f2fef893 (patch)
treed87f0c546859b5ef9a8f49a4f9abcdbd75ac63e7 /libsylph/utils.c
parentaf81f08052caa5dd3bf88b31d9eed3cf6158e9df (diff)
implemented concatenation of partial messages (RFC 2046).
git-svn-id: svn://sylpheed.sraoss.jp/sylpheed/trunk@2608 ee746299-78ed-0310-b773-934348b2243d
Diffstat (limited to 'libsylph/utils.c')
-rw-r--r--libsylph/utils.c99
1 files changed, 78 insertions, 21 deletions
diff --git a/libsylph/utils.c b/libsylph/utils.c
index 145eccc6..23bb3b62 100644
--- a/libsylph/utils.c
+++ b/libsylph/utils.c
@@ -2422,6 +2422,49 @@ off_t get_left_file_size(FILE *fp)
return size;
}
+gint get_last_empty_line_size(FILE *fp, off_t size)
+{
+ glong pos;
+ gint lsize = 0;
+ gchar buf[4];
+ size_t nread;
+
+ if (size < 4)
+ return -1;
+
+ if ((pos = ftell(fp)) < 0) {
+ perror("ftell");
+ return -1;
+ }
+ if (fseek(fp, size - 4, SEEK_CUR) < 0) {
+ perror("fseek");
+ return -1;
+ }
+
+ /* read last 4 bytes */
+ nread = fread(buf, sizeof(buf), 1, fp);
+ if (nread != 1) {
+ perror("fread");
+ return -1;
+ }
+ g_print("last 4 bytes: %02x %02x %02x %02x\n", buf[0], buf[1], buf[2], buf[3]);
+ if (buf[3] == '\n') {
+ if (buf[2] == '\n')
+ lsize = 1;
+ else if (buf[2] == '\r') {
+ if (buf[1] == '\n')
+ lsize = 2;
+ }
+ }
+
+ if (fseek(fp, pos, SEEK_SET) < 0) {
+ perror("fseek");
+ return -1;
+ }
+
+ return lsize;
+}
+
gboolean file_exist(const gchar *file, gboolean allow_fifo)
{
if (file == NULL)
@@ -2989,29 +3032,20 @@ gint move_file(const gchar *src, const gchar *dest, gboolean overwrite)
return 0;
}
-gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
+gint append_file_part(FILE *fp, off_t offset, size_t length, FILE *dest_fp)
{
- FILE *dest_fp;
gint n_read;
gint bytes_left, to_read;
gchar buf[BUFSIZ];
- gboolean err = FALSE;
+
+ g_return_val_if_fail(fp != NULL, -1);
+ g_return_val_if_fail(dest_fp != NULL, -1);
if (fseek(fp, offset, SEEK_SET) < 0) {
perror("fseek");
return -1;
}
- if ((dest_fp = g_fopen(dest, "wb")) == NULL) {
- FILE_OP_ERROR(dest, "fopen");
- return -1;
- }
-
- if (change_file_mode_rw(dest_fp, dest) < 0) {
- FILE_OP_ERROR(dest, "chmod");
- g_warning("can't change file mode\n");
- }
-
bytes_left = length;
to_read = MIN(bytes_left, sizeof(buf));
@@ -3019,9 +3053,7 @@ gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
if (n_read < to_read && ferror(fp))
break;
if (fwrite(buf, n_read, 1, dest_fp) < 1) {
- g_warning(_("writing to %s failed.\n"), dest);
- fclose(dest_fp);
- g_unlink(dest);
+ g_warning("append_file_part: writing to file failed.\n");
return -1;
}
bytes_left -= n_read;
@@ -3032,14 +3064,39 @@ gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
if (ferror(fp)) {
perror("fread");
- err = TRUE;
+ return -1;
}
- if (fclose(dest_fp) == EOF) {
- FILE_OP_ERROR(dest, "fclose");
- err = TRUE;
+ if (fflush(dest_fp) == EOF) {
+ FILE_OP_ERROR("append_file_part", "fflush");
+ return -1;
}
- if (err) {
+ return 0;
+}
+
+gint copy_file_part(FILE *fp, off_t offset, size_t length, const gchar *dest)
+{
+ FILE *dest_fp;
+
+ if ((dest_fp = g_fopen(dest, "wb")) == NULL) {
+ FILE_OP_ERROR(dest, "fopen");
+ return -1;
+ }
+
+ if (change_file_mode_rw(dest_fp, dest) < 0) {
+ FILE_OP_ERROR(dest, "chmod");
+ g_warning("can't change file mode\n");
+ }
+
+ if (append_file_part(fp, offset, length, dest_fp) < 0) {
+ g_warning("writing to %s failed.\n", dest);
+ fclose(dest_fp);
+ g_unlink(dest);
+ return -1;
+ }
+
+ if (fclose(dest_fp) == EOF) {
+ FILE_OP_ERROR(dest, "fclose");
g_unlink(dest);
return -1;
}