本文整理匯總了Golang中syscall.Access函數的典型用法代碼示例。如果您正苦於以下問題:Golang Access函數的具體用法?Golang Access怎麽用?Golang Access使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Access函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestAccess
func TestAccess(t *testing.T) {
if os.Geteuid() == 0 {
t.Log("Skipping TestAccess() as root.")
return
}
tc := NewTestCase(t)
defer tc.Cleanup()
contents := []byte{1, 2, 3}
err := ioutil.WriteFile(tc.origFile, []byte(contents), 0700)
if err != nil {
t.Fatalf("WriteFile failed: %v", err)
}
err = os.Chmod(tc.origFile, 0)
if err != nil {
t.Fatalf("Chmod failed: %v", err)
}
// Ugh - copied from unistd.h
const W_OK uint32 = 2
errCode := syscall.Access(tc.mountFile, W_OK)
if errCode != syscall.EACCES {
t.Errorf("Expected EACCES for non-writable, %v %v", errCode, syscall.EACCES)
}
err = os.Chmod(tc.origFile, 0222)
if err != nil {
t.Fatalf("Chmod failed: %v", err)
}
errCode = syscall.Access(tc.mountFile, W_OK)
if errCode != nil {
t.Errorf("Expected no error code for writable. %v", errCode)
}
}
示例2: TestAccess
func TestAccess(t *testing.T) {
if os.Geteuid() == 0 {
t.Log("Skipping TestAccess() as root.")
return
}
me := NewTestCase(t)
defer me.Cleanup()
err := ioutil.WriteFile(me.origFile, []byte(contents), 0700)
CheckSuccess(err)
err = os.Chmod(me.origFile, 0)
CheckSuccess(err)
// Ugh - copied from unistd.h
const W_OK uint32 = 2
errCode := syscall.Access(me.mountFile, W_OK)
if errCode != syscall.EACCES {
t.Errorf("Expected EACCES for non-writable, %v %v", errCode, syscall.EACCES)
}
err = os.Chmod(me.origFile, 0222)
CheckSuccess(err)
errCode = syscall.Access(me.mountFile, W_OK)
if errCode != nil {
t.Errorf("Expected no error code for writable. %v", errCode)
}
}
示例3: InitFilecache
//初始化文件緩存,配置參數如下
//
// filecache_dir 緩存文件目錄
//
// filecache_suffix 緩存文件後綴(.cache)
//
// filecache_level 目錄層級(2)
//
// filecache_expire 過期時間(3600秒)
func InitFilecache() (cache.Cache, error) {
if beego.BConfig.RunMode == "dev" {
beego.Info("initiating file cache")
}
filecache_dir := beego.AppConfig.DefaultString("filecache_dir", "")
filecache_suffix := beego.AppConfig.DefaultString("filecache_suffix", ".cache")
filecache_level := beego.AppConfig.DefaultInt("filecache_level", 2)
filecache_expire := beego.AppConfig.DefaultInt("filecache_expire", 3600)
if filecache_dir == "" {
panic("filecache_dir is not set")
}
info, err := os.Stat(filecache_dir)
if err != nil && !os.IsExist(err) {
if err := os.MkdirAll(filecache_dir, 777); err != nil {
panic(fmt.Sprintf("%s not exist and can not be created\n%v", filecache_dir, err))
}
}
if !info.IsDir() {
panic(fmt.Sprintf("%s is not a directory", filecache_dir))
}
if err := syscall.Access(filecache_dir, syscall.O_RDWR); err != nil {
panic(fmt.Sprintf("%s is not accessable\n%v", filecache_dir, err))
}
return cache.NewCache("file", fmt.Sprintf(`{"CachePath":"%s","FileSuffix":"%s","DirectoryLevel":%d,"EmbedExpiry":%d}`, filecache_suffix, filecache_suffix, filecache_level, filecache_expire))
}
示例4: checkArgs
func checkArgs() {
flag.Parse()
if *showHelp {
flag.Usage()
os.Exit(0)
}
if flag.NArg() == 0 {
showErrorAndQuit("No input files", -1)
}
// check that args end with the efene suffix and that the files exist
args := flag.Args()
for i := 0; i < flag.NArg(); i++ {
file := args[i]
if *outputType == "erl2ast" {
if !strings.HasSuffix(file, ".erl") {
showErrorAndQuit("Invalid input filename. '"+file+"'", -1)
}
} else if !strings.HasSuffix(file, fnSuffix) {
showErrorAndQuit("Invalid input filename '"+file+"'", -1)
}
fmt.Printf("")
if syscall.Access(file, syscall.O_RDONLY) != 0 {
showErrorAndQuit("Can't read file '"+file+"'", -1)
}
}
}
示例5: isFileExist
func isFileExist(path string) bool {
if path == "" {
return false
}
return syscall.Access(path, syscall.F_OK) == nil
}
示例6: access
func access(u U, a []uint64) uint64 {
// TODO: portability
path, _ := u.Mem().ReadStrAt(a[0])
amode := uint32(a[1])
err := syscall.Access(path, amode)
return errno(err)
}
示例7: getProgramPath
func getProgramPath() (string, os.Error) {
program := os.Args[0]
dir, _ := path.Split(program)
if dir == "" {
binPath, pathError := exec.LookPath(program)
if pathError != nil {
if syscall.Access(program, syscall.O_RDONLY) != 0 {
return "", os.NewError("Path to " + program + " couldn't be found")
}
cwd, cwdError := os.Getwd()
if cwdError != nil {
return "", cwdError
}
return cwd, nil
}
binPath, _ = path.Split(binPath)
return binPath, nil
}
dir, _ = abspath(dir)
return dir, nil
}
示例8: Init
// Init initialises the srvlog package. if either consoleLog or fileLog
// is true it will start the logger in another gorutine ready to log
func Init(consolLog, fileLog bool, maxLogDays int, pathToLogDir string) {
logToConsol = consolLog
logToFile = fileLog
logDir = pathToLogDir
maxDays = maxLogDays
if logToFile {
// make sure log directory exists
info, err := os.Stat(logDir)
if err != nil {
if os.IsNotExist(err) {
log.Fatalln("The directory specified to serverlog does not exist.")
}
log.Fatalln(err)
}
if !info.IsDir() {
log.Fatalln("The path specified to serverlog is not a directory.")
}
// make sure have premissions to log directory
err = syscall.Access(logDir, syscall.O_RDWR)
if err != nil {
log.Fatalln("Serverlog needs read and write premissions to the specified directory.")
}
// manage logfile names and number of logfiles at any one time
// only needed if logging to files
go logFileOverseer()
}
go listen()
}
示例9: fileOkay
// Checks whether the file can be considered a match according to given options
// Existing option requires the file to exist
// Symlink option allows the file to be a symlink
func fileOkay(path string, options *Options) (bool, error) {
var fi syscall.Stat_t
err := syscall.Lstat(path, &fi)
if err != nil && err != syscall.ENOENT {
return false, err
}
if options.Existing && (err == syscall.ENOENT) {
return false, nil
} // Drop dead files...
if options.Accessable { // FIXME(utkan): No R_OK(=4) in syscall package!
err = syscall.Access(path, 4)
if err != nil {
return false, nil
}
}
issym := fi.Mode&syscall.S_IFLNK == syscall.S_IFLNK
if options.Symlink == false && issym {
return false, nil
} // ...and symlinks, if necessary.
return true, nil
}
示例10: Access
func (constor *Constor) Access(input *fuse.AccessIn) (code fuse.Status) {
constor.log("%d", input.NodeId)
// FIXME: oops fix this
path, err := constor.getPath(input.NodeId)
if err != nil {
return fuse.ToStatus(err)
}
return fuse.ToStatus(syscall.Access(path, input.Mask))
}
示例11: IsExist
// IsExist check if target is exist in fs or not
func IsExist(path string) bool {
if path == "" {
return false
}
path = PATH.Clean(path)
return syscall.Access(path, syscall.F_OK) == nil
}
示例12: Access
func (fs *FS) Access(path string, mode uint32, context *fuse.Context) (code fuse.Status) {
if fs.isFiltered(path) {
return fuse.EPERM
}
cPath, err := fs.getBackingPath(path)
if err != nil {
return fuse.ToStatus(err)
}
return fuse.ToStatus(syscall.Access(cPath, mode))
}
示例13: IsWritable
// 判斷一個文件或目錄是否有寫入權限
func IsWritable(path string) (bool, error) {
err := syscall.Access(path, syscall.O_RDWR)
if err == nil {
return true, nil
}
// Check if error is "no such file or directory"
if _, ok := err.(*os.PathError); ok {
return false, nil
}
return false, err
}
示例14: Which
// Which find full path to some app
func Which(name string) string {
paths := Get().Path()
for _, path := range paths {
if syscall.Access(path+"/"+name, syscall.F_OK) == nil {
return path + "/" + name
}
}
return ""
}
示例15: testAccess
func (me *testCase) testAccess() {
me.writeOrigFile()
err := os.Chmod(me.origFile, 0)
CheckSuccess(err)
// Ugh - copied from unistd.h
const W_OK uint32 = 2
errCode := syscall.Access(me.mountFile, W_OK)
if errCode != syscall.EACCES {
me.tester.Errorf("Expected EACCES for non-writable, %v %v", errCode, syscall.EACCES)
}
err = os.Chmod(me.origFile, 0222)
CheckSuccess(err)
errCode = syscall.Access(me.mountFile, W_OK)
if errCode != 0 {
me.tester.Errorf("Expected no error code for writable. %v", errCode)
}
me.removeMountFile()
me.removeMountFile()
}