[PATCH 2/2] ntdll: Use swapping method to return . and .. as first entries.

Grazvydas Ignotas notasas at gmail.com
Thu Jul 21 15:32:25 CDT 2011


The old method didn't handle ReturnSingleEntry and RestartScan flags
correctly. The basic idea of new method is to swap first 2 entries
returned by getdents with . and .. if . and .. are not returned first.
---
 dlls/ntdll/directory.c       |  130 ++++++++++++++++++++++++------------------
 dlls/ntdll/tests/directory.c |   13 +---
 2 files changed, 77 insertions(+), 66 deletions(-)

diff --git a/dlls/ntdll/directory.c b/dlls/ntdll/directory.c
index 42b3639..b4132ed 100644
--- a/dlls/ntdll/directory.c
+++ b/dlls/ntdll/directory.c
@@ -295,6 +295,20 @@ static void flush_dir_queue(void)
     }
 }
 
+static char *ntdll_strdup(const char *s)
+{
+    char *r;
+    int len;
+
+    if (s)
+    {
+        len = strlen(s) + 1;
+        r = RtlAllocateHeap(GetProcessHeap(), 0, len);
+        if (r)
+            return memcpy(r, s, len);
+    }
+    return NULL;
+}
 
 /***********************************************************************
  *           get_default_com_device
@@ -1497,11 +1511,14 @@ static int read_directory_getdents( int fd, IO_STATUS_BLOCK *io, void *buffer, U
                                     BOOLEAN restart_scan, FILE_INFORMATION_CLASS class )
 {
     off_t old_pos = 0;
+    off_t second_entry_pos = (off_t)-1;
     size_t size = length;
-    int res, fake_dot_dot = 1;
     char *data, local_buffer[8192];
-    KERNEL_DIRENT64 *de;
+    KERNEL_DIRENT64 *de, *de_tmp;
+    char *firstnames[2] = { NULL, NULL };
     union file_directory_info *info, *last_info = NULL;
+    const char *filename;
+    int res, which;
 
     if (size <= sizeof(local_buffer) || !(data = RtlAllocateHeap( GetProcessHeap(), 0, size )))
     {
@@ -1509,8 +1526,7 @@ static int read_directory_getdents( int fd, IO_STATUS_BLOCK *io, void *buffer, U
         data = local_buffer;
     }
 
-    if (restart_scan) lseek( fd, 0, SEEK_SET );
-    else if (length < max_dir_info_size(class))  /* we may have to return a partial entry here */
+    if (!restart_scan)
     {
         old_pos = lseek( fd, 0, SEEK_CUR );
         if (old_pos == -1 && errno == ENOENT)
@@ -1522,76 +1538,76 @@ static int read_directory_getdents( int fd, IO_STATUS_BLOCK *io, void *buffer, U
     }
 
     io->u.Status = STATUS_SUCCESS;
+    de = (KERNEL_DIRENT64 *)data;
 
+    /* read first 2 entries so we can handle "." and ".." */
+    lseek( fd, 0, SEEK_SET );
     res = getdents64( fd, data, size );
     if (res == -1)
     {
-        if (errno != ENOSYS)
-        {
-            io->u.Status = FILE_GetNtStatus();
-            res = 0;
-        }
-        goto done;
+        if (errno == ENOSYS)
+            goto done;
     }
-
-    de = (KERNEL_DIRENT64 *)data;
-
-    if (restart_scan)
+    else
     {
-        /* check if we got . and .. from getdents */
-        if (res > 0)
-        {
-            if (!strcmp( de->d_name, "." ) && res > de->d_reclen)
-            {
-                KERNEL_DIRENT64 *next_de = (KERNEL_DIRENT64 *)(data + de->d_reclen);
-                if (!strcmp( next_de->d_name, ".." )) fake_dot_dot = 0;
-            }
-        }
-        /* make sure we have enough room for both entries */
-        if (fake_dot_dot)
+        second_entry_pos = de->d_off;
+        firstnames[0] = ntdll_strdup(de->d_name);
+        if (res > de->d_reclen)
         {
-            const ULONG min_info_size = dir_info_size( class, 1 ) + dir_info_size( class, 2 );
-            if (length < min_info_size || single_entry)
-            {
-                FIXME( "not enough room %u/%u for fake . and .. entries\n", length, single_entry );
-                fake_dot_dot = 0;
-            }
+            de_tmp = (KERNEL_DIRENT64 *)((char *)de + de->d_reclen);
+            firstnames[1] = ntdll_strdup(de_tmp->d_name);
         }
+    }
+    if (!firstnames[0] || !firstnames[1])
+    {
+        FIXME( "failed to read first entries, listing will be incomplete\n" );
+    }
 
-        if (fake_dot_dot)
-        {
-            if ((info = append_entry( buffer, io, length, ".", NULL, mask, class )))
-                last_info = info;
-            if ((info = append_entry( buffer, io, length, "..", NULL, mask, class )))
-                last_info = info;
-
-            /* check if we still have enough space for the largest possible entry */
-            if (last_info && io->Information + max_dir_info_size(class) > length)
-            {
-                lseek( fd, 0, SEEK_SET );  /* reset pos to first entry */
-                res = 0;
-            }
-        }
+    if (old_pos != 0)
+        lseek( fd, old_pos, SEEK_SET );
+    if (old_pos != 0 || res == -1)
+        res = getdents64( fd, data, size );
+    if (res == -1)
+    {
+        io->u.Status = FILE_GetNtStatus();
+        res = 0;
     }
 
     while (res > 0)
     {
         res -= de->d_reclen;
-        if (de->d_ino &&
-            !(fake_dot_dot && (!strcmp( de->d_name, "." ) || !strcmp( de->d_name, ".." ))) &&
-            (info = append_entry( buffer, io, length, de->d_name, NULL, mask, class )))
+        if (de->d_ino)
         {
-            last_info = info;
-            if (io->u.Status == STATUS_BUFFER_OVERFLOW)
+            /* we must return first 2 entries as "." and "..", but getdents64()
+             * can return them anywhere, so swap first entries with "." and ".." */
+            if (old_pos == 0)
+                filename = ".";
+            else if (old_pos == second_entry_pos)
+                filename = "..";
+            else if (!strcmp( de->d_name, "." ) || !strcmp( de->d_name, ".." ))
             {
-                lseek( fd, old_pos, SEEK_SET );  /* restore pos to previous entry */
-                break;
+                which = !strcmp( de->d_name, "." ) ? 0 : 1;
+                if (firstnames[which] && (!strcmp( firstnames[which], "." ) || !strcmp( firstnames[which], ".." )))
+                    which ^= 1;
+                filename = firstnames[which];
             }
-            /* check if we still have enough space for the largest possible entry */
-            if (single_entry || io->Information + max_dir_info_size(class) > length)
+            else
+                filename = de->d_name;
+
+            if (filename && (info = append_entry( buffer, io, length, filename, NULL, mask, class )))
             {
-                if (res > 0) lseek( fd, de->d_off, SEEK_SET );  /* set pos to next entry */
-                break;
+                last_info = info;
+                if (io->u.Status == STATUS_BUFFER_OVERFLOW)
+                {
+                    lseek( fd, old_pos, SEEK_SET );  /* restore pos to previous entry */
+                    break;
+                }
+                /* check if we still have enough space for the largest possible entry */
+                if (single_entry || io->Information + max_dir_info_size(class) > length)
+                {
+                   if (res > 0) lseek( fd, de->d_off, SEEK_SET );  /* set pos to next entry */
+                   break;
+                }
             }
         }
         old_pos = de->d_off;
@@ -1608,6 +1624,8 @@ static int read_directory_getdents( int fd, IO_STATUS_BLOCK *io, void *buffer, U
     else io->u.Status = restart_scan ? STATUS_NO_SUCH_FILE : STATUS_NO_MORE_FILES;
     res = 0;
 done:
+    if (firstnames[0]) RtlFreeHeap( GetProcessHeap(), 0, firstnames[0] );
+    if (firstnames[1]) RtlFreeHeap( GetProcessHeap(), 0, firstnames[1] );
     if (data != local_buffer) RtlFreeHeap( GetProcessHeap(), 0, data );
     return res;
 }
diff --git a/dlls/ntdll/tests/directory.c b/dlls/ntdll/tests/directory.c
index e11719f..4593830 100644
--- a/dlls/ntdll/tests/directory.c
+++ b/dlls/ntdll/tests/directory.c
@@ -219,16 +219,9 @@ static void test_NtQueryDirectoryFile(BOOLEAN single_entry, BOOLEAN restart_flag
     }
     ok(numfiles < max_test_dir_size, "too many loops\n");
 
-    for (i=0; testfiles[i].name; i++) {
-        if ((strcmp(testfiles[i].name, ".") == 0 || strcmp(testfiles[i].name, "..") == 0) && (single_entry || !restart_flag)) {
-            todo_wine
-            ok(testfiles[i].nfound == 1, "Wrong number %d of %s files found (single=%d,restart=%d)\n",
-              testfiles[i].nfound, testfiles[i].description, single_entry, restart_flag);
-        } else {
-            ok(testfiles[i].nfound == 1, "Wrong number %d of %s files found (single=%d,restart=%d)\n",
-              testfiles[i].nfound, testfiles[i].description, single_entry, restart_flag);
-        }
-    }
+    for (i=0; testfiles[i].name; i++)
+        ok(testfiles[i].nfound == 1, "Wrong number %d of %s files found (single=%d,restart=%d)\n",
+           testfiles[i].nfound, testfiles[i].description, single_entry, restart_flag);
 
     pNtClose(dirh);
 done:
-- 
1.7.0.4




More information about the wine-patches mailing list