You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.1 KiB

5 years ago
5 years ago
  1. package storage
  2. import (
  3. "os"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "github.com/chrislusf/seaweedfs/weed/storage/backend"
  6. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  7. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  8. )
  9. func (v *Volume) maybeWriteSuperBlock() error {
  10. datSize, _, e := v.DataBackend.GetStat()
  11. if e != nil {
  12. glog.V(0).Infof("failed to stat datafile %s: %v", v.DataBackend.Name(), e)
  13. return e
  14. }
  15. if datSize == 0 {
  16. v.SuperBlock.Version = needle.CurrentVersion
  17. _, e = v.DataBackend.WriteAt(v.SuperBlock.Bytes(), 0)
  18. if e != nil && os.IsPermission(e) {
  19. //read-only, but zero length - recreate it!
  20. var dataFile *os.File
  21. if dataFile, e = os.Create(v.DataBackend.Name()); e == nil {
  22. v.DataBackend = backend.NewDiskFile(dataFile)
  23. if _, e = v.DataBackend.WriteAt(v.SuperBlock.Bytes(), 0); e == nil {
  24. v.noWriteOrDelete = false
  25. v.noWriteCanDelete = false
  26. }
  27. }
  28. }
  29. }
  30. return e
  31. }
  32. func (v *Volume) readSuperBlock() (err error) {
  33. v.SuperBlock, err = super_block.ReadSuperBlock(v.DataBackend)
  34. return err
  35. }