1// Copyright 2016 Keybase Inc. All rights reserved.
2// Use of this source code is governed by a BSD
3// license that can be found in the LICENSE file.
4//
5// +build !windows
6
7package libfuse
8
9import (
10	"bazil.org/fuse"
11	"bazil.org/fuse/fs"
12	"github.com/keybase/client/go/kbfs/libkbfs"
13	"golang.org/x/net/context"
14)
15
16// ResetCachesFile represents a write-only file where any write of at
17// least one byte triggers the resetting of all data caches.  It can
18// be reached from any directory under the FUSE mountpoint.  Note that
19// it does not clear the *node* cache, which means that the
20// BlockPointers for existing nodes are still cached, such that
21// directory listings can still be implicitly cached for nodes still
22// being held by the kernel.
23type ResetCachesFile struct {
24	fs *FS
25}
26
27var _ fs.Node = (*ResetCachesFile)(nil)
28
29// Attr implements the fs.Node interface for ResetCachesFile.
30func (f *ResetCachesFile) Attr(ctx context.Context, a *fuse.Attr) error {
31	a.Size = 0
32	a.Mode = 0222
33	return nil
34}
35
36var _ fs.Handle = (*ResetCachesFile)(nil)
37
38var _ fs.HandleWriter = (*ResetCachesFile)(nil)
39
40// Write implements the fs.HandleWriter interface for ResetCachesFile.
41func (f *ResetCachesFile) Write(ctx context.Context, req *fuse.WriteRequest,
42	resp *fuse.WriteResponse) (err error) {
43	f.fs.log.CDebugf(ctx, "ResetCachesFile Write")
44	defer func() { err = f.fs.processError(ctx, libkbfs.WriteMode, err) }()
45	if len(req.Data) == 0 {
46		return nil
47	}
48	f.fs.config.ResetCaches()
49	resp.Size = len(req.Data)
50	return nil
51}
52