Home Backend Development Golang golang monitor file modification

golang monitor file modification

May 10, 2023 pm 05:34 PM

With the continuous development of computer technology, file operations have become an essential part of our daily work and life. However, for some important files, we need to monitor them regularly to ensure their security and integrity. So, how to implement file monitoring and modification detection in golang?

1. System file monitoring

1.1 FSnotify

Golang provides a very excellent file system monitoring library-FSnotify. By adding a listener in the monitoring directory, developers can be notified when files are created, modified, deleted and other operations occur, and handle them accordingly.

The advantages of FSnotify include: cross-platform support, high-performance event capture, monitoring files without blocking the program, etc. Therefore, it is widely used in file synchronization, log analysis, file backup and other scenarios.

The following is the basic usage of FSnotify:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

package main

 

import (

    "github.com/fsnotify/fsnotify"

    "log"

)

 

func main() {

    // 创建文件系统监控器

    watcher, err := fsnotify.NewWatcher()

    if err != nil {

        log.Fatal(err)

    }

    defer watcher.Close()

 

    // 添加需要监控的目录

    err = watcher.Add("/path/to/monitor")

    if err != nil {

        log.Fatal(err)

    }

 

    // 开始监听文件更改事件

    for {

        select {

        case event := <-watcher.Events:

            log.Println("event:", event)

        case err := <-watcher.Errors:

            log.Println("error:", err)

        }

    }

}

Copy after login

In the above sample code, we created a file system monitor and specified the directory that needs to be monitored. Then, we use a for loop to continuously listen for file change events.

By parsing the event, we can know whether the file is created, modified or deleted. For example, if we need to know the event when a file is created, we can make the following judgment:

1

2

3

if event.Op&fsnotify.Create == fsnotify.Create {

    log.Println("File created:", event.Name)

}

Copy after login

Similarly, we can judge by other operation identifiers (such as Write, Remove, Rename, Chmod, etc.) File modification, deletion, renaming, permission changes and other events.

1.2 Regularly detect file modifications

In addition to using FSnotify, we can also implement file modification detection by regularly detecting files. Although this method is not as good as FSnotify's real-time response performance, it may be more suitable in certain scenarios.

The following is a sample code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

package main

 

import (

    "log"

    "os"

    "time"

)

 

func main() {

    for {

        fileInfo, err := os.Stat("/path/to/file")

        if err != nil {

            log.Fatal(err)

        }

         

        // 检查文件的修改时间是否变化

        if fileInfo.ModTime() != lastModified {

            log.Println("File modified!")

            lastModified = fileInfo.ModTime()

        }

 

        // 等待一段时间后再次检测

        time.Sleep(1 * time.Second)

    }

}

Copy after login

In the above sample code, we detect whether the file has been modified by regularly reading the modification time of the file. Since the frequency of timing detection is relatively low, each time the modification time of a file is read, it must be judged whether it is the same as the last modification time to avoid repeating the same operation.

Although this method is not as good as FSnotify's real-time performance, in some scenarios that do not require high real-time performance, this method may be more concise and easier to understand.

2. File modification detection

Although we have been able to monitor files, there is no guarantee that the monitored files must have been modified. Therefore, we also need to compare the contents of the files to ensure the security and integrity of the files.

2.1 Calculate the MD5 value of the file

MD5 is a message digest algorithm that calculates input data of any length and obtains a 128-bit digest output. It has the following characteristics: irreversibility, uniqueness, non-conflict, etc. Therefore, we can determine whether the content of the file has changed by calculating the MD5 value of the file.

The following is a sample code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

package main

 

import (

    "crypto/md5"

    "encoding/hex"

    "io/ioutil"

    "log"

)

 

func main() {

    fileData, err := ioutil.ReadFile("/path/to/file")

    if err != nil {

        log.Fatal(err)

    }

 

    md5Sum := md5.Sum(fileData)

    md5SumString := hex.EncodeToString(md5Sum[:])

    log.Println("File MD5:", md5SumString)

}

Copy after login

In the above sample code, we read the contents of the file through the ioutil.ReadFile function, and then use the crypto/md5 library to calculate the MD5 value of the file, and Convert it into string form for output. Since the MD5 value is unique, we can compare the calculated MD5 value with the previous MD5 value to determine whether the content of the file has changed.

2.2 Real-time comparison of file content

In addition to calculating the MD5 value of the file, we can also compare the file content in real time to determine whether it has changed. The specific method is to read the content of the file and then compare it with the last read content.

The following is a sample code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

package main

 

import (

    "io/ioutil"

    "log"

)

 

var lastContent []byte

 

func main() {

    for {

        fileData, err := ioutil.ReadFile("/path/to/file")

        if err != nil {

            log.Fatal(err)

        }

         

        // 检查文件的内容是否变化

        if string(fileData) != string(lastContent) {

            log.Println("File modified!")

            lastContent = fileData

        }

    }

}

Copy after login

In the above sample code, we read the content of the file and then convert it into a string for comparison to determine whether the content of the file is changes occur. Since each time the file content is read, it needs to be compared with the last read content, so the real-time performance of this method will be lower, but it can still play a better role in some scenarios.

Summary

This article introduces how to implement file monitoring and modification detection in golang. For file monitoring, we can choose to use FSnotify or scheduled detection. For file modification detection, it can be achieved by calculating the MD5 value of the file or comparing the contents of the file in real time. In actual work, we can choose appropriate methods to implement based on specific needs to ensure the security and integrity of files.

The above is the detailed content of golang monitor file modification. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the vulnerabilities of Debian OpenSSL What are the vulnerabilities of Debian OpenSSL Apr 02, 2025 am 07:30 AM

OpenSSL, as an open source library widely used in secure communications, provides encryption algorithms, keys and certificate management functions. However, there are some known security vulnerabilities in its historical version, some of which are extremely harmful. This article will focus on common vulnerabilities and response measures for OpenSSL in Debian systems. DebianOpenSSL known vulnerabilities: OpenSSL has experienced several serious vulnerabilities, such as: Heart Bleeding Vulnerability (CVE-2014-0160): This vulnerability affects OpenSSL 1.0.1 to 1.0.1f and 1.0.2 to 1.0.2 beta versions. An attacker can use this vulnerability to unauthorized read sensitive information on the server, including encryption keys, etc.

What libraries are used for floating point number operations in Go? What libraries are used for floating point number operations in Go? Apr 02, 2025 pm 02:06 PM

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

What is the problem with Queue thread in Go's crawler Colly? What is the problem with Queue thread in Go's crawler Colly? Apr 02, 2025 pm 02:09 PM

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Transforming from front-end to back-end development, is it more promising to learn Java or Golang? Apr 02, 2025 am 09:12 AM

Backend learning path: The exploration journey from front-end to back-end As a back-end beginner who transforms from front-end development, you already have the foundation of nodejs,...

PostgreSQL monitoring method under Debian PostgreSQL monitoring method under Debian Apr 02, 2025 am 07:27 AM

This article introduces a variety of methods and tools to monitor PostgreSQL databases under the Debian system, helping you to fully grasp database performance monitoring. 1. Use PostgreSQL to build-in monitoring view PostgreSQL itself provides multiple views for monitoring database activities: pg_stat_activity: displays database activities in real time, including connections, queries, transactions and other information. pg_stat_replication: Monitors replication status, especially suitable for stream replication clusters. pg_stat_database: Provides database statistics, such as database size, transaction commit/rollback times and other key indicators. 2. Use log analysis tool pgBadg

In Go, why does printing strings with Println and string() functions have different effects? In Go, why does printing strings with Println and string() functions have different effects? Apr 02, 2025 pm 02:03 PM

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? Apr 02, 2025 pm 04:54 PM

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

How to specify the database associated with the model in Beego ORM? How to specify the database associated with the model in Beego ORM? Apr 02, 2025 pm 03:54 PM

Under the BeegoORM framework, how to specify the database associated with the model? Many Beego projects require multiple databases to be operated simultaneously. When using Beego...

See all articles