room_state.go 1.62 KB
Newer Older
Tulir Asokan's avatar
Tulir Asokan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// mautrixfs - A Matrix client as a FUSE filesystem.
// Copyright (C) 2019 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

package main

import (
	"context"
Tulir Asokan's avatar
Tulir Asokan committed
21
	"fmt"
Tulir Asokan's avatar
Tulir Asokan committed
22 23 24 25 26 27 28 29 30
	"net/http"
	"syscall"

	"github.com/hanwen/go-fuse/v2/fs"
	"github.com/hanwen/go-fuse/v2/fuse"

	"maunium.net/go/mautrix"
)

Tulir Asokan's avatar
Tulir Asokan committed
31
type RoomStateRoot struct {
Tulir Asokan's avatar
Tulir Asokan committed
32 33 34 35 36 37
	fs.Inode

	room *RoomNode
	client *mautrix.Client
}

Tulir Asokan's avatar
Tulir Asokan committed
38
func (state *RoomStateRoot) Getattr(ctx context.Context, fh fs.FileHandle, out *fuse.AttrOut) syscall.Errno {
Tulir Asokan's avatar
Tulir Asokan committed
39 40 41 42
	out.Mode = 0555
	return OK
}

Tulir Asokan's avatar
Tulir Asokan committed
43 44 45 46
func (state *RoomStateRoot) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) {
	fmt.Println("State lookup", name)
	url := state.client.BuildURL("rooms", state.room.ID, "state", name)
	data, err := state.client.MakeRequest(http.MethodGet, url, nil, nil)
Tulir Asokan's avatar
Tulir Asokan committed
47 48 49 50
	if err != nil || data == nil {
		return nil, syscall.ENOENT
	}

Tulir Asokan's avatar
Tulir Asokan committed
51
	return state.NewInode(ctx, &fs.MemRegularFile{
Tulir Asokan's avatar
Tulir Asokan committed
52 53 54 55 56 57
		Data:  data,
		Attr:  fuse.Attr{
			Mode: 0444,
		},
	}, fs.StableAttr{ Mode: syscall.S_IFREG }), OK
}