警告:此答案主要是关于向文件添加第二个链接,而不是复制内容。
一个 强大的 和 高效率 副本的概念很简单,但并不容易实现,因为需要处理一些边缘情况和系统限制由目标操作系统强加的,它的配置。
如果您只想复制现有文件,则可以使用
os.link(srcName,dstName)。这样可以避免在应用程序中移动字节并节省磁盘空间。对于大文件,这可以节省大量的时间和空间。
但是,各种操作系统对硬链接的工作方式都有不同的限制。根据您的应用程序和目标系统配置的不同,
link()调用可能无法在所有情况下都起作用。
如果您想要一个通用,健壮和高效的复制功能,请更新
Copy()至:
- 执行检查以确保至少某种形式的复制将成功(访问权限,目录存在等)
- 检查两个文件是否已经存在并且使用相同
os.SameFile
,如果相同则返回成功 - 尝试链接,成功则返回
- 复制字节(所有有效方法均失败),返回结果
一种优化方法是在go例程中复制字节,以便调用者不会在字节复制上进行阻塞。这样做会给调用者带来额外的复杂性,以使其正确处理成功/错误情况。
如果两者都需要,我将有两个不同的复制功能:
CopyFile(src, dst string)(error)用于阻塞复制,
CopyFileAsync(src, dst string) (chan c,error)对于异步情况,它将信令通道传递回调用方。
package mainimport ( "fmt" "io" "os")// CopyFile copies a file from src to dst. If src and dst files exist, and are// the same, then return success. Otherise, attempt to create a hard link// between the two files. If that fail, copy the file contents from src to dst.func CopyFile(src, dst string) (err error) { sfi, err := os.Stat(src) if err != nil { return } if !sfi.Mode().IsRegular() { // cannot copy non-regular files (e.g., directories, // symlinks, devices, etc.) return fmt.Errorf("CopyFile: non-regular source file %s (%q)", sfi.Name(), sfi.Mode().String()) } dfi, err := os.Stat(dst) if err != nil { if !os.IsNotExist(err) { return } } else { if !(dfi.Mode().IsRegular()) { return fmt.Errorf("CopyFile: non-regular destination file %s (%q)", dfi.Name(), dfi.Mode().String()) } if os.SameFile(sfi, dfi) { return } } if err = os.link(src, dst); err == nil { return } err = copyFileContents(src, dst) return}// copyFileContents copies the contents of the file named src to the file named// by dst. The file will be created if it does not already exist. If the// destination file exists, all it's contents will be replaced by the contents// of the source file.func copyFileContents(src, dst string) (err error) { in, err := os.Open(src) if err != nil { return } defer in.Close() out, err := os.Create(dst) if err != nil { return } defer func() { cerr := out.Close() if err == nil { err = cerr } }() if _, err = io.Copy(out, in); err != nil { return } err = out.Sync() return}func main() { fmt.Printf("Copying %s to %sn", os.Args[1], os.Args[2]) err := CopyFile(os.Args[1], os.Args[2]) if err != nil { fmt.Printf("CopyFile failed %qn", err) } else { fmt.Printf("CopyFile succeededn") }}


