mirror of
https://github.com/hanzoai/mail.git
synced 2026-09-23 23:53:02 +00:00
Fork of github.com/mjl-/mox at 5656ca6d1b
(upstream v0.0.15 plus 55 commits).
Rename the module path in go.mod and the import paths in 268 Go files. Also
update the five scripts that "go install" the module (genwebsite.sh,
docker-release.sh, apidiff.sh, test-upgrade.sh, Dockerfile.release), which
would otherwise build upstream rather than this fork.
Add NOTICE recording the upstream project, author, license, and fork point,
and stating which license covers which files: MIT for the mox source and our
changes, MPL-2.0 for publicsuffix/public_suffix_list.txt alone, and the
vendored dependencies' own licenses under licenses/. Embed NOTICE so
"mail licenses" prints the lineage from the running binary. LICENSE.MIT,
LICENSE.MPLv2.0 and the licenses/ tree are preserved verbatim; no code is
relicensed.
The mox command name, config keys, data directory layout and website content
are unchanged. LLM.md records the fork point, the rename, what was left alone,
and the steps to stand the server up.
Co-authored-by: Hanzo Dev <[email protected]>
74 lines
2.0 KiB
Go
74 lines
2.0 KiB
Go
package moxio
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"os"
|
|
|
|
"github.com/hanzoai/mail/mlog"
|
|
)
|
|
|
|
// LinkOrCopy attempts to make a hardlink dst. If that fails, it will try to do a
|
|
// regular file copy. If srcReaderOpt is not nil, it will be used for reading. If
|
|
// fileSync is true and the file is copied instead of hardlinked, fsync is called
|
|
// on the file after writing to ensure the file is flushed to disk. Callers should
|
|
// also sync the directory of the destination file, but may want to do that after
|
|
// linking/copying multiple files. If dst was created and an error occurred, it is
|
|
// removed.
|
|
func LinkOrCopy(log mlog.Log, dst, src string, srcReaderOpt io.Reader, fileSync bool) (rerr error) {
|
|
|
|
// Try hardlink first.
|
|
err := os.Link(src, dst)
|
|
if err == nil {
|
|
return nil
|
|
} else if os.IsNotExist(err) {
|
|
// No point in trying with regular copy, we would fail again. Either src doesn't
|
|
// exist or dst directory doesn't exist.
|
|
return err
|
|
}
|
|
|
|
// File system may not support hardlinks, or link could be crossing file systems.
|
|
// Do a regular file copy.
|
|
if srcReaderOpt == nil {
|
|
sf, err := os.Open(src)
|
|
if err != nil {
|
|
return fmt.Errorf("open source file: %w", err)
|
|
}
|
|
defer func() {
|
|
err := sf.Close()
|
|
log.Check(err, "closing copied source file")
|
|
}()
|
|
srcReaderOpt = sf
|
|
}
|
|
|
|
df, err := os.OpenFile(dst, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0660)
|
|
if err != nil {
|
|
return fmt.Errorf("create destination: %w", err)
|
|
}
|
|
defer func() {
|
|
if df != nil {
|
|
err := df.Close()
|
|
log.Check(err, "closing partial destination file")
|
|
}
|
|
if rerr != nil {
|
|
err = os.Remove(dst)
|
|
log.Check(err, "removing partial destination file", slog.String("path", dst))
|
|
}
|
|
}()
|
|
|
|
if _, err := io.Copy(df, srcReaderOpt); err != nil {
|
|
return fmt.Errorf("copy: %w", err)
|
|
}
|
|
if fileSync {
|
|
if err := df.Sync(); err != nil {
|
|
return fmt.Errorf("sync destination: %w", err)
|
|
}
|
|
}
|
|
if err := df.Close(); err != nil {
|
|
return fmt.Errorf("flush and close destination file: %w", err)
|
|
}
|
|
df = nil
|
|
return nil
|
|
}
|