FileMode.Out

Chris Miller chris at dprogramming.com
Sun Jul 23 23:35:50 PDT 2006


On Mon, 24 Jul 2006 02:17:07 -0400, Chris Miller <chris at dprogramming.com>  
wrote:

> I open a File with FileMode.Out and I can't read the file from other  
> programs while the file is open. Why is the file sharing set this way?  
> It has been annoying me all the way from my early days of D.
>
> I often open a file at program startup and write log information to it;  
> sometimes the program runs 24 hours a day. What good is a log file if I  
> can never read it?
>
> Please set file sharing to always share read access. It should be 2  
> simple fixes to stream.d in function parseMode.
>
> Also, there could be a way to extend File to allow specifying file  
> sharing. Since not every environment offers this functionality, it could  
> just be a request, and there could also be another function to call that  
> returns true if sharing is definitely available, or false if it is not  
> or cannot be determined.
>
> A lot of times I just go back to C FILE*s because D's streams just feel  
> icky.
>
>
> Oh yeah, I forgot, opening for write causes sharing write access. This  
> is really bizarre. It should always share read and should never share  
> write (unless there's a way to change it per File).
>
>
> I'm going to be pretty mad if this isn't fixed for 1.0 - especially the  
> sharing write.


Sorry, forget the last part, it doesn't share write access.

Here's an updated parseMode function to always only give read sharing:


   private void parseMode(int mode,
			 out int access,
			 out int share,
			 out int createMode) {
     version (Win32) {
       share |= FILE_SHARE_READ;
       if (mode & FileMode.In) {
	access |= GENERIC_READ;
	createMode = OPEN_EXISTING;
       }
       if (mode & FileMode.Out) {
	access |= GENERIC_WRITE;
	createMode = OPEN_ALWAYS; // will create if not present
       }
       if ((mode & FileMode.OutNew) == FileMode.OutNew) {
	createMode = CREATE_ALWAYS; // resets file
       }
     }
     version (linux) {
       share = 0444;
       if (mode & FileMode.In) {
	access = O_RDONLY;
       }
       if (mode & FileMode.Out) {
	createMode = O_CREAT; // will create if not present
	access = O_WRONLY;
       }
       if (access == (O_WRONLY | O_RDONLY)) {
	access = O_RDWR;
       }
       if ((mode & FileMode.OutNew) == FileMode.OutNew) {
	access |= O_TRUNC; // resets file
       }
     }
   }


Another concern, when opening for write and not specifying read, should it  
imply also opening for read?



More information about the Digitalmars-d mailing list