Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 3833

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T06:23:08+00:00 2024-11-26T06:23:08+00:00

I am learning how linux handles file changes

  • 61k

I think it's always important to get to grips with the basics and not just apply libraries.

How to monitor file changes?

monitor the monitor

What do you do, when you want to familiarize yourself with an area as a software developer? That's right, you look for the corresponding specification.

Each operating system has its own mechanism to react to file changes. Since I work on Unix, I only looked at how Unix works today. Other operating systems like Windows, BSD or macOS have their own mechanism.


Inotify – Monitoring filesystem events

There was dnotify in the beginning, but that's history!

Quoted from Wikipedia:

inotify (inode notify) is a Linux kernel subsystem created by John McCutchan, which monitors changes to the filesystem, and reports those changes to applications. It can be used to automatically update directory views, reload configuration files, log changes, backup, synchronize, and upload.


From the Linux API documentation, we can already learn quite a lot about the mechanism.

Here is just a short quote:

The inotify API provides a mechanism for monitoring filesystem events. Inotify can be used to monitor individual files, or to monitor directories. When a directory is monitored, inotify will return events for the directory itself, and for files inside the directory.

You can also take a closer look at the Linux source code.

Here is the structure of the event:

/*  * struct inotify_event - structure read from the inotify device for each event  *  * When you are watching a directory, you will receive the filename for events  * such as IN_CREATE, IN_DELETE, IN_OPEN, IN_CLOSE, ..., relative to the wd.  */ struct inotify_event {     __s32       wd;     /* watch descriptor */     __u32       mask;       /* watch mask */     __u32       cookie;     /* cookie to synchronize two events */     __u32       len;        /* length (including nulls) of name */     char        name[]; /* stub for possible name */ };  
Enter fullscreen mode Exit fullscreen mode

If we take a closer look at the mode of action and data structures, we get a feeling for how the whole thing works.


Implementation in Go

Let's get back to Go. Of course, Go has good coverage of the Linux API, and we also find functions for Inotify here.

For Windows, of course, there is something comparable.

Under Go, as you would expect, there is an excellent library covering Linux, BSD and Windows.

Thus, one does not have to worry about the individual implementations.

So we put it all together.

First, we need to initialize a new watcher.

As usual in go, we have to take care of possible mistakes. Here, we react to errors with a panic.

w, err := fsnotify.NewWatcher() if err != nil {    panic(err) }  defer w.Close() 
Enter fullscreen mode Exit fullscreen mode

Now we add a directory that we want to monitor to the watcher.

err = w.Add("/tmp") if err != nil {    panic(err) } 
Enter fullscreen mode Exit fullscreen mode

The real magic then happens in its own go routine.

go func() {   for {     select {       case event, ok := <-w.Events:         if !ok {             return         }          if event.Has(fsnotify.Write) {            fmt.Println("modified file:", event.Name)         }        case err, ok := <-watcher.Errors:         if !ok {             return         }         fmt.Println("error:", err)       }     }   }() 
Enter fullscreen mode Exit fullscreen mode

With go func() {}() we start our own go routine that runs “parallel” to the main thread.

Go routines are great, I totally love them.

Since we don't have a default statement, the select blocks the execution until an event happens.

If the operating system now reports a change to the /tmp directory, we are informed of this and can react to it.


That's it.

golinuxprogrammingwebdev
  • 0 0 Answers
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.