Browse Source

core:sys/linux - implemented inotify

core:sys/linux - added constants and spacing
A1029384756 9 months ago
parent
commit
bb20338987
4 changed files with 72 additions and 3 deletions
  1. 22 0
      core/sys/linux/bits.odin
  2. 8 0
      core/sys/linux/constants.odin
  3. 23 3
      core/sys/linux/sys.odin
  4. 19 0
      core/sys/linux/types.odin

+ 22 - 0
core/sys/linux/bits.odin

@@ -519,6 +519,28 @@ Fd_Poll_Events_Bits :: enum {
 	RDHUP  = 13,
 	RDHUP  = 13,
 }
 }
 
 
+Inotify_Init_Bits :: enum {
+	IN_NONBLOCK = 11,
+	IN_CLOEXEC  = 19,
+}
+
+Inotify_Events_Bits :: enum u32 {
+	IN_ACCESS        = 0,
+	IN_MODIFY        = 1,
+	IN_ATTRIB        = 2,
+	IN_CLOSE_WRITE   = 3,
+	IN_CLOSE_NOWRITE = 4,
+	IN_CLOSE         = (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE),
+	IN_OPEN          = 5,
+	IN_MOVED_FROM    = 6,
+	IN_MOVED_TO      = 7,
+	IN_MOVE          = (IN_MOVED_FROM | IN_MOVED_TO),
+	IN_CREATE        = 8,
+	IN_DELETE        = 9,
+	IN_DELETE_SELF   = 10,
+	IN_MOVE_SELF     = 11,
+}
+
 /*
 /*
 	Bits for Mem_Protection bitfield
 	Bits for Mem_Protection bitfield
 */
 */

+ 8 - 0
core/sys/linux/constants.odin

@@ -135,6 +135,14 @@ STATX_BASIC_STATS :: Statx_Mask {
 	.BLOCKS,
 	.BLOCKS,
 }
 }
 
 
+IN_ONLYDIR     :: 0x01000000
+IN_DONT_FOLLOW :: 0x02000000
+IN_EXCL_UNLINK :: 0x04000000
+IN_MASK_CREATE :: 0x10000000
+IN_MASK_ADD    :: 0x20000000
+IN_ISDIR       :: 0x40000000
+IN_ONESHOT     :: 0x80000000
+
 /*
 /*
 	Tell `shmget` to create a new key
 	Tell `shmget` to create a new key
 */
 */

+ 23 - 3
core/sys/linux/sys.odin

@@ -2536,11 +2536,31 @@ waitid :: proc "contextless" (id_type: Id_Type, id: Id, sig_info: ^Sig_Info, opt
 
 
 // TODO(flysand): ioprio_get
 // TODO(flysand): ioprio_get
 
 
-// TODO(flysand): inotify_init
+inotify_init :: proc "contextless" () -> (Fd, Errno) {
+	ret := syscall(SYS_inotify_init)
+	return errno_unwrap(ret, Fd)
+}
+
+inotify_init1 :: proc "contextless" (flags: Inotify_Init_Flags) -> (Fd, Errno) {
+	ret := syscall(SYS_inotify_init1, transmute(i32)flags)
+	return errno_unwrap(ret, Fd)
+}
 
 
-// TODO(flysand): inotify_add_watch
+inotify_add_watch :: proc "contextless" (fd: Fd, pathname: cstring, mask: Inotify_Event_Mask) -> (Wd, Errno) {
+	ret := syscall(SYS_inotify_add_watch, fd, transmute(uintptr) pathname, transmute(u32) mask)
+	return errno_unwrap(ret, Wd)
+}
 
 
-// TODO(flysand): inotify_rm_watch
+inotify_rm_watch :: proc "contextless" (fd: Fd, wd: Wd) -> (Errno) {
+	ret := syscall(SYS_inotify_rm_watch, fd, wd)
+	return Errno(-ret)
+}
+
+// helper procedure to access the data within an `Inotify_Event`
+// since Odin doesn't have an alternative to `__flexarr`
+inotify_event_name :: proc "contextless" (event: ^Inotify_Event) -> cstring {
+	return transmute(cstring)&event.name
+}
 
 
 // TODO(flysand): migrate_pages
 // TODO(flysand): migrate_pages
 
 

+ 19 - 0
core/sys/linux/types.odin

@@ -30,6 +30,11 @@ Id :: distinct uint
 */
 */
 Fd  :: distinct i32
 Fd  :: distinct i32
 
 
+/*
+	Represents a watch descriptor.
+*/
+Wd  :: distinct i32
+
 /*
 /*
 	Type for PID file descriptors.
 	Type for PID file descriptors.
 */
 */
@@ -343,6 +348,20 @@ Poll_Fd :: struct {
 	revents: Fd_Poll_Events,
 	revents: Fd_Poll_Events,
 }
 }
 
 
+Inotify_Init_Flags :: bit_set[Inotify_Init_Bits; i32];
+
+Inotify_Event :: struct {
+	wd: Wd,
+	mask: Inotify_Event_Mask,
+	cookie: u32,
+	len: u32,
+	// used in place of __flexarr
+	// use inotify_event_name to read
+	name: [0]u8,
+}
+
+Inotify_Event_Mask :: bit_set[Inotify_Events_Bits; u32]
+
 /*
 /*
 	Specifies protection for memory pages.
 	Specifies protection for memory pages.
 */
 */