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.

50 lines
1.4 KiB

5 years ago
5 years ago
  1. package storage
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/chrislusf/seaweedfs/weed/util/log"
  6. "github.com/chrislusf/seaweedfs/weed/storage/backend"
  7. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  8. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  9. )
  10. func (v *Volume) maybeWriteSuperBlock() error {
  11. datSize, _, e := v.DataBackend.GetStat()
  12. if e != nil {
  13. log.Infof("failed to stat datafile %s: %v", v.DataBackend.Name(), e)
  14. return e
  15. }
  16. if datSize == 0 {
  17. v.SuperBlock.Version = needle.CurrentVersion
  18. _, e = v.DataBackend.WriteAt(v.SuperBlock.Bytes(), 0)
  19. if e != nil && os.IsPermission(e) {
  20. //read-only, but zero length - recreate it!
  21. var dataFile *os.File
  22. if dataFile, e = os.Create(v.DataBackend.Name()); e == nil {
  23. v.DataBackend = backend.NewDiskFile(dataFile)
  24. if _, e = v.DataBackend.WriteAt(v.SuperBlock.Bytes(), 0); e == nil {
  25. v.noWriteLock.Lock()
  26. v.noWriteOrDelete = false
  27. v.noWriteCanDelete = false
  28. v.noWriteLock.Unlock()
  29. }
  30. }
  31. }
  32. }
  33. return e
  34. }
  35. func (v *Volume) readSuperBlock() (err error) {
  36. v.SuperBlock, err = super_block.ReadSuperBlock(v.DataBackend)
  37. if v.volumeInfo != nil && v.volumeInfo.Replication != "" {
  38. if replication, err := super_block.NewReplicaPlacementFromString(v.volumeInfo.Replication); err != nil {
  39. return fmt.Errorf("Error parse volume %d replication %s : %v", v.Id, v.volumeInfo.Replication, err)
  40. } else {
  41. v.SuperBlock.ReplicaPlacement = replication
  42. }
  43. }
  44. return err
  45. }