Skip to content

Fix struct dirent d_type macro test #348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

sjshuck
Copy link

@sjshuck sjshuck commented Jul 3, 2025

Fixes #347. Credit to Andrew Gunnerson (@chenxiaolong).

Test program:

module Main (main) where

import Control.Exception                (bracket)
import Data.Function                    (fix)
import Foreign.C.String                 (peekCString)
import System.Posix.Directory
import System.Posix.Directory.Internals
import Text.Printf                      (printf)

printDirEnt :: DirEnt -> IO ()
printDirEnt dirEnt = do
    dName <- dirEntName dirEnt >>= peekCString
    dType <- dirEntType dirEnt
    printf "%-20s%s\n" dName (show dType)

main :: IO ()
main = bracket (openDirStream "/home/steve/foo") closeDirStream $ \dirStream ->
    fix $ \go -> readDirStreamWith printDirEnt dirStream >>= mapM_ (\_ -> go)

Output for unix-2.8.7.0:

.                   DirType 0
..                  DirType 0
a regular file.txt  DirType 0
a directory.d       DirType 0
a symlink           DirType 0

Output with this change:

.                   DirType 4
..                  DirType 4
a regular file.txt  DirType 8
a directory.d       DirType 4
a symlink           DirType 10

@Bodigrim
Copy link
Contributor

Bodigrim commented Jul 3, 2025

@sjshuck could you add a test please?

@sjshuck
Copy link
Author

sjshuck commented Jul 3, 2025

I'm a little stuck on how to skip the test if d_type isn't defined or if the filesystem doesn't set non-zero values for it. Open to suggestions.

Do all test containers have a cc installed on them?

@Bodigrim
Copy link
Contributor

Bodigrim commented Jul 3, 2025

Do all test containers have a cc installed on them?

I think so, these are generic CI machines, not bare minimum containers.

Is #ifdef HAVE_STRUCT_DIRENT_D_TYPE inaccessible in the test suite?

@sjshuck
Copy link
Author

sjshuck commented Jul 4, 2025

Is #ifdef HAVE_STRUCT_DIRENT_D_TYPE inaccessible in the test suite?

I'm not sure TBH. I don't know how standard of a macro that is, how much it has to do with GNU, Linux, ... I did the test another way, compiling optimistically with d_type, and if the compilation fails, I'm assuming it's because that isn't defined, and the Haskell test is skipped. I could scrape stderr and find mention of "d_type" in it to be extra sure, I suppose.

@sjshuck sjshuck force-pushed the fix-dirent-d-type branch from a459fc8 to 9eb439b Compare July 4, 2025 02:38
@sjshuck sjshuck force-pushed the fix-dirent-d-type branch from 9eb439b to c18caa0 Compare July 4, 2025 03:49
…Type

/proc/self is guaranteed to exist as a symlink on Linux and procfs has
emitted its d_type for decades.
@sjshuck
Copy link
Author

sjshuck commented Jul 5, 2025

Temporarily changed back to the incorrect #ifdef HAVE_DIRENT_D_TYPE and the test on my Linux machine fails with

DirEnt of /proc/self has DirType 0; expected DirType 10!

@sjshuck sjshuck requested review from hasufell and Bodigrim July 5, 2025 13:46
Copy link
Contributor

@Bodigrim Bodigrim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

@sjshuck
Copy link
Author

sjshuck commented Jul 8, 2025

Anything else needed of me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

System.Posix.Directory.Internals.dirEntType always returns DT_UNKNOWN
3 participants