]> ruderich.org/simon Gitweb - safcm/safcm.git/blobdiff - cmd/safcm-remote/sync/files.go
safcm: add experimental support to sync from Windows hosts
[safcm/safcm.git] / cmd / safcm-remote / sync / files.go
index 6e001a8186d14675864b4d90794d77694269cee0..e0a2221009deda9824aa38419fec6ffc1decc0f0 100644 (file)
@@ -15,6 +15,8 @@
 // You should have received a copy of the GNU General Public License
 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+// +build !windows
+
 package sync
 
 import (
@@ -113,7 +115,7 @@ reopen:
        oldFh, err := OpenFileNoFollow(file.Path)
        if err != nil {
                err := err.(*fs.PathError)
-               if err.Err == syscall.ELOOP {
+               if err.Err == syscall.ELOOP || err.Err == syscall.EMLINK {
                        // Check if ELOOP was caused not by O_NOFOLLOW but by
                        // too many nested symlinks before the final path
                        // component.
@@ -149,6 +151,15 @@ reopen:
        if !change.Created {
                // Compare permissions
                change.Old.Mode = oldStat.Mode()
+               if change.Old.Mode.Type() == fs.ModeSymlink {
+                       // Some BSD systems permit changing permissions of
+                       // symlinks but ignore them on traversal. To keep it
+                       // simple we don't support that and always use 0777
+                       // for symlink permissions (the value on GNU/Linux).
+                       //
+                       // TODO: Add proper support for symlinks on BSD
+                       change.Old.Mode |= 0777
+               }
                if change.Old.Mode != file.Mode {
                        if change.Old.Mode.Type() != file.Mode.Type() {
                                changeType = true
@@ -258,7 +269,7 @@ reopen:
                }
        }
 
-       // Directory: create new directory (also type change to directory)
+       // Directory: create new directory, also type change to directory
        if file.Mode.IsDir() && (change.Created || changeType) {
                debugf("creating directory")
                err := os.Mkdir(file.Path, 0700)
@@ -364,7 +375,7 @@ reopen:
                        os.Remove(tmpPath)
                        return err
                }
-               // Permissions are irrelevant for symlinks
+               // Permissions are irrelevant for symlinks (on most systems)
 
        default:
                panic(fmt.Sprintf("invalid file type %s", file.Mode))
@@ -376,7 +387,7 @@ reopen:
                os.Remove(tmpPath)
                return err
        }
-       err = syncPath(dir)
+       err = SyncPath(dir)
        if err != nil {
                return err
        }
@@ -443,13 +454,16 @@ func diffData(oldData []byte, newData []byte) (string, error) {
        oldBin := !strings.HasPrefix(http.DetectContentType(oldData), "text/")
        newBin := !strings.HasPrefix(http.DetectContentType(newData), "text/")
        if oldBin && newBin {
-               return "Binary files differ, cannot show diff", nil
+               return fmt.Sprintf("Binary files differ (%d -> %d bytes), "+
+                       "cannot show diff", len(oldData), len(newData)), nil
        }
        if oldBin {
-               oldData = []byte("<binary content>\n")
+               oldData = []byte(fmt.Sprintf("<binary content, %d bytes>\n",
+                       len(oldData)))
        }
        if newBin {
-               newData = []byte("<binary content>\n")
+               newData = []byte(fmt.Sprintf("<binary content, %d bytes>\n",
+                       len(newData)))
        }
 
        // TODO: difflib shows empty context lines at the end of the file
@@ -517,7 +531,7 @@ func WriteTemp(dir, base string, data []byte, uid, gid int, mode fs.FileMode) (
        return tmpPath, nil
 }
 
-// syncPath syncs path, which should be a directory. To guarantee durability
+// SyncPath syncs path, which should be a directory. To guarantee durability
 // it must be called on a parent directory after adding, renaming or removing
 // files therein.
 //
@@ -525,7 +539,7 @@ func WriteTemp(dir, base string, data []byte, uid, gid int, mode fs.FileMode) (
 // fsync: "Calling fsync() does not necessarily ensure that the entry in the
 // directory containing the file has also reached disk. For that an explicit
 // fsync() on a file descriptor for the directory is also needed."
-func syncPath(path string) error {
+func SyncPath(path string) error {
        x, err := os.Open(path)
        if err != nil {
                return err