如何实现文件系统?

I need write program which will mount(work) with all type of fyle system.

In linux you can mount the different file systems. And each file system has parameters, some number of "folders" and even the sequence of execution of commands. examples:

  1. A simple example: mount -t ntfs / dev / sda1 / mnt / win_xp
  2. Mounting / lxc / shared: mount --bind / lxc / shared / lxc / shared mount --make-unbindable / lxc / shared mount --make-shared / lxc / shared
  3. receives 2-directory instead of one: mount -t overlayfs -o rw, lowerdir =/low-layer, upperdir = /hilayer overlayfs /overlayfs next-command: find / overlayfs -lname '(overlay-whiteout)' -exec rm -rf {} \;

In essence, each file system has two states: - mounted - not mounted

And over each can hold a limited number of operations:

  • mount ()
  • unmount ()
  • check mounted
  • Remove (previously unmounted)

PS: I write code in the Go language. I have wrote some code http://play.golang.org/p/Ri5yAqsLu7 but it doesn't fit.

package main

import (
    "fmt"
    "sort"
    "sync"
    "os/exec"
    "strings"
)
type LayerForMount struct {
    Upper        string
    Lower        string
    MountPoint   string
    MountOptions string
    FSType       string
}

type LayersOverlayFS struct {
    layers map[int]LayerForMount
    sync.RWMutex
}

func (l *LayersOverlayFS) Add(layer LayerForMount, key int) error {
    l.Lock()
    defer l.Unlock()
    if _, ok := l.layers[key]; ok {
        return fmt.Errorf("key %v exist", key)
    }
    l.layers[key] = layer
    return nil
}
func (l *LayersOverlayFS) Remove(key int) {
    l.Lock()
    defer l.Unlock()
    delete(l.layers, key)
}

/*
    mount lower,upper dirs to mountpoint if mountpoint not mounted
    TODO: cheack that lower,upper == to already mounted
*/
func (l *LayersOverlayFS) Mount() (err error) {
    l.RLock()
    defer l.RUnlock()
    forceRemount := false
    var keys []int
    for k, _ := range l.layers {
        keys = append(keys, k)
    }
    sort.Ints(keys)

    for _, k := range keys {
        err = MountLowerUpperLayer(l.layers[k].FSType, l.layers[k].Lower, l.layers[k].Upper, l.layers[k].MountPoint, l.layers[k].MountOptions, forceRemount)
        if err != nil {
            return fmt.Errorf("MountLowerUpperLayer(%v,%v,%v,%v,%v,%v) has error: %v", l.layers[k].FSType, l.layers[k].Lower, l.layers[k].Upper, l.layers[k].MountPoint, l.layers[k].MountOptions, forceRemount, err)
        }
    }
    return nil
}
func (l *LayersOverlayFS) GetCFGofLastLayer() LayerForMount {
    l.RLock()
    defer l.RUnlock()
    var keys []int
    for k, _ := range l.layers {
        keys = append(keys, k)
    }
    sort.Ints(keys)
    lastLayer := keys[len(keys)]
    return l.layers[lastLayer]
}

/* unmount only last layer */
func (l *LayersOverlayFS) Umount() (err error) {
    l.RLock()
    defer l.RUnlock()

    forceRemount := false
    var keys []int
    for k, _ := range l.layers {
        keys = append(keys, k)
    }
    sort.Ints(keys)
    lastLayer := keys[len(keys)]
    if IsMounted(l.layers[lastLayer].MountPoint, l.layers[lastLayer].FSType) == true {
        UnmountLayer(l.layers[lastLayer].FSType, l.layers[lastLayer].MountPoint, forceRemount)
    }
    return nil
}


func IsMounted (MountPoint,FSType string) bool{
return true 
}
func UnmountLayer(FSType,MountPoint string,forceRemount bool) {
}
func MountLowerUpperLayer(fstype, lower, upper, mountpoint, mount_options string, forceremount bool) (err error) {      
        cmdLayerMount:="mount"
        cmd := " -n -t " + " " + fstype + " -o " + mount_options + ",lowerdir=" + lower + "/,upperdir=" + upper + "/ " + fstype + " " + mountpoint + "/"
        cmdObject := exec.Command(cmdLayerMount, strings.Fields(cmd)...)
        _, err = cmdObject.Output()
        if err != nil {         
            return err
        }

    return nil
}


func main() {
    fmt.Println("Hello, playground")
}

I want implement all type of File system, but now this is OverlayFS

There shouldn't be any need to reimplement these things. Try researching GVFS or KIO, and the related components. They are parts of Gnome and KDE, allowing them to manage remote mounts and resources (also local, apparently).

GVFS you can use with gccgo, KIO with Swig.