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.
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.