The go language implements simple directory replication features _golang

Source: Internet
Author: User

This article describes the go language implementation of simple directory replication. Share to everyone for your reference. The implementation methods are as follows:

Create a separate goroutine traversal file, and the main process is responsible for writing the data. The program copies an empty directory, or you can set up a file that copies only the end of ". XX".
Strictly speaking, this is not a copy file, but a new file. Because this program is to create a new file, and then write the copied data. Our General copy command does not modify the CTime (change time) state of the file.

The code is as follows:

Copy Code code as follows:
A simple Directory replicator: A separate goroutine traversal directory where the main process is responsible for writing data to the new directory.
2014-11-02 BING.L
Package Main

Import (
"IO"
"Log"
"OS"
"Path/filepath"
"Strings"
)

Type FileInfo struct {
RelPath string
Size Int64
isdir BOOL
Handle *os. File
}

Copying file data
Func iocopy (Srchandle *os. File, dstpth String) (err error) {
Dsthandle, err: = OS. OpenFile (dstpth, OS. O_create|os. O_wronly, OS. Modeperm)
If Err!= nil {
return err
}

Defer Srchandle.close ()
Defer Dsthandle.close ()

_, err = Io. Copy (Dsthandle, Srchandle)
return err
}

Traversing the directory, passing the file information to the channel
Func walkfiles (srcdir, suffix string, c chan<-*fileinfo) {
suffix = strings. ToUpper (suffix)

FilePath. Walk (Srcdir, func (f string, fi os.) FileInfo, err Error) error {//traverse directory
If Err!= nil {
Log. Println ("[E]", err)
}

FileInfo: = &fileinfo{}
If strings. Hassuffix (Strings. ToUpper (FI. Name ()), suffix) {//matching file
If FH, err: = OS. OpenFile (f, OS. O_rdonly, OS. Modeperm); Err!= Nil {
Log. Println ("[E]", err)
} else {
Fileinfo.handle = fh
Fileinfo.relpath, _ = filepath. Rel (Srcdir, f)//relative path
Fileinfo.size = fi. Size ()
Fileinfo.isdir = fi. Isdir ()
}

C <-FileInfo
}
})
Close (c)//traversal complete, closing channel
}

Write target file
Func writefiles (Dstdir string, C <-chan *fileinfo) {
If err: = OS. Chdir (Dstdir); Err!= Nil {//Toggle work Path
Log. Fatalln ("[F]", err)
}

For f: = range C {
If fi, err: = OS. Stat (F.relpath); Os. Isnotexist (ERR) {//target does not exist
If F.isdir {
If err: = OS. Mkdirall (F.relpath, OS. MODEDIR); Err!= Nil {
Log. Println ("[E]", err)
}
} else {
If err: = Iocopy (F.handle, F.relpath); Err!= Nil {
Log. Println ("[E]", err)
} else {
Log. Println ("[I] CP:", F.relpath)
}
}
else if!f.isdir {//target exists, and the source is not a directory

If Fi. Isdir ()!= f.isdir {//Check file name is in conflict with directory name occupation
Log. Println ("[E]", "FileName conflict:", F.relpath)

else if fi. Size ()!= f.size {//source and destination are not identical
If err: = Iocopy (F.handle, F.relpath); Err!= Nil {
Log. Println ("[E]", err)
} else {
Log. Println ("[I] CP:", F.relpath)
}
}
}
}
}

Func Main () {
Files_ch: = Make (chan *fileinfo, 100)

Go walkfiles ("E:\\study", ". Doc", files_ch)//Traverse file in a separate goroutine
Writefiles ("E:\\study.bak", Files_ch)
}

I hope this article will help you with your go language program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.