Ejemplo n.º 1
1
func NewCommonFileMap(fileName string, fi *os.FileInfo) map[string]interface{} {
	m := newCamliMap(1, "" /* no type yet */)

	lastSlash := strings.LastIndex(fileName, "/")
	baseName := fileName[lastSlash+1:]
	if isValidUtf8(baseName) {
		m["fileName"] = baseName
	} else {
		m["fileNameBytes"] = []uint8(baseName)
	}

	// Common elements (from file-common.txt)
	m["unixPermission"] = fmt.Sprintf("0%o", fi.Permission())
	if fi.Uid != -1 {
		m["unixOwnerId"] = fi.Uid
		if user := getUserFromUid(fi.Uid); user != "" {
			m["unixOwner"] = user
		}
	}
	if fi.Gid != -1 {
		m["unixGroupId"] = fi.Gid
		if group := getGroupFromGid(fi.Gid); group != "" {
			m["unixGroup"] = group
		}
	}
	if mtime := fi.Mtime_ns; mtime != 0 {
		m["unixMtime"] = rfc3339FromNanos(mtime)
	}
	// Include the ctime too, if it differs.
	if ctime := fi.Ctime_ns; ctime != 0 && fi.Mtime_ns != fi.Ctime_ns {
		m["unixCtime"] = rfc3339FromNanos(ctime)
	}

	return m
}
Ejemplo n.º 2
0
func NewCommonFileMap(fileName string, fi *os.FileInfo) map[string]interface{} {
	m := NewCommonFilenameMap(fileName)
	// Common elements (from file-common.txt)
	if !fi.IsSymlink() {
		m["unixPermission"] = fmt.Sprintf("0%o", fi.Permission())
	}
	if fi.Uid != -1 {
		m["unixOwnerId"] = fi.Uid
		if user := getUserFromUid(fi.Uid); user != "" {
			m["unixOwner"] = user
		}
	}
	if fi.Gid != -1 {
		m["unixGroupId"] = fi.Gid
		if group := getGroupFromGid(fi.Gid); group != "" {
			m["unixGroup"] = group
		}
	}
	if mtime := fi.Mtime_ns; mtime != 0 {
		m["unixMtime"] = RFC3339FromNanos(mtime)
	}
	// Include the ctime too, if it differs.
	if ctime := fi.Ctime_ns; ctime != 0 && fi.Mtime_ns != fi.Ctime_ns {
		m["unixCtime"] = RFC3339FromNanos(ctime)
	}

	return m
}
Ejemplo n.º 3
0
func (p *Propolis) SetRequestMetaData(req *http.Request, info *os.FileInfo) {
	// file permissions: grant "public-read" if the file grants world read permission
	if info.Permission()&s_iroth != 0 {
		req.Header.Set("X-Amz-Acl", acl_public)
	} else {
		req.Header.Set("X-Amz-Acl", acl_private)
	}

	// user id: store the numeric and symbolic names
	user, err := user.LookupId(info.Uid)
	if err != nil {
		req.Header.Set("X-Amz-Meta-Uid", fmt.Sprintf("%d", info.Uid))
	} else {
		req.Header.Set("X-Amz-Meta-Uid", fmt.Sprintf("%d (%s)", info.Uid, user.Username))
	}

	// group id: just store the numeric id for now until Go supports looking up group names
	req.Header.Set("X-Amz-Meta-Gid", fmt.Sprintf("%d", info.Gid))

	// store the permissions as an octal number
	req.Header.Set("X-Amz-Meta-Mode", fmt.Sprintf("0%o", info.Mode))

	// store the modified date in a nice format
	sec := info.Mtime_ns / 1e9
	ns := info.Mtime_ns % 1e9
	date := time.SecondsToLocalTime(sec).String()
	if ns == 0 {
		req.Header.Set("X-Amz-Meta-Mtime", fmt.Sprintf("%d (%s)", sec, date))
	} else {
		req.Header.Set("X-Amz-Meta-Mtime", fmt.Sprintf("%d.%09d (%s)", sec, ns, date))
	}

	// set the content-type by looking up the MIME type
	mimetype := default_mime_type
	switch {
	case info.IsDirectory():
		mimetype = directory_mime_type
	case info.IsSymlink():
		mimetype = symlink_mime_type
	default:
		if dot := strings.LastIndex(info.Name, "."); dot >= 0 && dot+1 < len(info.Name) {
			extension := strings.ToLower(info.Name[dot:])
			if kind := mime.TypeByExtension(extension); kind != "" {
				mimetype = kind
			}
		}
	}
	req.Header.Set("Content-Type", mimetype)
}
Ejemplo n.º 4
0
func (self *Syncer) copyDirectory(srcName string, stat *os.FileInfo, dstName string, out chan SyncStats) {
	stats := new(SyncStats)
	defer func() { out <- *stats }()

	err := os.Mkdir(dstName, stat.Permission())
	if err != nil {
		stats.ErrorCount++
		return
	}
	stats.DirsCreated++
	if self.Verbose {
		fmt.Fprintln(self.VerboseWriter, dstName)
	}

	ops := new(outstandingOps)
	go self.SyncDirectories(srcName, dstName, ops.new())
	ops.wait(stats)
}
Ejemplo n.º 5
0
func canWrite(stat *os.FileInfo, euid, egid int) bool {
	perm := stat.Permission()
	return perm&02 != 0 || perm&020 != 0 && egid == stat.Gid || perm&0200 != 0 && euid == stat.Uid
}
Ejemplo n.º 6
0
/*
 Entry point. Used for setting some variables and parsing the command line.
*/
func main() {
	var err os.Error
	var rootPathDir *os.FileInfo

	// parse command line arguments
	flag.Parse()

	if *flagQuieterMode {
		logger.SetVerbosityLevel(logger.ERROR)
	} else if *flagQuietMode {
		logger.SetVerbosityLevel(logger.WARN)
	} else if *flagVerboseMode {
		logger.SetVerbosityLevel(logger.DEBUG)
	}

	if *flagClean {
		clean()
		os.Exit(0)
	}

	// get the compiler/linker executable
	switch os.Getenv("GOARCH") {
	case "amd64":
		compilerBin = "6g"
		linkerBin = "6l"
		objExt = ".6"
	case "386":
		compilerBin = "8g"
		linkerBin = "8l"
		objExt = ".8"
	case "arm":
		compilerBin = "5g"
		linkerBin = "5l"
		objExt = ".5"
	default:
		logger.Error("Please specify a valid GOARCH (amd64/386/arm).\n")
		os.Exit(1)
	}

	// get the complete path to the compiler/linker
	compilerBin, err = exec.LookPath(compilerBin)
	if err != nil {
		logger.Error("Could not find compiler %s: %s\n", compilerBin, err)
		os.Exit(127)
	}
	linkerBin, err = exec.LookPath(linkerBin)
	if err != nil {
		logger.Error("Could not find linker %s: %s\n", linkerBin, err)
		os.Exit(127)
	}
	gopackBin, err = exec.LookPath(gopackBin)
	if err != nil {
		logger.Error("Could not find gopack executable (%s): %s\n", gopackBin, err)
		os.Exit(127)
	}

	// get the root path from where the application was called
	// and its permissions (used for subdirectories)
	if rootPath, err = os.Getwd(); err != nil {
		logger.Error("Could not get the root path: %s\n", err)
		os.Exit(1)
	}
	if rootPathDir, err = os.Stat(rootPath); err != nil {
		logger.Error("Could not read the root path: %s\n", err)
		os.Exit(1)
	}
	rootPathPerm = rootPathDir.Permission()

	// create the package container
	goPackages = godata.NewGoPackageContainer()

	// check if -o with path
	if *flagOutputFileName != "" {
		dir, err := os.Stat(*flagOutputFileName)
		if err != nil {
			// doesn't exist? try to make it if it's a path
			if (*flagOutputFileName)[len(*flagOutputFileName)-1] == '/' {
				err = os.MkdirAll(*flagOutputFileName, rootPathPerm)
				if err == nil {
					outputDirPrefix = *flagOutputFileName
				}
			} else {
				godata.DefaultOutputFileName = *flagOutputFileName
			}
		} else if dir.IsDirectory() {
			if (*flagOutputFileName)[len(*flagOutputFileName)-1] == '/' {
				outputDirPrefix = *flagOutputFileName
			} else {
				outputDirPrefix = *flagOutputFileName + "/"
			}
		} else {
			godata.DefaultOutputFileName = *flagOutputFileName
		}

		// make path to output file
		if outputDirPrefix == "" && strings.Index(*flagOutputFileName, "/") != -1 {
			err = os.MkdirAll((*flagOutputFileName)[0:strings.LastIndex(*flagOutputFileName, "/")], rootPathPerm)
			if err != nil {
				logger.Error("Could not create %s: %s\n",
					(*flagOutputFileName)[0:strings.LastIndex(*flagOutputFileName, "/")],
					err)
			}
		}

	}

	// read all go files in the current path + subdirectories and parse them
	logger.Info("Parsing go file(s)...\n")
	readFiles(rootPath)

	if *flagTesting {
		buildTestExecutable()
	} else if *flagLibrary {
		buildLibrary()
	} else {
		buildExecutable()
	}

	// make sure exit status is != 0 if there were compiler/linker errors
	if compileErrors || linkErrors {
		os.Exit(1)
	}
}
Ejemplo n.º 7
0
func (self *Syncer) copyRegularFile(srcName string, stat *os.FileInfo, dstName string, out chan SyncStats) {
	stats := new(SyncStats)
	defer func() { out <- *stats }()

	outfd, err := os.Open(dstName, os.O_CREATE|os.O_EXCL|os.O_WRONLY,
		stat.Permission())
	if err != nil {
		fmt.Fprintf(self.ErrorWriter,
			"Error opening copy output file %s: %s\n",
			dstName, err)
		stats.ErrorCount++
		return
	}
	defer outfd.Close()

	infd, err := os.Open(srcName, os.O_RDONLY, 0)
	if err != nil {
		fmt.Fprintf(self.ErrorWriter,
			"Error opening copy source file %s: %s\n",
			srcName, err)
		stats.ErrorCount++
		return
	}
	defer infd.Close()

	const BUF_SIZE = 1024 * 256
	buf := make([]byte, BUF_SIZE)
	bytesRemain := stat.Size
	for bytesRemain > 0 {
		n, err := infd.Read(buf)
		switch {
		case n == 0:
			break
		case n < 0:
			stats.ErrorCount++
			fmt.Fprintf(self.ErrorWriter, "Error copying file %s in read: %s",
				srcName, err)
			return
		default:
			outN, err := outfd.Write(buf[0:n])
			if err != nil || outN != n {
				fmt.Fprintf(self.ErrorWriter, "Error copying file %s in write: %s",
					srcName, err)
				return
			}
			bytesRemain -= int64(outN)
		}
	}

	// Close it explicitly before we syscall.Utime() it, even
	// though the precautionary defer'd Close() above will close
	// it again later.  That's harmless.
	err = outfd.Close()
	if err != nil {
		stats.ErrorCount++
		return
	}

	// When the Chtimes patch is merged upstream...
	// err = os.Chtimes(dstName, stat.Atime_ns, stat.Mtime_ns)
	var tv []syscall.Timeval = make([]syscall.Timeval, 2)
	tv[0] = syscall.NsecToTimeval(stat.Atime_ns)
	tv[1] = syscall.NsecToTimeval(stat.Mtime_ns)
	errno := syscall.Utimes(dstName, tv)
	if errno != 0 {
		fmt.Fprintf(self.ErrorWriter, "Error modifying utimes on %s: %v",
			dstName, errno)
		stats.ErrorCount++
		return
	}

	stats.FilesCreated++
	if self.Verbose {
		fmt.Fprintln(self.VerboseWriter, dstName)
	}
}