+ if *version {
+ fmt.Println("sgodup version", Version, "built with", runtime.Version())
+ return
+ }
+ if *warranty {
+ fmt.Println(`This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, version 3 of the License.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.`)
+ return
+ }
+
+ var stdoutW *bufio.Writer
+ switch *actionS {
+ case "print":
+ action = ActPrint
+ case "ns":
+ action = ActNS
+ stdoutW = bufio.NewWriterSize(os.Stdout, BufSize)
+ nsW = netstring.NewWriter(stdoutW)
+ case "symlink":
+ action = ActSymlink
+ case "hardlink":
+ action = ActHardlink
+ default:
+ log.Fatalln("invalid action")
+ }
+
+ if *nsPath != "" {
+ if action == ActNS {
+ log.Fatalln("\"-action ns\" has no meaning with -ns")
+ }
+ fd, err := os.Open(*nsPath)
+ if err != nil {
+ log.Fatal(err)
+ }
+ nsR := netstring.NewReader(bufio.NewReaderSize(fd, BufSize))
+ pathDup := make([]byte, 1<<10)
+ pathOrig := make([]byte, 1<<10)
+ var pathDupLen, pathOrigLen uint64
+ files := 0
+ fullSize := int64(0)
+ progress := NewProgress(0, 0, &files, &fullSize, " linked", "")
+ termRequired := signalHandler(progress.Stop, &files)
+ for {
+ pathDupLen, err = nsR.Next()
+ if err != nil {
+ if err == io.EOF {
+ break
+ }
+ log.Fatal(err)
+ }
+ if _, err = io.ReadFull(nsR, pathDup[:pathDupLen]); err != nil {
+ log.Fatal(err)
+ }
+ pathOrigLen, err = nsR.Next()
+ if err != nil {
+ log.Fatal(err)
+ }
+ if _, err = io.ReadFull(nsR, pathOrig[:pathOrigLen]); err != nil {
+ log.Fatal(err)
+ }
+ link(string(pathDup[:pathDupLen]), string(pathOrig[:pathOrigLen]))
+ files++
+ }
+ termRequired <- syscall.SIGTERM
+ <-termRequired
+ }
+