Commits

Taru Karttunen committed f844660 Draft

Update to Go1

Comments (0)

Files changed (6)

-Copyright (c) 2010 Taru Karttunen <taruti@taruti.net>
+Copyright (c) 2010-2012 Taru Karttunen <taruti@taruti.net>
 
  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation

Makefile

-include $(GOROOT)/src/Make.inc
-
-TARG=bitbucket.org/taruti/io_replacefile.go
-GOFILES=\
-	realpath.go\
-	replacefile_$(GOOS).go\
-
-include $(GOROOT)/src/Make.pkg
 package replacefile
 
 import (
-	"os"
-	"bytes"
+	"path/filepath"
 )
 
-func RealPath(filepath string) (res string, err os.Error) {
-	if len(filepath) == 0 {
-		return "", os.EIO
-	}
-
-	if filepath[0] != '/' {
-		pwd, err := os.Getwd()
-		if err != nil {
-			return
-		}
-		filepath = pwd + "/" + filepath
-	}
-
-	path := []byte(filepath)
-	nlinks := 0
-	start := 1
-	prev := 1
-	for start < len(path) {
-		c := nextComponent(path, start)
-		cur := c[start:]
-		//				fmt.Printf("Loop start %2d @ c '%s' - path %s\n", start, c, path)
-		//				fmt.Printf("path[start:] = '%s'\n", path[start:])
-		//				fmt.Printf("cur          = '%s'\n", cur)
-		switch {
-		case len(cur) == 0:
-			copy(path[start:], path[start+1:])
-			path = path[0 : len(path)-1]
-		case len(cur) == 1 && cur[0] == '.':
-			if start+2 < len(path) {
-				copy(path[start:], path[start+2:])
-			}
-			path = path[0 : len(path)-2]
-		case len(cur) == 2 && cur[0] == '.' && cur[1] == '.':
-			copy(path[prev:], path[start+2:])
-			path = path[0 : len(path)+prev-(start+2)]
-			prev = 1
-			start = 1
-		default:
-			var fi *os.FileInfo
-			fi, err = os.Lstat(string(c))
-			if err != nil {
-				return string(path), nil
-			}
-			if fi.IsSymlink() {
-
-				nlinks++
-				if nlinks > 16 {
-					return "", os.EIO
-				}
-
-				var dst string
-				dst, err = os.Readlink(string(c))
-				//		fmt.Printf("SYMLINK -> %s\n", dst)
-
-				rest := string(path[len(c):])
-				if dst[0] == '/' {
-					// Absolute links
-					path = []byte(dst + "/" + rest)
-				} else {
-					// Relative links
-					path = []byte(string(path[0:start]) + dst + "/" + rest)
-
-				}
-				prev = 1
-				start = 1
-			} else {
-				// Directories
-				prev = start
-				start = len(c) + 1
-			}
-		}
-	}
-	//		fmt.Printf("RP0 -> %s\n", path)
-	for len(path) > 1 && path[len(path)-1] == '/' {
-		path = path[0 : len(path)-1]
-	}
-	//		fmt.Printf("RP1 -> %s\n", path)
-	return string(path), nil
+func RealPath(path string) (string,error) {
+	return filepath.EvalSymlinks(path)
 }
-
-func nextComponent(path []byte, start int) []byte {
-	v := bytes.IndexByte(path[start:], '/')
-	if v < 0 {
-		return path
-	}
-	return path[0 : start+v]
-}

replacefile_freebsd.go

 //   3) chown+chmod it like the configuration file
 //   4) write and fsync
 //   5) rename
-func ReplaceFile(filename string, data []byte, perm uint32) (err os.Error) {
+func ReplaceFile(filename string, data []byte, perm uint32) (err error) {
 	// Get the real filename behind links
 	dest, err := RealPath(filename)
 	if err == nil {

replacefile_linux.go

+// +build linux
 package replacefile
 
 import (
 //   3) chown+chmod it like the configuration file
 //   4) write and fsync
 //   5) rename
-func ReplaceFile(filename string, data []byte, perm uint32) (err os.Error) {
+func ReplaceFile(filename string, data []byte, perm os.FileMode) (err error) {
 	// Get the real filename behind links
 	dest, err := RealPath(filename)
 	if err == nil {
 	// Stat the original file and copy permissions, may fail if the original does not exist
 	fi, err := os.Stat(filename)
 	if err == nil {
-		f.Chmod(uint32(fi.Permission()))
-		f.Chown(int(fi.Uid), int(fi.Gid))
+		st := fi.Sys().(*syscall.Stat_t)
+		f.Chmod(os.FileMode(st.Mode & 0777))
+		f.Chown(int(st.Uid), int(st.Gid))
 	}
 
 	// Write all the data
 	}
 
 	// Fsync
-	errno := os.Errno(syscall.Fsync(f.Fd()))
-	if errno != 0 {
-		return os.Errno(errno)
+	err = syscall.Fsync(int(f.Fd()))
+	er2:= f.Close()
+	if err != nil {
+		return err
 	}
-	f.Close()
+	if er2 != nil {
+		return er2
+	}
 
 	// Rename
 	err = os.Rename(tmpfile, filename)

replacefile_plan9.go

 //   3) chown+chmod it like the configuration file
 //   4) write and fsync
 //   5) rename
-func ReplaceFile(filename string, data []byte, perm uint32) (err os.Error) {
+func ReplaceFile(filename string, data []byte, perm uint32) (err error) {
 	// Open the file
 	f, err := os.Create(filename)
 	if err != nil {