Chris Lu
12 years ago
3 changed files with 92 additions and 76 deletions
-
91weed-fs/src/cmd/weed/fix.go
-
36weed-fs/src/pkg/storage/needle_read_write.go
-
41weed-fs/src/pkg/storage/volume.go
@ -1,65 +1,70 @@ |
|||||
package main |
package main |
||||
|
|
||||
import ( |
import ( |
||||
"pkg/storage" |
|
||||
"log" |
|
||||
"os" |
|
||||
"path" |
|
||||
"strconv" |
|
||||
|
"log" |
||||
|
"os" |
||||
|
"path" |
||||
|
"pkg/storage" |
||||
|
"strconv" |
||||
) |
) |
||||
|
|
||||
func init() { |
func init() { |
||||
cmdFix.Run = runFix // break init cycle
|
|
||||
IsDebug = cmdFix.Flag.Bool("debug", false, "enable debug mode") |
|
||||
|
cmdFix.Run = runFix // break init cycle
|
||||
|
IsDebug = cmdFix.Flag.Bool("debug", false, "enable debug mode") |
||||
} |
} |
||||
|
|
||||
var cmdFix = &Command{ |
var cmdFix = &Command{ |
||||
UsageLine: "fix -dir=/tmp -volumeId=234 -debug=1", |
|
||||
Short: "run weed tool fix on index file if corrupted", |
|
||||
Long: `Fix runs the WeedFS fix command to re-create the index .idx file. |
|
||||
|
UsageLine: "fix -dir=/tmp -volumeId=234 -debug=1", |
||||
|
Short: "run weed tool fix on index file if corrupted", |
||||
|
Long: `Fix runs the WeedFS fix command to re-create the index .idx file. |
||||
|
|
||||
`, |
`, |
||||
} |
} |
||||
|
|
||||
var ( |
var ( |
||||
dir = cmdFix.Flag.String("dir", "/tmp", "data directory to store files") |
|
||||
volumeId = cmdFix.Flag.Int("volumeId", -1, "a non-negative volume id. The volume should already exist in the dir. The volume index file should not exist.") |
|
||||
|
dir = cmdFix.Flag.String("dir", "/tmp", "data directory to store files") |
||||
|
volumeId = cmdFix.Flag.Int("volumeId", -1, "a non-negative volume id. The volume should already exist in the dir. The volume index file should not exist.") |
||||
) |
) |
||||
|
|
||||
func runFix(cmd *Command, args []string) bool { |
func runFix(cmd *Command, args []string) bool { |
||||
|
|
||||
if *volumeId == -1 { |
|
||||
return false |
|
||||
} |
|
||||
|
if *volumeId == -1 { |
||||
|
return false |
||||
|
} |
||||
|
|
||||
fileName := strconv.Itoa(*volumeId) |
|
||||
dataFile, e := os.OpenFile(path.Join(*dir, fileName+".dat"), os.O_RDONLY, 0644) |
|
||||
if e != nil { |
|
||||
log.Fatalf("Read Volume [ERROR] %s\n", e) |
|
||||
} |
|
||||
defer dataFile.Close() |
|
||||
indexFile, ie := os.OpenFile(path.Join(*dir, fileName+".idx"), os.O_WRONLY|os.O_CREATE, 0644) |
|
||||
if ie != nil { |
|
||||
log.Fatalf("Create Volume Index [ERROR] %s\n", ie) |
|
||||
} |
|
||||
defer indexFile.Close() |
|
||||
|
fileName := strconv.Itoa(*volumeId) |
||||
|
dataFile, e := os.OpenFile(path.Join(*dir, fileName+".dat"), os.O_RDONLY, 0644) |
||||
|
if e != nil { |
||||
|
log.Fatalf("Read Volume [ERROR] %s\n", e) |
||||
|
} |
||||
|
defer dataFile.Close() |
||||
|
indexFile, ie := os.OpenFile(path.Join(*dir, fileName+".idx"), os.O_WRONLY|os.O_CREATE, 0644) |
||||
|
if ie != nil { |
||||
|
log.Fatalf("Create Volume Index [ERROR] %s\n", ie) |
||||
|
} |
||||
|
defer indexFile.Close() |
||||
|
|
||||
//skip the volume super block
|
|
||||
dataFile.Seek(storage.SuperBlockSize, 0) |
|
||||
|
dataFile.Seek(0, 0) |
||||
|
header := make([]byte, storage.SuperBlockSize) |
||||
|
if _, e := dataFile.Read(header); e != nil { |
||||
|
log.Fatalf("cannot read superblock: %s", e) |
||||
|
} |
||||
|
|
||||
n, rest := storage.ReadNeedle(dataFile) |
|
||||
dataFile.Seek(int64(rest), 1) |
|
||||
nm := storage.NewNeedleMap(indexFile) |
|
||||
offset := uint32(storage.SuperBlockSize) |
|
||||
for n != nil { |
|
||||
debug("key", n.Id, "volume offset", offset, "data_size", n.Size, "rest", rest) |
|
||||
if n.Size > 0 { |
|
||||
count, pe := nm.Put(n.Id, offset/8, n.Size) |
|
||||
debug("saved", count, "with error", pe) |
|
||||
} |
|
||||
offset += rest+16 |
|
||||
n, rest = storage.ReadNeedle(dataFile) |
|
||||
dataFile.Seek(int64(rest), 1) |
|
||||
} |
|
||||
return true |
|
||||
|
ver, _, _ := storage.ParseSuperBlock(header) |
||||
|
|
||||
|
n, rest := storage.ReadNeedle(dataFile, ver) |
||||
|
dataFile.Seek(int64(rest), 1) |
||||
|
nm := storage.NewNeedleMap(indexFile) |
||||
|
offset := uint32(storage.SuperBlockSize) |
||||
|
for n != nil { |
||||
|
debug("key", n.Id, "volume offset", offset, "data_size", n.Size, "rest", rest) |
||||
|
if n.Size > 0 { |
||||
|
count, pe := nm.Put(n.Id, offset/8, n.Size) |
||||
|
debug("saved", count, "with error", pe) |
||||
|
} |
||||
|
offset += rest + 16 |
||||
|
n, rest = storage.ReadNeedle(dataFile, ver) |
||||
|
dataFile.Seek(int64(rest), 1) |
||||
|
} |
||||
|
return true |
||||
} |
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue