go-hep.org/x/hep@v0.38.1/xrootd/xrdfs/file.go (about)

     1  // Copyright ©2018 The go-hep Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package xrdfs
     6  
     7  import (
     8  	"context"
     9  	"io"
    10  
    11  	"go-hep.org/x/hep/xrootd/internal/xrdenc"
    12  )
    13  
    14  // File implements access to a content and meta information of file over XRootD.
    15  type File interface {
    16  	io.ReaderAt
    17  	io.WriterAt
    18  
    19  	// Compression returns the compression info.
    20  	Compression() *FileCompression
    21  
    22  	// Info returns the cached stat info.
    23  	// Note that it may return nil if info was not yet fetched and info may be not up-to-date.
    24  	Info() *EntryStat
    25  
    26  	// Handle returns the file handle.
    27  	Handle() FileHandle
    28  
    29  	// Close closes the file.
    30  	Close(ctx context.Context) error
    31  
    32  	// CloseVerify closes the file and checks whether the file has the provided size.
    33  	// A zero size suppresses the verification.
    34  	CloseVerify(ctx context.Context, size int64) error
    35  
    36  	// Sync commits all pending writes to an open file.
    37  	Sync(ctx context.Context) error
    38  
    39  	// ReadAtContext reads len(p) bytes into p starting at offset off.
    40  	ReadAtContext(ctx context.Context, p []byte, off int64) (n int, err error)
    41  
    42  	// WriteAtContext writes len(p) bytes from p to the file at offset off.
    43  	WriteAtContext(ctx context.Context, p []byte, off int64) error
    44  
    45  	// Truncate changes the size of the file.
    46  	Truncate(ctx context.Context, size int64) error
    47  
    48  	// Stat fetches the stat info of this file from the XRootD server.
    49  	// Note that Stat re-fetches value returned by the Info, so after the call to Stat
    50  	// calls to Info may return different value than before.
    51  	Stat(ctx context.Context) (EntryStat, error)
    52  
    53  	// StatVirtualFS fetches the virtual stat info of this file from the XRootD server.
    54  	StatVirtualFS(ctx context.Context) (VirtualFSStat, error)
    55  
    56  	// VerifyWriteAt writes len(p) bytes from p to the file at offset off using crc32 verification.
    57  	//
    58  	// TODO: note that verifyw is not supported by the XRootD server.
    59  	// See https://github.com/xrootd/xrootd/issues/738 for the details.
    60  	VerifyWriteAt(ctx context.Context, p []byte, off int64) error
    61  }
    62  
    63  // FileHandle is the file handle, which should be treated as opaque data.
    64  type FileHandle [4]byte
    65  
    66  // FileCompression holds the compression parameters such as the page size and the type of compression.
    67  type FileCompression struct {
    68  	PageSize int32
    69  	Type     [4]byte
    70  }
    71  
    72  // MarshalXrd implements xrdproto.Marshaler
    73  func (o FileCompression) MarshalXrd(wBuffer *xrdenc.WBuffer) error {
    74  	wBuffer.WriteI32(o.PageSize)
    75  	wBuffer.WriteBytes(o.Type[:])
    76  	return nil
    77  }
    78  
    79  // UnmarshalXrd implements xrdproto.Unmarshaler
    80  func (o *FileCompression) UnmarshalXrd(rBuffer *xrdenc.RBuffer) error {
    81  	o.PageSize = rBuffer.ReadI32()
    82  	rBuffer.ReadBytes(o.Type[:])
    83  	return nil
    84  }