Exemple #1
0
// Initializes the fields of a file and add it to a directory.
// Returns nil if successful, or an error.
func (f *File) Add(dir *File, name string, uid p.User, gid p.Group, mode uint32, ops interface{}) error {

	lock.Lock()
	qpath := qnext
	qnext++
	lock.Unlock()

	f.Qid.Type = uint8(mode >> 24)
	f.Qid.Version = 0
	f.Qid.Path = qpath
	f.Mode = mode
	f.Atime = uint32(time.Now().Unix())
	f.Mtime = f.Atime
	f.Length = 0
	f.Name = name
	if uid != nil {
		f.Uid = uid.Name()
		f.Uidnum = uint32(uid.Id())
	} else {
		f.Uid = "none"
		f.Uidnum = p.NOUID
	}

	if gid != nil {
		f.Gid = gid.Name()
		f.Gidnum = uint32(gid.Id())
	} else {
		f.Gid = "none"
		f.Gidnum = p.NOUID
	}

	f.Muid = ""
	f.Muidnum = p.NOUID
	f.Ext = ""

	if dir != nil {
		f.Parent = dir
		dir.Lock()
		for p := dir.cfirst; p != nil; p = p.next {
			if name == p.Name {
				dir.Unlock()
				return Eexist
			}
		}

		if dir.clast != nil {
			dir.clast.next = f
		} else {
			dir.cfirst = f
		}

		f.prev = dir.clast
		f.next = nil
		dir.clast = f
		dir.Unlock()
	} else {
		f.Parent = f
	}

	f.Ops = ops
	return nil
}