Index: Util/FileSystem.cs
===================================================================
--- Util/FileSystem.cs.orig
+++ Util/FileSystem.cs
@@ -148,6 +148,19 @@ namespace Beagle.Util {
                         return path;
                 }
 
+		// Windows (and hence .Net File.Delete) requires write
+		// permission on a file to delete it. This is different from
+		// the POSIX behaviour and works against our readonly
+		// tmp files.
+		public static void PosixDelete (string path)
+		{
+			int ret = Mono.Unix.Native.Syscall.unlink (path);
+		    	if (ret == -1)
+				throw new System.IO.IOException (String.Format (
+					    "Delete failed for {0}: {1}",
+					    path,
+					    Mono.Unix.Native.Stdlib.strerror (Mono.Unix.Native.Stdlib.GetLastError ())));
+		}
 	}
 
 	public class NoSpaceException : Exception {
Index: beagled/Lucene.Net/Store/SimpleFSLockFactory.cs
===================================================================
--- beagled/Lucene.Net/Store/SimpleFSLockFactory.cs.orig
+++ beagled/Lucene.Net/Store/SimpleFSLockFactory.cs
@@ -260,13 +260,7 @@ namespace Lucene.Net.Store
 		
 		public override void  Release()
 		{
-			int fd = Mono.Unix.Native.Syscall.unlink (
-		    	    	lockFile.FullName);
-		    	if (fd == -1)
-				throw new System.IO.IOException (
-					"Could not release lock file: "
-					+ Mono.Unix.Native.Stdlib.strerror (Mono.Unix.Native.Stdlib.GetLastError ()
-				));
+			Beagle.Util.FileSystem.PosixDelete (lockFile.FullName);
 
 			if (System.IO.File.Exists(lockFile.FullName)) {
 				Beagle.Util.Logger.Log.Warn ("Release didnt delete lockfile {0}.", lockFile.FullName);
Index: beagled/TextCache.cs
===================================================================
--- beagled/TextCache.cs.orig
+++ beagled/TextCache.cs
@@ -380,7 +380,10 @@ namespace Beagle.Daemon {
 			
 				if (! world_readable) {
 					// Make files only readable by the owner.
-					Mono.Unix.Native.Syscall.chmod (path, (Mono.Unix.Native.FilePermissions) 384);
+					Mono.Unix.Native.Syscall.chmod (
+						path,
+						Mono.Unix.Native.FilePermissions.S_IRUSR |
+						Mono.Unix.Native.FilePermissions.S_IWUSR);
 				}
 
 				stream = file_stream;
Index: BeagleClient/Indexable.cs
===================================================================
--- BeagleClient/Indexable.cs.orig
+++ BeagleClient/Indexable.cs
@@ -326,7 +326,7 @@ namespace Beagle {
 						Logger.Log.Debug ("Cleaning up {0} ({1})", contentUri.LocalPath, Uri);
 
 					try {
-						File.Delete (contentUri.LocalPath);
+						FileSystem.PosixDelete (contentUri.LocalPath);
 					} catch { 
 						// It might be gone already, so catch the exception.
 					}
@@ -339,7 +339,7 @@ namespace Beagle {
 						Logger.Log.Debug ("Cleaning up {0} ({1})", hotContentUri.LocalPath, Uri);
 
 					try {
-						File.Delete (hotContentUri.LocalPath);
+						FileSystem.PosixDelete (hotContentUri.LocalPath);
 					} catch {
 						// Ditto
 					}
@@ -635,7 +635,7 @@ namespace Beagle {
 			// Make sure the temporary file is only readable by the owner.
 			// FIXME: There is probably a race here.  Could some malicious program
 			// do something to the file between creation and the chmod?
-			Mono.Unix.Native.Syscall.chmod (filename, (Mono.Unix.Native.FilePermissions) 256);
+			Mono.Unix.Native.Syscall.chmod (filename, Mono.Unix.Native.FilePermissions.S_IRUSR);
 
 			BufferedStream bufferedStream = new BufferedStream (fileStream);
 			StreamWriter writer = new StreamWriter (bufferedStream);
@@ -671,7 +671,7 @@ namespace Beagle {
 			// Make sure the temporary file is only readable by the owner.
 			// FIXME: There is probably a race here.  Could some malicious program
 			// do something to the file between creation and the chmod?
-			Mono.Unix.Native.Syscall.chmod (filename, (Mono.Unix.Native.FilePermissions) 256);
+			Mono.Unix.Native.Syscall.chmod (filename, Mono.Unix.Native.FilePermissions.S_IRUSR);
 
 			BufferedStream bufferedStream = new BufferedStream (fileStream);
 
Index: Filters/FilterArchive.cs
===================================================================
--- Filters/FilterArchive.cs.orig
+++ Filters/FilterArchive.cs
@@ -227,12 +227,17 @@ namespace Beagle.Filters {
 				return null;
 
 			string filename = FileSystem.GetTempFileName (extension);
-			FileStream file_stream = File.OpenWrite (filename);
+			FileStream file_stream = new FileStream (filename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite); // FileShare.ReadWrite needed for setting the mtime
+
+			// When we dump the contents of an indexable into a file, we
+			// expect to use it again soon.
+			FileAdvise.PreLoad (file_stream);
 
 			//Log.Debug ("Storing archive contents in {0}", filename);
-			
-			Mono.Unix.Native.Syscall.chmod (filename, (Mono.Unix.Native.FilePermissions) 384); // 384 is 0600
-			
+
+			File.SetLastWriteTimeUtc (filename, mtime); // change this before making read-only
+			Mono.Unix.Native.Syscall.chmod (filename, Mono.Unix.Native.FilePermissions.S_IRUSR);
+
 			BufferedStream buffered_stream = new BufferedStream (file_stream);
 
 			byte [] buffer = new byte [8192];
@@ -286,14 +291,10 @@ namespace Beagle.Filters {
 			buffered_stream.Close ();
 
 			if (skip_file) {
-				File.Delete (filename);
+				FileSystem.PosixDelete (filename);
 				return null;
 			}
 
-			File.SetLastWriteTimeUtc (filename, mtime);
-
-			Mono.Unix.Native.Syscall.chmod (filename, (Mono.Unix.Native.FilePermissions) 256); // 256 is 0400
-
 			return filename;
 		}
 
