All pastes #729316 Raw Edit

File.cpp Util.h

public text v1 · immutable
·published 2007-10-07 21:14 UTC
rendered paste body
Index: Util.h
===================================================================
RCS file: /cvsroot/linuxdcpp/linuxdcpp/client/Util.h,v
retrieving revision 1.11
diff -u -r1.11 Util.h
--- Util.h	24 Mar 2007 18:33:17 -0000	1.11
+++ Util.h	7 Oct 2007 21:12:52 -0000
@@ -192,7 +192,13 @@
 		}
 		return tmp;
 #else // _WIN32
-		return Text::toUtf8(strerror(aError));
+// strerror is not thread safe.		
+//		return Text::toUtf8(strerror(aError));
+
+		char errorBuffer[100];
+		char* errorMsg = strerror_r(aError, errorBuffer, 100);
+		return Text::toUtf8(errorMsg);
+
 #endif // _WIN32
 	}
 
Index: File.cpp
===================================================================
RCS file: /cvsroot/linuxdcpp/linuxdcpp/client/File.cpp,v
retrieving revision 1.4
diff -u -r1.4 File.cpp
--- File.cpp	7 Jul 2007 21:21:06 -0000	1.4
+++ File.cpp	7 Oct 2007 21:12:52 -0000
@@ -223,8 +223,9 @@
 	}
 
 	h = open(filename.c_str(), m, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
-	if(h == -1)
-		throw FileException("Could not open file");
+	if(h == -1) {
+		throw FileException(Util::translateError(errno));
+	}
 }
 
 uint32_t File::getLastModified() throw() {
@@ -271,20 +272,31 @@
 }
 
 size_t File::read(void* buf, size_t& len) throw(FileException) {
-	ssize_t x = ::read(h, buf, len);
-	if(x == -1)
-		throw FileException("Read error");
-	len = x;
-	return (size_t)x;
+	ssize_t result = ::read(h, buf, len);
+	if (result == -1) {
+		throw FileException(Util::translateError(errno));
+	}
+	len = result;
+	return (size_t)result;
 }
 
 size_t File::write(const void* buf, size_t len) throw(FileException) {
-	ssize_t x = ::write(h, buf, len);
-	if(x == -1)
-		throw FileException("Write error");
-	if(x < (ssize_t)len)
-		throw FileException("Disk full(?)");
-	return x;
+	ssize_t result;
+	char* pointer = (char*)buf;
+	int left = len;
+
+	while (left > 0) {
+		result = ::write(h,pointer,left);
+		if (result == -1) {
+			if (errno != EINTR) {
+				throw FileException(Util::translateError(errno));
+			}
+		} else {
+			pointer+=result;
+			left-=result;
+		}
+	}
+	return len;
 }
 
 // some ftruncate implementations can't extend files like SetEndOfFile,
@@ -338,10 +350,10 @@
 void File::renameFile(const string& source, const string& target) throw(FileException) {
 	int ret = ::rename(Text::fromUtf8(source).c_str(), Text::fromUtf8(target).c_str());
 	if(ret != 0 && errno == EXDEV) {
-		copyFile(source.c_str(), target.c_str());
-		deleteFile(source.c_str());
+		copyFile(source, target);
+		deleteFile(source);
 	} else if(ret != 0)
-		throw FileException(source.c_str() + Util::translateError(errno));
+		throw FileException(source + Util::translateError(errno));
 }
 
 // This doesn't assume all bytes are written in one write call, it is a bit safer