]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Synchronize la_open function in bsdtar_windows.c
authorMichihiro NAKAJIMA <ggcueroad@gmail.com>
Sat, 21 Feb 2009 03:08:18 +0000 (22:08 -0500)
committerMichihiro NAKAJIMA <ggcueroad@gmail.com>
Sat, 21 Feb 2009 03:08:18 +0000 (22:08 -0500)
with the one in libarchive_windows.c for
maintenance to be easily.

SVN-Revision: 681

tar/bsdtar_windows.c

index 29ee93919eece878cdbc18313a6ca621c55da8e6..b92b6696473f0512e7d29822df0c28cfd13fb58b 100644 (file)
@@ -663,38 +663,70 @@ la_open(const char *path, int flags, ...)
        va_start(ap, flags);
        pmode = va_arg(ap, int);
        va_end(ap);
-       if (path[0] == '.' && path[1] == '\0' && flags == O_RDONLY) {
-               HANDLE handle;
-       
-               handle = la_CreateFile(path, 0, 0, NULL, OPEN_EXISTING,
-                       FILE_FLAG_BACKUP_SEMANTICS | FILE_ATTRIBUTE_READONLY,
-                       NULL);
-               if (handle == INVALID_HANDLE_VALUE) {
+       ws = NULL;
+       if ((flags & ~O_BINARY) == O_RDONLY) {
+               /*
+                * When we open a directory, _open function returns 
+                * "Permission denied" error.
+                */
+               attr = GetFileAttributesA(path);
+               if (attr == -1 && GetLastError() == ERROR_PATH_NOT_FOUND) {
+                       ws = permissive_name(path);
+                       if (ws == NULL) {
+                               errno = EINVAL;
+                               return (-1);
+                       }
+                       attr = GetFileAttributesW(ws);
+               }
+               if (attr == -1) {
                        _dosmaperr(GetLastError());
+                       free(ws);
                        return (-1);
                }
-               r = _open_osfhandle((intptr_t)handle, _O_RDONLY);
-
-               return (r);
-       }
-       r = _open(path, flags, pmode);
-       if (r < 0 && errno == EACCES && (flags & O_CREAT) != 0) {
-               /* simular other POSIX system action to pass a test */
-               attr = GetFileAttributesA(path);
-               if (attr == -1)
-                       _dosmaperr(GetLastError());
-               else if (attr & FILE_ATTRIBUTE_DIRECTORY)
-                       errno = EISDIR;
-               else
-                       errno = EACCES;
-               return (-1);
+               if (attr & FILE_ATTRIBUTE_DIRECTORY) {
+                       HANDLE handle;
+
+                       if (ws != NULL)
+                               handle = CreateFileW(ws, 0, 0, NULL,
+                                   OPEN_EXISTING,
+                                   FILE_FLAG_BACKUP_SEMANTICS |
+                                   FILE_ATTRIBUTE_READONLY,
+                                       NULL);
+                       else
+                               handle = CreateFileA(path, 0, 0, NULL,
+                                   OPEN_EXISTING,
+                                   FILE_FLAG_BACKUP_SEMANTICS |
+                                   FILE_ATTRIBUTE_READONLY,
+                                       NULL);
+                       free(ws);
+                       if (handle == INVALID_HANDLE_VALUE) {
+                               _dosmaperr(GetLastError());
+                               return (-1);
+                       }
+                       r = _open_osfhandle((intptr_t)handle, _O_RDONLY);
+                       return (r);
+               }
        }
-       if (r >= 0 || errno != ENOENT)
-               return (r);
-       ws = permissive_name(path);
        if (ws == NULL) {
-               errno = EINVAL;
-               return (-1);
+               r = _open(path, flags, pmode);
+               if (r < 0 && errno == EACCES && (flags & O_CREAT) != 0) {
+                       /* simular other POSIX system action to pass a test */
+                       attr = GetFileAttributesA(path);
+                       if (attr == -1)
+                               _dosmaperr(GetLastError());
+                       else if (attr & FILE_ATTRIBUTE_DIRECTORY)
+                               errno = EISDIR;
+                       else
+                               errno = EACCES;
+                       return (-1);
+               }
+               if (r >= 0 || errno != ENOENT)
+                       return (r);
+               ws = permissive_name(path);
+               if (ws == NULL) {
+                       errno = EINVAL;
+                       return (-1);
+               }
        }
        r = _wopen(ws, flags, pmode);
        if (r < 0 && errno == EACCES && (flags & O_CREAT) != 0) {