2013-10-03 20:10:26 +00:00
/ *
2018-01-04 00:52:49 +00:00
Copyright 2013 The Perkeep Authors
2013-10-03 20:10:26 +00:00
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
package main
import (
"errors"
"flag"
"fmt"
"log"
2013-11-28 19:58:47 +00:00
"os"
2013-10-03 20:10:26 +00:00
2015-12-01 16:19:49 +00:00
"go4.org/jsonconfig"
2018-01-03 05:03:30 +00:00
"perkeep.org/internal/osutil"
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
"perkeep.org/pkg/blobserver/diskpacked"
"perkeep.org/pkg/cmdmain"
"perkeep.org/pkg/serverinit"
2013-10-03 20:10:26 +00:00
)
type reindexdpCmd struct {
2016-04-04 16:12:50 +00:00
overwrite bool
2013-10-03 20:10:26 +00:00
}
func init ( ) {
cmdmain . RegisterCommand ( "reindex-diskpacked" ,
func ( flags * flag . FlagSet ) cmdmain . CommandRunner {
cmd := new ( reindexdpCmd )
flags . BoolVar ( & cmd . overwrite , "overwrite" , false ,
2014-09-20 07:49:15 +00:00
"Whether to overwrite the existing index. If false, only check." )
2013-10-03 20:10:26 +00:00
return cmd
} )
}
func ( c * reindexdpCmd ) Describe ( ) string {
return "Rebuild the index of the diskpacked blob store"
}
func ( c * reindexdpCmd ) Usage ( ) {
fmt . Fprintln ( os . Stderr , "Usage: camtool [globalopts] reindex-diskpacked [reindex-opts]" )
2014-02-12 03:24:08 +00:00
fmt . Fprintln ( os . Stderr , " camtool reindex-diskpacked [--overwrite] # dir from server config" )
fmt . Fprintln ( os . Stderr , " camtool reindex-diskpacked [--overwrite] /path/to/directory" )
2013-10-03 20:10:26 +00:00
}
func ( c * reindexdpCmd ) RunCommand ( args [ ] string ) error {
var path string
2014-09-20 07:49:15 +00:00
var indexConf jsonconfig . Obj
switch len ( args ) {
case 0 :
case 1 :
path = args [ 0 ]
default :
return errors . New ( "More than 1 argument not allowed" )
}
cfg , err := serverinit . LoadFile ( osutil . UserServerConfigPath ( ) )
if err != nil {
return err
}
prefixes , ok := cfg . Obj [ "prefixes" ] . ( map [ string ] interface { } )
if ! ok {
return fmt . Errorf ( "No 'prefixes' object in low-level (or converted) config file %s" , osutil . UserServerConfigPath ( ) )
}
2015-07-18 19:44:26 +00:00
paths , confs := [ ] string { } , [ ] jsonconfig . Obj { }
2014-09-20 07:49:15 +00:00
for prefix , vei := range prefixes {
pmap , ok := vei . ( map [ string ] interface { } )
2013-12-25 04:16:36 +00:00
if ! ok {
2014-09-20 07:49:15 +00:00
log . Printf ( "prefix %q value is a %T, not an object" , prefix , vei )
continue
2013-10-03 20:10:26 +00:00
}
2014-09-20 07:49:15 +00:00
pconf := jsonconfig . Obj ( pmap )
handlerType := pconf . RequiredString ( "handler" )
handlerArgs := pconf . OptionalObject ( "handlerArgs" )
// no pconf.Validate, as this is a recover tool
if handlerType != "storage-diskpacked" {
continue
2013-10-03 20:10:26 +00:00
}
2014-09-20 07:49:15 +00:00
log . Printf ( "handlerArgs of %q: %v" , prefix , handlerArgs )
if handlerArgs == nil {
log . Printf ( "no handlerArgs for %q" , prefix )
continue
2013-12-25 04:16:36 +00:00
}
2014-09-20 07:49:15 +00:00
aconf := jsonconfig . Obj ( handlerArgs )
apath := aconf . RequiredString ( "path" )
// no aconv.Validate, as this is a recover tool
if apath == "" {
log . Printf ( "path is missing for %q" , prefix )
continue
2013-12-25 04:16:36 +00:00
}
2014-09-20 07:49:15 +00:00
if path != "" && path != apath {
continue
}
paths = append ( paths , apath )
2015-07-18 19:44:26 +00:00
confs = append ( confs , aconf )
2014-09-20 07:49:15 +00:00
}
if len ( paths ) == 0 {
2017-12-10 09:13:00 +00:00
return fmt . Errorf ( "server config file %s doesn't specify a disk-packed storage handler" ,
2014-09-20 07:49:15 +00:00
osutil . UserServerConfigPath ( ) )
}
if len ( paths ) > 1 {
return fmt . Errorf ( "Ambiguity. Server config file %s d specify more than 1 disk-packed storage handler. Please specify one of: %v" , osutil . UserServerConfigPath ( ) , paths )
2013-10-03 20:10:26 +00:00
}
2014-09-20 07:49:15 +00:00
path = paths [ 0 ]
2013-10-03 20:10:26 +00:00
if path == "" {
return errors . New ( "no path is given/found" )
}
2015-07-18 19:44:26 +00:00
// If no index is specified, the default will be used (as on the regular path).
if mi := confs [ 0 ] [ "metaIndex" ] ; mi != nil {
if mi , ok := mi . ( map [ string ] interface { } ) ; ok {
indexConf = jsonconfig . Obj ( mi )
}
}
log . Printf ( "indexConf: %v" , indexConf )
2013-10-03 20:10:26 +00:00
2014-09-20 07:49:15 +00:00
return diskpacked . Reindex ( path , c . overwrite , indexConf )
2013-10-03 20:10:26 +00:00
}