gmime r1560 - in trunk: . gmime src



Author: fejj
Date: Mon Apr  6 00:06:44 2009
New Revision: 1560
URL: http://svn.gnome.org/viewvc/gmime?rev=1560&view=rev

Log:
2009-04-05  Jeffrey Stedfast  <fejj novell com>

	* src/uuencode.c (uuencode): Don't bother dup()ing the stdout fd, just
	use set_owner(FALSE) so that it doesn't close when we destroy the stream
	instead.

	* gmime/gmime-stream-file.c (stream_read, stream_write): Don't depend on
	simple arithmetic to update our stream->position on Windows since it
	may do line-ending translation behind our backs. Call ftell() to get
	our real position after reading or writing.
	
	* gmime/gmime-stream-fs.c (stream_read, stream_write): Same idea.


Modified:
   trunk/ChangeLog
   trunk/gmime/gmime-stream-file.c
   trunk/gmime/gmime-stream-fs.c
   trunk/src/uuencode.c

Modified: trunk/gmime/gmime-stream-file.c
==============================================================================
--- trunk/gmime/gmime-stream-file.c	(original)
+++ trunk/gmime/gmime-stream-file.c	Mon Apr  6 00:06:44 2009
@@ -149,8 +149,20 @@
 	/* make sure we are at the right position */
 	fseek (fstream->fp, (long) stream->position, SEEK_SET);
 	
-	if ((nread = fread (buf, 1, len, fstream->fp)) > 0)
+	if ((nread = fread (buf, 1, len, fstream->fp)) > 0) {
+#ifdef G_OS_WIN32
+		/* fucking Windows... sigh. Since it might decide to translate \r\n into \n,
+		 * we need to query the real position rather than using simple math. */
+		long pos;
+		
+		if ((pos = ftell (fstream->fp)) == -1)
+			stream->position += nread;
+		else
+			stream->position = pos;
+#else
 		stream->position += nread;
+#endif
+	}
 	
 	return (ssize_t) nread;
 }
@@ -176,9 +188,21 @@
 	
 	/* make sure we are at the right position */
 	fseek (fstream->fp, (long) stream->position, SEEK_SET);
-	
-	if ((nwritten = fwrite (buf, 1, len, fstream->fp)) > 0)
+
+	if ((nwritten = fwrite (buf, 1, len, fstream->fp)) > 0) {
+#ifdef G_OS_WIN32
+		/* fucking Windows... sigh. Since it might decide to translate \n into \r\n,
+		 * we need to query the real position rather than using simple math. */
+		long pos;
+		
+		if ((pos = ftell (fstream->fp)) == -1)
+			stream->position += nwritten;
+		else
+			stream->position = pos;
+#else
 		stream->position += nwritten;
+#endif
+	}
 	
 	return (ssize_t) nwritten;
 }

Modified: trunk/gmime/gmime-stream-fs.c
==============================================================================
--- trunk/gmime/gmime-stream-fs.c	(original)
+++ trunk/gmime/gmime-stream-fs.c	Mon Apr  6 00:06:44 2009
@@ -40,7 +40,9 @@
 
 #ifndef HAVE_FSYNC
 #ifdef G_OS_WIN32
-static int fsync (int fd) { return _commit (fd); }
+/* _commit() is the equivalent of fsync() on Windows, but it aborts the
+ * program if the fd is a tty, so we'll just no-op for now... */
+static int fsync (int fd) { return 0; }
 #else
 static int fsync (int fd) { return 0; }
 #endif
@@ -169,10 +171,22 @@
 		nread = read (fs->fd, buf, len);
 	} while (nread == -1 && errno == EINTR);
 	
-	if (nread > 0)
+	if (nread > 0) {
+#ifdef G_OS_WIN32
+		/* fucking Windows... sigh. Since it might decide to translate \r\n into \n,
+		 * we need to query the real position rather than using simple math. */
+		off_t pos;
+		
+		if ((pos = lseek (fs->fd, 0, SEEK_CUR)) == -1)
+			stream->position += nread;
+		else
+			stream->position = pos;
+#else
 		stream->position += nread;
-	else if (nread == 0)
+#endif
+	} else if (nread == 0) {
 		fs->eos = TRUE;
+	}
 	
 	return nread;
 }
@@ -212,10 +226,23 @@
 	if (n == -1 && (errno == EFBIG || errno == ENOSPC))
 		fs->eos = TRUE;
 	
-	if (nwritten > 0)
+	if (nwritten > 0) {
+#ifdef G_OS_WIN32
+		/* fucking Windows... sigh. Since it might decide to translate \n into \r\n,
+		 * we need to query the real position rather than using simple math. */
+		off_t pos;
+		
+		if ((pos = lseek (fs->fd, 0, SEEK_CUR)) == -1)
+			stream->position += nwritten;
+		else
+			stream->position = pos;
+#else
 		stream->position += nwritten;
-	else if (n == -1)
+#endif
+	} else if (n == -1) {
+		/* error and nothing written */
 		return -1;
+	}
 	
 	return nwritten;
 }

Modified: trunk/src/uuencode.c
==============================================================================
--- trunk/src/uuencode.c	(original)
+++ trunk/src/uuencode.c	Mon Apr  6 00:06:44 2009
@@ -136,14 +136,6 @@
 		return -1;
 	}
 	
-	if ((fd = dup (1)) == -1) {
-		fprintf (stderr, "%s: cannot open stdout: %s\n",
-			 progname, g_strerror (errno));
-		return -1;
-	}
-	
-	ostream = g_mime_stream_fs_new (fd);
-	
 	if (optind + 1 < argc)
 		filename = argv[optind++];
 	else
@@ -151,33 +143,35 @@
 	
 	name = argv[optind];
 	
+	/* open our input file... */
 	if ((fd = filename ? open (filename, O_RDONLY) : dup (0)) == -1) {
 		fprintf (stderr, "%s: %s: %s\n", progname,
 			 filename ? filename : "stdin",
 			 g_strerror (errno));
-		g_object_unref (ostream);
 		return -1;
 	}
 	
+	/* stat() our input file for file mode permissions */
 	if (fstat (fd, &st) == -1) {
 		fprintf (stderr, "%s: %s: %s\n", progname,
 			 filename ? filename : "stdin",
 			 g_strerror (errno));
-		g_object_unref (ostream);
+		close (fd);
 		return -1;
 	}
 	
-	if (g_mime_stream_printf (ostream, "begin%s %.3o %s\n",
-				  base64 ? "-base64" : "", st.st_mode & 0777, name) == -1) {
-		fprintf (stderr, "%s: %s\n", progname, g_strerror (errno));
-		g_object_unref (ostream);
-		return -1;
-	}
+	printf ("begin%s %.3o %s\n", base64 ? "-base64" : "", st.st_mode & 0777, name);
+	fflush (stdout);
 	
 	istream = g_mime_stream_fs_new (fd);
 	
+	/* open our output stream */
+	ostream = g_mime_stream_fs_new (1);
+	g_mime_stream_fs_set_owner ((GMimeStreamFs *) ostream, FALSE);
+	
 	fstream = g_mime_stream_filter_new (ostream);
 	
+	/* attach an encode filter */
 	filter = g_mime_filter_basic_new (encoding, TRUE);
 	g_mime_stream_filter_add ((GMimeStreamFilter *) fstream, filter);
 	g_object_unref (filter);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]