github.com/cs3org/reva/v2@v2.27.7/pkg/storage/fs/owncloudsql/owncloudsql_unix.go (about)

     1  // Copyright 2018-2021 CERN
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  //go:build !windows
    20  // +build !windows
    21  
    22  package owncloudsql
    23  
    24  import (
    25  	"context"
    26  	"crypto/md5"
    27  	"encoding/binary"
    28  	"fmt"
    29  	"os"
    30  	"strings"
    31  	"syscall"
    32  
    33  	provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
    34  	"github.com/cs3org/reva/v2/pkg/appctx"
    35  )
    36  
    37  // TODO(jfd) get rid of the differences between unix and windows. the inode and dev should never be used for the etag because it interferes with backups
    38  
    39  // calcEtag will create an etag based on the md5 of
    40  // - mtime,
    41  // - inode (if available),
    42  // - device (if available) and
    43  // - size.
    44  // errors are logged, but an etag will still be returned
    45  func calcEtag(ctx context.Context, fi os.FileInfo) string {
    46  	log := appctx.GetLogger(ctx)
    47  	h := md5.New()
    48  	err := binary.Write(h, binary.BigEndian, fi.ModTime().UnixNano())
    49  	if err != nil {
    50  		log.Error().Err(err).Msg("error writing mtime")
    51  	}
    52  	stat, ok := fi.Sys().(*syscall.Stat_t)
    53  	if ok {
    54  		// take device and inode into account
    55  		err = binary.Write(h, binary.BigEndian, stat.Ino)
    56  		if err != nil {
    57  			log.Error().Err(err).Msg("error writing inode")
    58  		}
    59  		err = binary.Write(h, binary.BigEndian, stat.Dev)
    60  		if err != nil {
    61  			log.Error().Err(err).Msg("error writing device")
    62  		}
    63  	}
    64  	err = binary.Write(h, binary.BigEndian, fi.Size())
    65  	if err != nil {
    66  		log.Error().Err(err).Msg("error writing size")
    67  	}
    68  	etag := fmt.Sprintf("%x", h.Sum(nil))
    69  	return strings.Trim(etag, "\"")
    70  }
    71  
    72  func (fs *owncloudsqlfs) GetQuota(ctx context.Context, ref *provider.Reference) (uint64, uint64, uint64, error) {
    73  	// TODO quota of which storage space?
    74  	// we could use the logged in user, but when a user has access to multiple storages this falls short
    75  	// for now return quota of root
    76  	stat := syscall.Statfs_t{}
    77  	err := syscall.Statfs(fs.toInternalPath(ctx, "/"), &stat)
    78  	if err != nil {
    79  		return 0, 0, 0, err
    80  	}
    81  	// Total data blocks in filesystem
    82  	total := stat.Blocks * uint64(stat.Bsize)
    83  	// Free blocks available to unprivileged user
    84  	used := (stat.Blocks - uint64(stat.Bavail)) * uint64(stat.Bsize) //nolint:unconvert
    85  	remaining := total - used
    86  	return total, used, remaining, nil
    87  }