本文整理汇总了Golang中os.File.Chown方法的典型用法代码示例。如果您正苦于以下问题:Golang File.Chown方法的具体用法?Golang File.Chown怎么用?Golang File.Chown使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os.File
的用法示例。
在下文中一共展示了File.Chown方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Create
// Create implements pathfs.Filesystem.
func (fs *FS) Create(path string, flags uint32, mode uint32, context *fuse.Context) (fuseFile nodefs.File, code fuse.Status) {
if fs.isFiltered(path) {
return nil, fuse.EPERM
}
iflags, writeOnly := fs.mangleOpenFlags(flags)
cPath, err := fs.getBackingPath(path)
if err != nil {
return nil, fuse.ToStatus(err)
}
var fd *os.File
cName := filepath.Base(cPath)
// Handle long file name
if nametransform.IsLongContent(cName) {
var dirfd *os.File
dirfd, err = os.Open(filepath.Dir(cPath))
if err != nil {
return nil, fuse.ToStatus(err)
}
defer dirfd.Close()
// Create ".name"
err = fs.nameTransform.WriteLongName(dirfd, cName, path)
if err != nil {
return nil, fuse.ToStatus(err)
}
// Create content
var fdRaw int
fdRaw, err = syscallcompat.Openat(int(dirfd.Fd()), cName, iflags|os.O_CREATE, mode)
if err != nil {
nametransform.DeleteLongName(dirfd, cName)
return nil, fuse.ToStatus(err)
}
fd = os.NewFile(uintptr(fdRaw), cName)
} else {
// Normal (short) file name
fd, err = os.OpenFile(cPath, iflags|os.O_CREATE, os.FileMode(mode))
if err != nil {
return nil, fuse.ToStatus(err)
}
}
// Set owner
if fs.args.PreserveOwner {
err = fd.Chown(int(context.Owner.Uid), int(context.Owner.Gid))
if err != nil {
tlog.Warn.Printf("Create: fd.Chown failed: %v", err)
}
}
return NewFile(fd, writeOnly, fs)
}
示例2: writeFile
func (u *Unarchiver) writeFile(blockSource chan block, workInProgress *sync.WaitGroup) {
var file *os.File = nil
var bufferedFile *bufio.Writer
for block := range blockSource {
if block.blockType == blockTypeStartOfFile {
u.Logger.Verbose(block.filePath)
if u.DryRun {
continue
}
tmp, err := os.Create(block.filePath)
if err != nil {
u.Logger.Warning("File create error:", err.Error())
file = nil
continue
}
file = tmp
bufferedFile = bufio.NewWriter(file)
if !u.IgnoreOwners {
err = file.Chown(block.uid, block.gid)
if err != nil {
u.Logger.Warning("Unable to chown file to", block.uid, "/", block.gid, ":", err.Error())
}
}
if !u.IgnorePerms {
err = file.Chmod(block.mode)
if err != nil {
u.Logger.Warning("Unable to chmod file to", block.mode, ":", err.Error())
}
}
} else if file == nil {
// do nothing; file couldn't be opened for write
} else if block.blockType == blockTypeEndOfFile {
bufferedFile.Flush()
file.Close()
file = nil
} else {
_, err := bufferedFile.Write(block.buffer[:block.numBytes])
if err != nil {
u.Logger.Warning("File write error:", err.Error())
}
}
}
workInProgress.Done()
}
示例3: chownConfFile
/*
chownConfFile is responsible for changing the owner of a file
Input *os.File f : file handler to a file that has already been opened
Input string id : Service ID (example cd67c62b-e462-5137-2cd8-38732db4abd9)
Input string filename: zenmodeler_logstash_forwarder_conf
Input string owner : update the file's owner to this provided string
Output bool : returns true if: the owner parameter is not present present OR the file has been chowned to the requested owner successfully
*/
func chownConfFile(f *os.File, id string, filename string, owner string) bool {
if len(owner) != 0 {
parts := strings.Split(owner, ":")
if len(parts) != 2 {
glog.Errorf("Unsupported owner specification: %s, only %%d:%%d supported for now: %s, %s", owner, id, filename)
return false
}
uid, err := strconv.Atoi(parts[0])
if err != nil {
glog.Warningf("Malformed UID: %s %s: %s", id, filename, err)
return false
}
gid, err := strconv.Atoi(parts[0])
if err != nil {
glog.Warningf("Malformed GID: %s %s: %s", id, filename, err)
return false
}
err = f.Chown(uid, gid)
if err != nil {
glog.Warningf("Could not chown config file: %s %s: %s", id, filename, err)
}
}
return true
}
示例4: TemplateApply
func (c *containerLXD) TemplateApply(trigger string) error {
fname := path.Join(c.PathGet(""), "metadata.yaml")
if !shared.PathExists(fname) {
return nil
}
content, err := ioutil.ReadFile(fname)
if err != nil {
return err
}
metadata := new(imageMetadata)
err = yaml.Unmarshal(content, &metadata)
if err != nil {
return fmt.Errorf("Could not parse %s: %v", fname, err)
}
for filepath, template := range metadata.Templates {
var w *os.File
found := false
for _, tplTrigger := range template.When {
if tplTrigger == trigger {
found = true
break
}
}
if !found {
continue
}
fullpath := shared.VarPath("containers", c.name, "rootfs", strings.TrimLeft(filepath, "/"))
if shared.PathExists(fullpath) {
w, err = os.Create(fullpath)
if err != nil {
return err
}
} else {
uid := 0
gid := 0
if !c.IsPrivileged() {
uid, gid = c.idmapset.ShiftIntoNs(0, 0)
}
shared.MkdirAllOwner(path.Dir(fullpath), 0755, uid, gid)
w, err = os.Create(fullpath)
if err != nil {
return err
}
if !c.IsPrivileged() {
w.Chown(uid, gid)
}
w.Chmod(0644)
}
tplString, err := ioutil.ReadFile(shared.VarPath("containers", c.name, "templates", template.Template))
if err != nil {
return err
}
tpl, err := pongo2.FromString("{% autoescape off %}" + string(tplString) + "{% endautoescape %}")
if err != nil {
return err
}
containerMeta := make(map[string]string)
containerMeta["name"] = c.name
containerMeta["architecture"], _ = shared.ArchitectureName(c.architecture)
if c.ephemeral {
containerMeta["ephemeral"] = "true"
} else {
containerMeta["ephemeral"] = "false"
}
if c.IsPrivileged() {
containerMeta["privileged"] = "true"
} else {
containerMeta["privileged"] = "false"
}
configGet := func(confKey, confDefault *pongo2.Value) *pongo2.Value {
val, ok := c.config[confKey.String()]
if !ok {
return confDefault
}
return pongo2.AsValue(strings.TrimRight(val, "\r\n"))
}
tpl.ExecuteWriter(pongo2.Context{"trigger": trigger,
"path": filepath,
"container": containerMeta,
"config": c.config,
"devices": c.devices,
//.........这里部分代码省略.........
示例5: templateApply
func templateApply(c *lxdContainer, trigger string) error {
fname := shared.VarPath("lxc", c.name, "metadata.yaml")
if _, err := os.Stat(fname); err != nil {
return nil
}
content, err := ioutil.ReadFile(fname)
if err != nil {
return err
}
metadata := new(imageMetadata)
err = yaml.Unmarshal(content, &metadata)
if err != nil {
return fmt.Errorf("Could not parse %s: %v", fname, err)
}
for path, template := range metadata.Templates {
var w *os.File
found := false
for _, tplTrigger := range template.When {
if tplTrigger == trigger {
found = true
break
}
}
if !found {
continue
}
fpath := shared.VarPath("lxc", c.name, "rootfs", strings.TrimLeft(path, "/"))
if _, err := os.Stat(fpath); err == nil {
w, err = os.Create(fpath)
if err != nil {
return err
}
} else {
w, err = os.Create(fpath)
if err != nil {
return err
}
uid, gid := c.idmapset.ShiftIntoNs(0, 0)
w.Chown(uid, gid)
w.Chmod(0644)
}
tpl, err := pongo2.FromFile(shared.VarPath("lxc", c.name, "templates", template.Template))
if err != nil {
return err
}
container_meta := make(map[string]string)
container_meta["name"] = c.name
container_meta["architecture"], _ = shared.ArchitectureName(c.architecture)
if c.ephemeral {
container_meta["ephemeral"] = "true"
} else {
container_meta["ephemeral"] = "false"
}
if c.isPrivileged() {
container_meta["privileged"] = "true"
} else {
container_meta["privileged"] = "false"
}
tpl.ExecuteWriter(pongo2.Context{"trigger": trigger,
"path": path,
"container": container_meta,
"config": c.config,
"devices": c.devices,
"properties": template.Properties}, w)
}
return nil
}