Can not create folder using mfs

go-ipfs
#1

Hi all . I ma trying to create/move/get files and directory using mfs .

So in my app I have a code look like this

func (handler *FilesHandler) Mkdir(ctx context.Context, req *api.FileMkdirRequest) (*api.FileDataResponse, error) {

	var dirName = req.GetDirName()


	fmt.Println(dirName)

	err := mfs.Mkdir(core.Node.IpfsNode.FilesRoot, dirName, mfs.MkdirOpts{Flush: true})

	if err != nil {
		return nil, err
	}

	return &api.FileDataResponse{}, nil
}

func (handler *FilesHandler) Stat(ctx context.Context, req *api.FileStateRequest) (*api.FileStatResponse, error) {

	cnts := strings.Split(req.GetPath(), "/")

	if len(cnts) > 2 {
		return nil, errors.New("Invalid path")
	}

	dirName := cnts[0]

	dirLookup, err := mfs.Lookup(core.Node.IpfsNode.FilesRoot, dirName)

	if err != nil {
		return nil, err
	}

	dirNode, err := dirLookup.GetNode()

	if err != nil {
		return nil, err
	}

	state, err := dirNode.Stat()

	if err != nil {
		return &api.FileStatResponse{}, err
	}

	numLinks := int64(state.NumLinks)
	dataSize := int64(state.DataSize)
	linkSize := int64(state.LinksSize)
	cumulativeSize := int64(state.CumulativeSize)

	if len(cnts) == 1 {
		return &api.FileStatResponse{
			Cid:            dirNode.Cid().String(),
			NumLinks:       numLinks,
			CumulativeSize: cumulativeSize,
			LinksSize:      linkSize,
			DataSize:       dataSize,
			Type:           api.FileStatResponse_Directory,
		}, nil
	}

	fname := cnts[1]

	directory, ok := dirLookup.(*mfs.Directory)

	if !ok {
		return nil, errors.New("Invalid path")
	}

	file, err := directory.Child(fname)

	if err != nil {
		return nil, err
	}

	fileNode, err := file.GetNode()

	if err != nil {
		return nil, err
	}

	fDesc, ok := file.(*mfs.File)

	fDescNode, _ := fDesc.GetNode()

	state, err = fDescNode.Stat()

	if err != nil {
		return &api.FileStatResponse{}, err
	}

	numLinks = int64(state.NumLinks)
	dataSize = int64(state.DataSize)
	linkSize = int64(state.LinksSize)
	cumulativeSize = int64(state.CumulativeSize)

	return &api.FileStatResponse{
		Cid:            fileNode.Cid().KeyString(),
		NumLinks:       numLinks,
		CumulativeSize: cumulativeSize,
		LinksSize:      linkSize,
		DataSize:       dataSize,
		Type:           api.FileStatResponse_File,
	}, nil
}

func (handler *FilesHandler) Move(ctx context.Context, req *api.FileMoveRequest) (*api.FileResponse, error) {

	source := req.GetSource()
	destination := req.GetDestination()

	err := mfs.Mv(core.Node.IpfsNode.FilesRoot, source, destination)

	if err != nil {
		return &api.FileResponse{}, err
	}

	return &api.FileResponse{}, nil
}

func (handler *FilesHandler) Create(ctx context.Context, req *api.FileCreateRequest) (*api.FileResponse, error) {

	fname := req.GetFname()
	source := req.GetSource()

	nd := dag.NodeWithData(ft.FilePBData(nil, 0))

	f, err := mfs.NewFile(fname, nd, core.Node.IpfsNode.FilesRoot, core.Node.IpfsNode.DAG)

	if err != nil {
		return &api.FileResponse{}, err
	}

	wfd, err := f.Open(mfs.OpenReadWrite, true)

	defer wfd.Flush()

	defer wfd.Close()

	if err != nil {
		return &api.FileResponse{}, err
	}

	wfd.Seek(0, 0)

	sourceFile, err := os.Open(source)

	if err != nil {
		return &api.FileResponse{}, err
	}

	n, err := io.Copy(wfd, sourceFile)

	if err != nil {
		return &api.FileResponse{}, err
	}

	fmt.Printf("Written %v bytes\n", n)

	core.Node.IpfsNode.FilesRoot.Flush()

	return &api.FileResponse{}, nil
}

So after calling Mkdir function I am getting an error look like this

panic: interface conversion: mfs.FSNode is *mfs.File, not *mfs.Directory

Please help me to solve this error!

Note: This error is not always displayed, sometimes code working well

#2

What’s an example of what you’re passing into the Mkdir function when you get this error?