func main() { if len(os.Args) < 2 { fmt.Println("参数不能少于2个!") os.Exit(1) } fmt.Println(os.Args[1]) unix.Access(os.Args[1], unix.W_OK) if false { fmt.Println("access error for ", os.Args[1]) } else { fmt.Println("read access OK ") } }
// updateSudoCommand returns the right_st command name with any sudo prefix if necessary. The command name is returned // even if an error occurred. func updateSudoCommand() (string, error) { // get the full path to the right_st executable so its access can be checked exe, err := osext.Executable() if err != nil { return app.Name, err } // check if the right_st executable can be written by the current user err = unix.Access(exe, unix.W_OK) if err == syscall.EACCES { // the right_st executable cannot be written by the current user so prefix the command name with sudo return "sudo " + app.Name, nil } return app.Name, err }
func isPathWriteable(p gopath.GoPath) bool { return unix.Access(p.Path(), unix.W_OK) == nil }
// Access returns a boolean indicating whether the mode being checked // for is valid. func Access(path string, mode int) error { return unix.Access(path, uint32(mode)) }
func main() { conf = viper.New() conf.SetEnvPrefix("FIFO2KINESIS") conf.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) conf.AutomaticEnv() viper.SetConfigName("fifo2kinesis") pflag.IntP("buffer-queue-limit", "l", 500, "The maximum number of items in the buffer before it is flushed") conf.BindPFlag("buffer-queue-limit", pflag.Lookup("buffer-queue-limit")) conf.SetDefault("buffer-queue-limit", 500) pflag.BoolP("debug", "d", false, "Show debug level log messages") conf.BindPFlag("debug", pflag.Lookup("debug")) conf.SetDefault("debug", "") pflag.StringP("failed-attempts-dir", "D", "", "The path to the directory containing failed attempts") conf.BindPFlag("failed-attempts-dir", pflag.Lookup("failed-attempts-dir")) conf.SetDefault("failed-attempts-dir", "") pflag.StringP("fifo-name", "f", "", "The absolute path of the named pipe, e.g. /var/test.pipe") conf.BindPFlag("fifo-name", pflag.Lookup("fifo-name")) conf.SetDefault("fifo-name", "") pflag.StringP("flush-handler", "h", "kinesis", "Defaults to \"kinesis\", use \"logger\" for debugging") conf.BindPFlag("flush-handler", pflag.Lookup("flush-handler")) conf.SetDefault("flush-handler", "kinesis") pflag.IntP("flush-interval", "i", 5, "The number of seconds before the buffer is flushed and written to Kinesis") conf.BindPFlag("flush-interval", pflag.Lookup("flush-interval")) conf.SetDefault("flush-interval", 5) pflag.StringP("partition-key", "p", "", "The partition key, defaults to a 12 character random string if omitted") conf.BindPFlag("partition-key", pflag.Lookup("partition-key")) conf.SetDefault("partition-key", "") pflag.StringP("region", "R", "", "The AWS region that the Kinesis stream is provisioned in") conf.BindPFlag("region", pflag.Lookup("region")) conf.SetDefault("region", "") pflag.StringP("role-arn", "r", "", "The ARN of the AWS role being assumed.") conf.BindPFlag("role-arn", pflag.Lookup("role-arn")) conf.SetDefault("role-arn", "") pflag.StringP("role-session-name", "S", "", "The session name used when assuming a role.") conf.BindPFlag("role-session-name", pflag.Lookup("role-session-name")) conf.SetDefault("role-session-name", "") pflag.StringP("stream-name", "s", "", "The name of the Kinesis stream") conf.BindPFlag("stream-name", pflag.Lookup("stream-name")) conf.SetDefault("stream-name", "") pflag.Parse() if conf.GetBool("debug") { logger = NewLogger(LOG_DEBUG) } else { logger = NewLogger(LOG_INFO) } logger.Debug("configuration parsed") h := conf.GetString("flush-handler") if h != "kinesis" && h != "logger" { logger.Fatalf("flush handler not valid: %s", h) } fn := conf.GetString("fifo-name") if fn == "" { logger.Fatal("missing required option: fifo-name") } sn := conf.GetString("stream-name") if h == "kinesis" && sn == "" { logger.Fatal("missing required option: stream-name") } ql := conf.GetInt("buffer-queue-limit") if ql < 1 { logger.Fatal("buffer queue limit must be greater than 0") } else if h == "kinesis" && ql > 500 { logger.Fatal("buffer queue cannot exceed 500 items when using the kinesis handler") } fifo := &Fifo{fn} bw := &MemoryBufferWriter{ Fifo: fifo, FlushInterval: conf.GetInt("flush-interval"), QueueLimit: ql, } var bf BufferFlusher if h == "kinesis" { pk := conf.GetString("partition-key") bf = NewKinesisBufferFlusher(sn, pk) } else { bf = &LoggerBufferFlusher{} } var fh FailedAttemptHandler dir := conf.GetString("failed-attempts-dir") if dir == "" { fh = &NullFailedAttemptHandler{} } else { stat, err := os.Stat(dir) if os.IsNotExist(err) { logger.Fatal("failed attempts directory does not exist") } else if !stat.IsDir() { logger.Fatal("failed attempts directory is not a directory") } else if unix.Access(dir, unix.R_OK) != nil { logger.Fatal("failed attempts directory is not readable") } else { fh = &FileFailedAttemptHandler{dir, fifo} } } shutdown := EventListener() RunPipeline(fifo, &Buffer{bw, bf, fh}, shutdown) }