2012-08-07 09:20:26 +08:00
package main
import (
"pkg/storage"
"log"
"os"
"path"
"strconv"
)
func init ( ) {
cmdFix . Run = runFix // break init cycle
2012-08-07 16:29:22 +08:00
IsDebug = cmdFix . Flag . Bool ( "debug" , false , "enable debug mode" )
2012-08-07 09:20:26 +08:00
}
var cmdFix = & Command {
UsageLine : "fix -dir=/tmp -volumeId=234 -debug=1" ,
2012-11-22 02:52:08 +08:00
Short : "run weed tool fix on index file if corrupted" ,
Long : ` Fix runs the WeedFS fix command to re - create the index . idx file .
2012-08-07 09:20:26 +08:00
` ,
}
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." )
)
func runFix ( cmd * Command , args [ ] string ) bool {
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 ( )
//skip the volume super block
dataFile . Seek ( storage . SuperBlockSize , 0 )
2012-11-07 17:51:43 +08:00
n , rest := storage . ReadNeedle ( dataFile )
dataFile . Seek ( int64 ( rest ) , 1 )
2012-08-07 09:20:26 +08:00
nm := storage . NewNeedleMap ( indexFile )
offset := uint32 ( storage . SuperBlockSize )
for n != nil {
2012-11-07 17:51:43 +08:00
debug ( "key" , n . Id , "volume offset" , offset , "data_size" , n . Size , "rest" , rest )
2012-08-07 09:20:26 +08:00
if n . Size > 0 {
2012-08-24 14:06:15 +08:00
count , pe := nm . Put ( n . Id , offset / 8 , n . Size )
2012-09-28 03:17:27 +08:00
debug ( "saved" , count , "with error" , pe )
2012-08-07 09:20:26 +08:00
}
2012-11-07 17:51:43 +08:00
offset += rest + 16
n , rest = storage . ReadNeedle ( dataFile )
dataFile . Seek ( int64 ( rest ) , 1 )
2012-08-07 09:20:26 +08:00
}
return true
}