Jelajahi Sumber

Fix O_NOFOLLOW typo. Add Linux support for POSIX fcntl.

Isaac Andrade 1 tahun lalu
induk
melakukan
aa91479870
1 mengubah file dengan 109 tambahan dan 43 penghapusan
  1. 109 43
      core/sys/posix/fcntl.odin

+ 109 - 43
core/sys/posix/fcntl.odin

@@ -107,11 +107,11 @@ O_Flag_Bits :: enum c.int {
 	// If terminal device, do not make it the controlling terminal for the process.
 	NOCTTY    = log2(O_NOCTTY),
 	// Don't follow symbolic links, fail with errno ELOOP.
-	NOFOLLOW  = log2(O_NOFOLOW),
+	NOFOLLOW  = log2(O_NOFOLLOW),
 	// If exists and regular, truncate the length to 0.
 	TRUNC     = log2(O_TRUNC),
 
- 	// NOTE: use with `posix.O_TTY_INIT + { .OTHER_FLAG, .OTHER_FLAG }`, unfortunately can't be in
+	// NOTE: use with `posix.O_TTY_INIT + { .OTHER_FLAG, .OTHER_FLAG }`, unfortunately can't be in
 	// this bit set enum because it is 0 on some platforms and a value on others.
 	// TTY_INIT = O_TTY_INIT,
 
@@ -123,7 +123,7 @@ O_Flag_Bits :: enum c.int {
 	NONBLOCK  = log2(O_NONBLOCK),
 	// Write I/O shall complete as defined by synchronized I/O file integrity completion.
 	SYNC      = log2(O_SYNC),
- 	// NOTE: use with `posix.O_RSYNC + { .OTHER_FLAG, .OTHER_FLAG }`, unfortunately can't be in
+	// NOTE: use with `posix.O_RSYNC + { .OTHER_FLAG, .OTHER_FLAG }`, unfortunately can't be in
 	// this bit set enum because it is 0 on some platforms and a value on others.
 	// RSYNC = O_RSYNC,
 
@@ -135,7 +135,6 @@ O_Flag_Bits :: enum c.int {
 	WRONLY    = log2(O_WRONLY),
 	// Reading only.
 	// RDONLY = 0, // Default
-
 }
 O_Flags :: bit_set[O_Flag_Bits; c.int]
 
@@ -152,8 +151,8 @@ AT_Flags :: bit_set[AT_Flag_Bits; c.int]
 
 when ODIN_OS == .Darwin {
 
-	off_t  :: distinct c.int64_t
-	pid_t  :: distinct c.int32_t
+	off_t :: distinct c.int64_t
+	pid_t :: distinct c.int32_t
 
 	F_DUPFD         :: 0
 	F_DUPFD_CLOEXEC :: 67
@@ -178,7 +177,7 @@ when ODIN_OS == .Darwin {
 	O_DIRECTORY :: 0x00100000
 	O_EXCL      :: 0x00000800
 	O_NOCTTY    :: 0x00020000
-	O_NOFOLOW   :: 0x00000100
+	O_NOFOLLOW   :: 0x00000100
 	O_TRUNC     :: 0x00000400
 
 	_O_TTY_INIT :: 0
@@ -189,18 +188,18 @@ when ODIN_OS == .Darwin {
 	O_NONBLOCK :: 0x00000004
 	O_SYNC     :: 0x0080
 
-	_O_RSYNC   :: 0
-	O_RSYNC    :: O_Flags{}
+	_O_RSYNC :: 0
+	O_RSYNC  :: O_Flags{}
 
-	O_EXEC    :: 0x40000000
-	O_RDONLY  :: 0
-	O_RDWR    :: 0x0002
-	O_WRONLY  :: 0x0001
+	O_EXEC   :: 0x40000000
+	O_RDONLY :: 0
+	O_RDWR   :: 0x0002
+	O_WRONLY :: 0x0001
 
 	_O_SEARCH :: O_EXEC | O_DIRECTORY
-	O_SEARCH  :: O_Flags{ .EXEC, .DIRECTORY }
+	O_SEARCH  :: O_Flags{.EXEC, .DIRECTORY}
 
-	AT_FDCWD: FD: -2
+	AT_FDCWD: FD : -2
 
 	AT_EACCESS          :: 0x0010
 	AT_SYMLINK_NOFOLLOW :: 0x0020
@@ -217,8 +216,8 @@ when ODIN_OS == .Darwin {
 
 } else when ODIN_OS == .FreeBSD {
 
-	off_t  :: distinct c.int64_t
-	pid_t  :: distinct c.int32_t
+	off_t :: distinct c.int64_t
+	pid_t :: distinct c.int32_t
 
 	F_DUPFD         :: 0
 	F_DUPFD_CLOEXEC :: 17
@@ -243,7 +242,7 @@ when ODIN_OS == .Darwin {
 	O_DIRECTORY :: 0x00020000
 	O_EXCL      :: 0x0800
 	O_NOCTTY    :: 0x8000
-	O_NOFOLOW   :: 0x0100
+	O_NOFOLLOW   :: 0x0100
 	O_TRUNC     :: 0x0400
 
 	_O_TTY_INIT :: 0x00080000
@@ -256,15 +255,15 @@ when ODIN_OS == .Darwin {
 	_O_RSYNC   :: 0
 	O_RSYNC    :: O_Flags{} // NOTE: not defined in headers
 
-	O_EXEC    :: 0x00040000
-	O_RDONLY  :: 0
-	O_RDWR    :: 0x0002
-	O_WRONLY  :: 0x0001
+	O_EXEC   :: 0x00040000
+	O_RDONLY :: 0
+	O_RDWR   :: 0x0002
+	O_WRONLY :: 0x0001
 
 	_O_SEARCH :: O_EXEC
 	O_SEARCH  :: O_Flags{ .EXEC }
 
-	AT_FDCWD: FD: -100
+	AT_FDCWD: FD : -100
 
 	AT_EACCESS          :: 0x0100
 	AT_SYMLINK_NOFOLLOW :: 0x0200
@@ -282,8 +281,8 @@ when ODIN_OS == .Darwin {
 
 } else when ODIN_OS == .NetBSD {
 
-	off_t  :: distinct c.int64_t
-	pid_t  :: distinct c.int32_t
+	off_t :: distinct c.int64_t
+	pid_t :: distinct c.int32_t
 
 	F_DUPFD         :: 0
 	F_DUPFD_CLOEXEC :: 12
@@ -308,7 +307,7 @@ when ODIN_OS == .Darwin {
 	O_DIRECTORY :: 0x0020000
 	O_EXCL      :: 0x0800
 	O_NOCTTY    :: 0x8000
-	O_NOFOLOW   :: 0x0100
+	O_NOFOLLOW   :: 0x0100
 	O_TRUNC     :: 0x0400
 
 	_O_TTY_INIT :: 0
@@ -319,19 +318,19 @@ when ODIN_OS == .Darwin {
 	O_NONBLOCK :: 0x0004
 	O_SYNC     :: 0x0080
 
-	_O_RSYNC   :: 0x0002
-	O_RSYNC    :: O_Flags{O_Flag_Bits(log2(_O_RSYNC))}
+	_O_RSYNC :: 0x0002
+	O_RSYNC  :: O_Flags{O_Flag_Bits(log2(_O_RSYNC))}
 
 
-	O_EXEC    :: 0x04000000
-	O_RDONLY  :: 0
-	O_RDWR    :: 0x0002
-	O_WRONLY  :: 0x0001
+	O_EXEC   :: 0x04000000
+	O_RDONLY :: 0
+	O_RDWR   :: 0x0002
+	O_WRONLY :: 0x0001
 
 	_O_SEARCH :: 0x00800000
 	O_SEARCH  :: O_Flags{O_Flag_Bits(log2(_O_SEARCH))}
 
-	AT_FDCWD: FD: -100
+	AT_FDCWD: FD : -100
 
 	AT_EACCESS          :: 0x100
 	AT_SYMLINK_NOFOLLOW :: 0x200
@@ -347,8 +346,8 @@ when ODIN_OS == .Darwin {
 	}
 } else when ODIN_OS == .OpenBSD {
 
-	off_t  :: distinct c.int64_t
-	pid_t  :: distinct c.int32_t
+	off_t :: distinct c.int64_t
+	pid_t :: distinct c.int32_t
 
 	F_DUPFD         :: 0
 	F_DUPFD_CLOEXEC :: 10
@@ -373,7 +372,7 @@ when ODIN_OS == .Darwin {
 	O_DIRECTORY :: 0x20000
 	O_EXCL      :: 0x0800
 	O_NOCTTY    :: 0x8000
-	O_NOFOLOW   :: 0x0100
+	O_NOFOLLOW   :: 0x0100
 	O_TRUNC     :: 0x0400
 
 	_O_TTY_INIT :: 0
@@ -384,18 +383,18 @@ when ODIN_OS == .Darwin {
 	O_NONBLOCK :: 0x0004
 	O_SYNC     :: 0x0080
 
-	_O_RSYNC   :: O_SYNC
-	O_RSYNC    :: O_Flags{ .SYNC }
+	_O_RSYNC :: O_SYNC
+	O_RSYNC  :: O_Flags{.SYNC}
 
-	O_EXEC    :: 0x04000000 // NOTE: not defined in the headers
-	O_RDONLY  :: 0
-	O_RDWR    :: 0x0002
-	O_WRONLY  :: 0x0001
+	O_EXEC   :: 0x04000000 // NOTE: not defined in the headers
+	O_RDONLY :: 0
+	O_RDWR   :: 0x0002
+	O_WRONLY :: 0x0001
 
 	_O_SEARCH :: 0
 	O_SEARCH  :: O_Flags{} // NOTE: not defined in the headers
 
-	AT_FDCWD: FD: -100
+	AT_FDCWD: FD : -100
 
 	AT_EACCESS          :: 0x01
 	AT_SYMLINK_NOFOLLOW :: 0x02
@@ -410,6 +409,73 @@ when ODIN_OS == .Darwin {
 		l_whence: c.short,   /* [PSX] flag (Whence) of starting offset */
 	}
 
+} else when ODIN_OS == .Linux {
+
+	off_t :: distinct c.int64_t
+	pid_t :: distinct c.int
+
+	F_DUPFD  :: 0 // Duplicate file descriptor.
+	F_GETFD  :: 1 /* Get file descriptor flags.  */
+	F_SETFD  :: 2 /* Set file descriptor flags.  */
+	F_GETFL  :: 3 /* Get file status flags.  */
+	F_SETFL  :: 4 /* Set file status flags.  */
+	F_GETLK  :: 5 /* Get record locking info.  */
+	F_SETLK  :: 6 /* Set record locking info (non-blocking).  */
+	F_SETLKW :: 7 /* Set record locking info (blocking).  */
+	F_SETOWN :: 8 /* Get owner (process receiving SIGIO).  */
+	F_GETOWN :: 9 /* Set owner (process receiving SIGIO).  */
+	F_RDLCK  :: 0 /* Read lock.  */
+	F_UNLCK  :: 2 /* Remove lock.  */
+	F_WRLCK  :: 1 /* Write lock.  */
+
+	F_DUPFD_CLOEXEC :: 1030 /* Duplicate file descriptor with close-on-exit set.  */
+
+	FD_CLOEXEC :: 1
+
+	O_CREAT     :: 0o0_000_100
+	O_EXCL      :: 0o0_000_200
+	O_NOCTTY    :: 0o0_000_400
+	O_TRUNC     :: 0o0_001_000
+	O_DIRECTORY :: 0o0_200_000
+	O_NOFOLLOW  :: 0o0_400_000
+	O_CLOEXEC   :: 0o2_000_000
+
+	_O_TTY_INIT :: 0
+	O_TTY_INIT  :: O_Flags{}
+
+	O_APPEND   :: 0o0_002_000
+	O_NONBLOCK :: 0o0_004_000
+	O_DSYNC    :: 0o0_010_000
+	O_SYNC     :: 0o4_010_000
+
+	_O_RSYNC :: 0
+	O_RSYNC  :: O_Flags{}
+
+	// NOTE: Not implemented in Linux
+	O_EXEC   :: 0
+
+	O_RDONLY :: 0
+	O_WRONLY :: 0o1
+	O_RDWR   :: 0o2
+
+	_O_SEARCH :: 0
+	O_SEARCH  :: O_Flags{}
+
+	AT_FDCWD: FD : -100 // Special value used to indicate the *at functions should use the current working directory.
+
+	AT_EACCESS          :: 0x200 // Test access permitted for effective IDs, not real IDs.
+	AT_SYMLINK_NOFOLLOW :: 0x100 // Do not follow symbolic links.
+	AT_SYMLINK_FOLLOW   :: 0x400 // Follow symbolic links.
+	AT_REMOVEDIR        :: 0x200 // Remove directory instead of unlinking file.
+
+	flock :: struct {
+		l_start:  off_t,     // [PSX] relative offset in bytes.
+		l_len:    off_t,     // [PSX] size; if 0 then until EOF.
+		l_pid:    pid_t,     // [PSX] process ID of the process holding the lock.
+		l_type:   Lock_Type, // [PSX] type of lock.
+		l_whence: c.short,   // [PSX] flag (Whence) of starting offset.
+	}
+
 } else {
 	#panic("posix is unimplemented for the current target")
 }