Skip to content

Safe FFI exercise: dirent layout is different on Mac OS #570

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

Closed
mgeisler opened this issue Apr 20, 2023 · 2 comments · Fixed by #572
Closed

Safe FFI exercise: dirent layout is different on Mac OS #570

mgeisler opened this issue Apr 20, 2023 · 2 comments · Fixed by #572

Comments

@mgeisler
Copy link
Collaborator

The safe FFI wrapper exercise uses the wrong layout on Mac OS.

@ericye16
Copy link
Contributor

The man page for my system says:

>     The directory entry format is defined in the file ⟨sys/dirent.h⟩ and further in the file ⟨dirent.h⟩.  When the macro
     _DARWIN_FEATURE_64_BIT_INODE is not defined (see stat(2) for more information on this macro), the dirent structure is
     defined as:

     /*** Excerpt from <sys/dirent.h> ***/
     /*
      * The dirent structure defines the format of directory entries.
      *
      * A directory entry has a struct dirent at the front of it, containing its
      * inode number, the length of the entry, and the length of the name
      * contained in the entry.  These are followed by the name padded to a 4
      * byte boundary with null bytes.  All names are guaranteed null terminated.
      * The maximum length of a name in a directory is 255.
      */

     struct dirent { /* when _DARWIN_FEATURE_64_BIT_INODE is NOT defined */
             ino_t      d_ino;                /* file number of entry */
             __uint16_t d_reclen;             /* length of this record */
             __uint8_t  d_type;               /* file type, see below */
             __uint8_t  d_namlen;             /* length of string in d_name */
             char    d_name[255 + 1];   /* name must be no longer than this */
     };

     However, when the macro _DARWIN_FEATURE_64_BIT_INODE is defined, the dirent structure is defined as:

     /*
      * The dirent structure defines the format of directory entries.
      *
      * A directory entry has a struct dirent at the front of it, containing its
      * inode number, the length of the entry, and the length of the name
      * contained in the entry.  These are followed by the name padded to a 4
      * byte boundary with null bytes.  All names are guaranteed null terminated.
      * The maximum length of a name in a directory is 1023.
      */

     struct dirent { /* when _DARWIN_FEATURE_64_BIT_INODE is defined */
             ino_t      d_fileno;     /* file number of entry */
             __uint64_t d_seekoff;    /* seek offset (optional, used by servers) */
             __uint16_t d_reclen;     /* length of this record */
             __uint16_t d_namlen;     /* length of string in d_name */
             __uint8_t  d_type;       /* file type, see below */
             char    d_name[1024];    /* name must be no longer than this */
     };

schultetwin1 added a commit to schultetwin1/comprehensive-rust that referenced this issue Apr 20, 2023
Use the macOS definition of dirent.

Fixes google#570
@mgeisler
Copy link
Collaborator Author

Thanks @ericye16! @schultetwin1 was able to open a PR based on the man page above, so we should have this fixed now.

mgeisler added a commit that referenced this issue Apr 21, 2023
* Fix safe FFI exercise on macOS

Use the macOS definition of dirent.

Fixes #570

---------

Co-authored-by: Martin Geisler <[email protected]>
mgeisler added a commit that referenced this issue Jun 19, 2023
This would have helped us catch #570.
mgeisler added a commit that referenced this issue Jun 20, 2023
This would have helped us catch #570.
mgeisler added a commit that referenced this issue Jun 22, 2023
* Run builds on both Mac OS and Linux

This would have helped us catch #570.

* Fix MacOS CI (#848)

* Revert unnecessary changes

The changes might be good, but I want to keep this PR small and
focused. If we end up with the extra `cfg` statements, we should
include a comment to let students know what they do: we’re targeting
people new to Rust, so we need to be careful with explanations.

---------

Co-authored-by: Dominik Maier <[email protected]>
NoahDragon pushed a commit to wnghl/comprehensive-rust that referenced this issue Jul 19, 2023
* Fix safe FFI exercise on macOS

Use the macOS definition of dirent.

Fixes google#570

---------

Co-authored-by: Martin Geisler <[email protected]>
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 a pull request may close this issue.

2 participants