| 1 | package receiver |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "io/fs" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "syscall" |
| 11 | "time" |
| 12 | |
| 13 | "github.com/gokrazy/rsync" |
| 14 | "github.com/gokrazy/rsync/internal/rsyncchecksum" |
| 15 | "github.com/gokrazy/rsync/internal/rsynccommon" |
| 16 | "github.com/gokrazy/rsync/internal/rsyncopts" |
| 17 | ) |
| 18 | |
| 19 | // rsync/generator.c:generate_files() |
| 20 | func (rt *Transfer) GenerateFiles(fileList []*File) error { |
| 21 | phase := 0 |
| 22 | for idx, f := range fileList { |
| 23 | if err := rt.recvGenerator(idx, f); err != nil { |
| 24 | return err |
| 25 | } |
| 26 | } |
| 27 | phase++ |
| 28 | if rt.Opts.DebugGTE(rsyncopts.DEBUG_GENR, 1) { |
| 29 | rt.Logger.Printf("generateFiles phase=%d", phase) |
| 30 | } |
| 31 | if err := rt.Conn.WriteInt32(-1); err != nil { |
| 32 | return err |
| 33 | } |
| 34 | |
| 35 | // TODO: re-do any files that failed |
| 36 | phase++ |
| 37 | if rt.Opts.DebugGTE(rsyncopts.DEBUG_GENR, 1) { |
| 38 | rt.Logger.Printf("generateFiles phase=%d", phase) |
| 39 | } |
| 40 | if err := rt.Conn.WriteInt32(-1); err != nil { |
| 41 | return err |
| 42 | } |
| 43 | |
| 44 | // NOTE: touchUpDirs is called from [Transfer.Do] |
| 45 | // so that both goroutines (generator and receiver) |
| 46 | // have finished before we set final permissions. |
| 47 | |
| 48 | if rt.Opts.DebugGTE(rsyncopts.DEBUG_GENR, 1) { |
| 49 | rt.Logger.Printf("generateFiles finished") |
| 50 | } |
| 51 | return nil |
| 52 | } |
| 53 | |
| 54 | func (rt *Transfer) touchUpDirs(fileList []*File) error { |
| 55 | for idx, f := range fileList { |
| 56 | if rt.Opts.DebugGTE(rsyncopts.DEBUG_TIME, 2) { |
| 57 | rt.Logger.Printf("touchUpDirs: %s (%d)", f.Name, idx) |
| 58 | } |
| 59 | mode := fs.FileMode(f.Mode) |
| 60 | if mode&rsync.S_IFMT != rsync.S_IFDIR { |
| 61 | continue // not a directory |
| 62 | } |
| 63 | if rt.Opts.DryRun { |
| 64 | continue |
| 65 | } |
| 66 | if mode&syscall.S_IWUSR > 0 { |
| 67 | continue // directory is writeable, no touchup needed |
| 68 | } |
| 69 | if err := rt.setPerms(f, mode); err != nil { |
| 70 | return err |
| 71 | } |
| 72 | } |
| 73 | return nil |
| 74 | } |
| 75 | |
| 76 | // rsync/generator.c:skip_file |
| 77 | func (rt *Transfer) skipFile(f *File, st os.FileInfo) (bool, error) { |
| 78 | if st.Size() != f.Length { |
| 79 | return false, nil |
| 80 | } |
| 81 | |
| 82 | if rt.Opts.AlwaysChecksum { |
| 83 | checksum, err := rsyncchecksum.RootChecksum(rt.DestRoot, f.Name) |
| 84 | if err != nil { |
| 85 | return false, err |
| 86 | } |
| 87 | return bytes.Equal(f.Checksum[:], checksum[:]), nil |
| 88 | } |
| 89 | |
| 90 | // TODO: size only |
| 91 | |
| 92 | if rt.Opts.IgnoreTimes { |
| 93 | return false, nil |
| 94 | } |
| 95 | |
| 96 | return modTimeEqual(st.ModTime(), f.ModTime), nil |
| 97 | } |
| 98 | |
| 99 | func modTimeEqual(a, b time.Time) bool { |
| 100 | a = a.Truncate(time.Second) |
| 101 | b = b.Truncate(time.Second) |
| 102 | return a.Equal(b) |
| 103 | } |
| 104 | |
| 105 | // rsync/rsync.c:set_perms |
| 106 | func (rt *Transfer) setPerms(f *File, mode fs.FileMode) error { |
| 107 | if rt.Opts.DryRun { |
| 108 | return nil |
| 109 | } |
| 110 | |
| 111 | st, err := rt.DestRoot.Lstat(f.Name) |
| 112 | if err != nil { |
| 113 | return err |
| 114 | } |
| 115 | |
| 116 | perm := mode & os.ModePerm |
| 117 | mode = mode & rsync.S_IFMT |
| 118 | if rt.Opts.PreserveTimes && |
| 119 | mode != rsync.S_IFLNK && |
| 120 | !modTimeEqual(st.ModTime(), f.ModTime) { |
| 121 | if err := rt.DestRoot.Chtimes(f.Name, f.ModTime, f.ModTime); err != nil { |
| 122 | return err |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | _, err = rt.setUid(f, st) |
| 127 | if err != nil { |
| 128 | return err |
| 129 | } |
| 130 | |
| 131 | if mode != rsync.S_IFLNK { |
| 132 | if st.Mode().Perm() != perm { // only call Chmod if the permissions actually differ |
| 133 | if err := rt.DestRoot.Chmod(f.Name, perm); err != nil { |
| 134 | return err |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | return nil |
| 140 | } |
| 141 | |
| 142 | // rsync/generator.c:recv_generator |
| 143 | func (rt *Transfer) recvGenerator(idx int, f *File) error { |
| 144 | if rt.listOnly() { |
| 145 | fmt.Fprintf(rt.Env.Stdout, "%s %11.0f %s %s\n", |
| 146 | f.FileMode().String(), |
| 147 | float64(f.Length), // TODO: rsync prints decimal separators |
| 148 | f.ModTime.Format("2006/01/02 15:04:05"), |
| 149 | f.Name) |
| 150 | return nil |
| 151 | } |
| 152 | if rt.Opts.DebugGTE(rsyncopts.DEBUG_GENR, 1) { |
| 153 | rt.Logger.Printf("recv_generator(f=%+v)", f) |
| 154 | } |
| 155 | |
| 156 | local := filepath.Join(rt.Dest, f.Name) |
| 157 | st, err := rt.DestRoot.Lstat(f.Name) |
| 158 | |
| 159 | mode := f.Mode & rsync.S_IFMT |
| 160 | if mode == rsync.S_IFDIR { |
| 161 | if rt.Opts.DryRun { |
| 162 | return nil |
| 163 | } |
| 164 | if err == nil && !st.IsDir() { |
| 165 | // A file (not a directory) with this name exists. Delete it so that |
| 166 | // we can create a directory instead. |
| 167 | if err := rt.DestRoot.Remove(f.Name); err != nil { |
| 168 | return fmt.Errorf("unlinking to make room for directory: %v", err) |
| 169 | } |
| 170 | err = fmt.Errorf("file removed") |
| 171 | } |
| 172 | if err != nil { |
| 173 | perm := fs.FileMode(f.Mode) & os.ModePerm |
| 174 | if rt.Opts.DebugGTE(rsyncopts.DEBUG_GENR, 1) { |
| 175 | rt.Logger.Printf("MkdirAll(%s, %v)", f.Name, perm) |
| 176 | } |
| 177 | if err := rt.DestRoot.MkdirAll(f.Name, perm); err != nil { |
| 178 | // TODO: EEXIST is okay |
| 179 | return err |
| 180 | } |
| 181 | // fallthrough to setPerms and return nil |
| 182 | } |
| 183 | mode := fs.FileMode(f.Mode) |
| 184 | if mode&syscall.S_IWUSR == 0 { |
| 185 | // The directory is lacking write permission, |
| 186 | // so we need to create it writeable as long as |
| 187 | // we are creating files inside that directory. |
| 188 | // GenerateFiles will fix permissions afterwards. |
| 189 | rt.retouchDirPerms = true |
| 190 | mode |= syscall.S_IWUSR |
| 191 | } |
| 192 | if err := rt.setPerms(f, mode); err != nil { |
| 193 | return err |
| 194 | } |
| 195 | return nil |
| 196 | } |
| 197 | |
| 198 | if rt.Opts.PreserveLinks && mode == rsync.S_IFLNK { |
| 199 | // TODO: safe_symlinks option |
| 200 | if err == nil { |
| 201 | // local file exists, verify target matches |
| 202 | if target, err := rt.DestRoot.Readlink(f.Name); err == nil { |
| 203 | if rt.Opts.DebugGTE(rsyncopts.DEBUG_GENR, 1) { |
| 204 | rt.Logger.Printf("existing target: %q", target) |
| 205 | } |
| 206 | if target == f.LinkTarget { |
| 207 | if err := rt.setPerms(f, fs.FileMode(f.Mode)); err != nil { |
| 208 | return err |
| 209 | } |
| 210 | return nil // skip |
| 211 | } |
| 212 | // fallthrough to create or replace the symlink |
| 213 | } |
| 214 | // fallthrough to create or replace the symlink |
| 215 | } |
| 216 | if rt.Opts.DebugGTE(rsyncopts.DEBUG_GENR, 1) { |
| 217 | rt.Logger.Printf("symlink %s -> %s", f.Name, f.LinkTarget) |
| 218 | } |
| 219 | if err := symlink(rt.DestRoot, f.LinkTarget, f.Name); err != nil { |
| 220 | return err |
| 221 | } |
| 222 | if err := rt.setPerms(f, fs.FileMode(f.Mode)); err != nil { |
| 223 | return err |
| 224 | } |
| 225 | return nil |
| 226 | } |
| 227 | |
| 228 | if rt.Opts.PreserveDevices && (mode == rsync.S_IFCHR || |
| 229 | mode == rsync.S_IFBLK || |
| 230 | mode == rsync.S_IFSOCK || |
| 231 | mode == rsync.S_IFIFO) { |
| 232 | if err := rt.createDevice(f, st); err != nil { |
| 233 | return err |
| 234 | } |
| 235 | return nil |
| 236 | } |
| 237 | |
| 238 | if rt.Opts.PreserveHardlinks { |
| 239 | // TODO: hard link check |
| 240 | } |
| 241 | |
| 242 | if !f.FileMode().IsRegular() { |
| 243 | // None of the Preserve* options is enabled, so just skip over |
| 244 | // non-regular files. |
| 245 | return nil |
| 246 | } |
| 247 | |
| 248 | requestFullFile := func() error { |
| 249 | if rt.Opts.DebugGTE(rsyncopts.DEBUG_GENR, 1) { |
| 250 | rt.Logger.Printf("requesting: %s", f.Name) |
| 251 | } |
| 252 | if err := rt.Conn.WriteInt32(int32(idx)); err != nil { |
| 253 | return err |
| 254 | } |
| 255 | if rt.Opts.DryRun { |
| 256 | return nil |
| 257 | } |
| 258 | var sh rsync.SumHead |
| 259 | if err := sh.WriteTo(rt.Conn); err != nil { |
| 260 | return err |
| 261 | } |
| 262 | return nil |
| 263 | } |
| 264 | |
| 265 | if os.IsNotExist(err) { |
| 266 | return requestFullFile() |
| 267 | } |
| 268 | if err != nil { |
| 269 | return err |
| 270 | } |
| 271 | |
| 272 | if !st.Mode().IsRegular() { |
| 273 | // A non-regular file with this name exists. Delete it so that we can |
| 274 | // create our file instead. |
| 275 | if err := rt.DestRoot.Remove(f.Name); err != nil { |
| 276 | return fmt.Errorf("unlinking to make room for regular file: %v", err) |
| 277 | } |
| 278 | return requestFullFile() |
| 279 | } |
| 280 | |
| 281 | // TODO: update-only check |
| 282 | |
| 283 | skip, err := rt.skipFile(f, st) |
| 284 | if err != nil { |
| 285 | return err |
| 286 | } |
| 287 | if skip { |
| 288 | if rt.Opts.InfoGTE(rsyncopts.INFO_SKIP, 1) { |
| 289 | rt.Logger.Printf("skipping %s", local) |
| 290 | } |
| 291 | if err := rt.setPerms(f, fs.FileMode(f.Mode)); err != nil { |
| 292 | return err |
| 293 | } |
| 294 | return nil |
| 295 | } |
| 296 | |
| 297 | if rt.Opts.DryRun { |
| 298 | if err := rt.Conn.WriteInt32(int32(idx)); err != nil { |
| 299 | return err |
| 300 | } |
| 301 | |
| 302 | return nil |
| 303 | } |
| 304 | |
| 305 | // TODO: if deltas are disabled, request the file in full |
| 306 | |
| 307 | in, err := rt.DestRoot.Open(f.Name) |
| 308 | if err != nil { |
| 309 | rt.Logger.Printf("failed to open %s, continuing: %v", local, err) |
| 310 | return requestFullFile() |
| 311 | } |
| 312 | defer in.Close() |
| 313 | |
| 314 | if rt.Opts.DebugGTE(rsyncopts.DEBUG_GENR, 1) { |
| 315 | rt.Logger.Printf("sending sums for: %s", f.Name) |
| 316 | } |
| 317 | if err := rt.Conn.WriteInt32(int32(idx)); err != nil { |
| 318 | return err |
| 319 | } |
| 320 | |
| 321 | return rt.generateAndSendSums(in, st.Size()) |
| 322 | } |
| 323 | |
| 324 | // rsync/generator.c:generate_and_send_sums |
| 325 | func (rt *Transfer) generateAndSendSums(in *os.File, fileLen int64) error { |
| 326 | sh := rsynccommon.SumSizesSqroot(fileLen) |
| 327 | if err := sh.WriteTo(rt.Conn); err != nil { |
| 328 | return err |
| 329 | } |
| 330 | buf := make([]byte, int(sh.BlockLength)) |
| 331 | remaining := fileLen |
| 332 | for i := int32(0); i < sh.ChecksumCount; i++ { |
| 333 | n1 := min(int64(sh.BlockLength), remaining) |
| 334 | b := buf[:n1] |
| 335 | if _, err := io.ReadFull(in, b); err != nil { |
| 336 | return err |
| 337 | } |
| 338 | |
| 339 | sum1 := rsyncchecksum.Checksum1(b) |
| 340 | sum2 := rsyncchecksum.Checksum2(rt.Seed, b) |
| 341 | if err := rt.Conn.WriteInt32(int32(sum1)); err != nil { |
| 342 | return err |
| 343 | } |
| 344 | if _, err := rt.Conn.Writer.Write(sum2); err != nil { |
| 345 | return err |
| 346 | } |
| 347 | remaining -= n1 |
| 348 | } |
| 349 | return nil |
| 350 | } |