#4363: openFile sharing permissions are inconsistent across platforms
---------------------------------+------------------------------------------
    Reporter:  jystic            |       Owner:                             
        Type:  bug               |      Status:  new                        
    Priority:  high              |   Milestone:  7.6.1                      
   Component:  libraries/base    |     Version:  6.12.3                     
    Keywords:                    |          Os:  Windows                    
Architecture:  Unknown/Multiple  |     Failure:  Incorrect result at runtime
  Difficulty:                    |    Testcase:                             
   Blockedby:                    |    Blocking:                             
     Related:                    |  
---------------------------------+------------------------------------------

Comment(by jystic):

 On the topic of sharing permissions being inconsistent across platforms,
 there is another scenario which I have noticed that probably needs
 attention as well. Perhaps it needs its own ticket, but it could be part
 of this clean up.

 When we open a file on POSIX systems, it is legal to delete or rename the
 file even though it is open. On Windows however, we are locking the file
 so that it may not be deleted or renamed.

 This is purely because we are using the POSIX style APIs (i.e. _wsopen)
 for opening files. These APIs do not have flags for specifying all the
 possible sharing permissions and unfortunately Windows defaults to
 preventing open files from being deleted or renamed.

 This program on POSIX will allow the test file to be deleted while the
 program is running (standard behaviour):

 {{{


 #include <stdio.h>
 #include <fcntl.h>
 #include <string.h>
 #include <unistd.h>

 int main()
 {
     const char *file = "./test.txt";

     int fd;
     int i = 0;
     char buf[16];

     fd = open(file, O_WRONLY | O_CREAT, 0644);

     while (1)
     {
         sprintf(buf, "%d\n", i++);
         write(fd, buf, strlen(buf));
         sleep(1);
     }

     return 0;
 }


 }}}

 The following program achieves the same behaviour on Windows by using the
 FILE_SHARE_DELETE flag:

 {{{

 #include <stdio.h>
 #include <fcntl.h>
 #include <windows.h>

 int main()
 {
     const wchar_t *file = L"test.txt";

     int fd;
     HANDLE h;
     int i = 0;
     char buf[16];

     h = CreateFileW(file,
             GENERIC_WRITE,
             FILE_SHARE_READ | FILE_SHARE_DELETE,
             NULL,
             CREATE_NEW,
             FILE_ATTRIBUTE_NORMAL,
             NULL);

     fd = _open_osfhandle((intptr_t)h, 0);

     while (1)
     {
         sprintf(buf, "%d\n", i++);
         write(fd, buf, strlen(buf));
         Sleep(1000);
     }

     return 0;
 }

 }}}

-- 
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/4363#comment:12>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
Glasgow-haskell-bugs@haskell.org
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs

Reply via email to