{"record":{"id":"ad2efa66c63b8671","repo":"lionsoul2014/ip2region","slug":"seek-to-get-xdb-file-length-w","errorCode":null,"errorMessage":"seek to get xdb file length: %w","messagePattern":"seek to get xdb file length: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"binding/golang/xdb/util.go","lineNumber":274,"sourceCode":"\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"open xdb file `%s`: %w\", dbFile, err)\n\t}\n\tdefer handle.Close()\n\n\tvIndex, err := LoadVectorIndex(handle)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\treturn vIndex, nil\n}\n\n// LoadContent load the whole xdb content from the specified file handle\nfunc LoadContent(handle io.ReadSeeker) ([]byte, error) {\n\t// seek to the head of the file\n\t_, err := handle.Seek(0, 0)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"seek to get xdb file length: %w\", err)\n\t}\n\n\treturn io.ReadAll(handle)\n}\n\n// LoadContentFromFile load the whole xdb content from the specified db file path\nfunc LoadContentFromFile(dbFile string) ([]byte, error) {\n\thandle, err := os.OpenFile(dbFile, os.O_RDONLY, 0600)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"open xdb file `%s`: %w\", dbFile, err)\n\t}\n\tdefer handle.Close()\n\n\tcBuff, err := LoadContent(handle)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n","sourceCodeStart":256,"sourceCodeEnd":292,"githubUrl":"https://github.com/lionsoul2014/ip2region/blob/c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1/binding/golang/xdb/util.go#L256-L292","documentation":"LoadContent seeks to offset 0 (to measure and stream the whole file) before io.ReadAll. If Seek(0,0) fails, the error is wrapped as 'seek to get xdb file length: %w'. It indicates the content could not be loaded because the handle is not seekable or the seek failed.","triggerScenarios":"Calling LoadContent (or LoadContentFromFile-backed searchers) with a non-seekable io.ReadSeeker — pipes, network streams, stdin — or a broken/closed descriptor.","commonSituations":"Piping an xdb from curl into the program and passing the pipe reader directly; reading from a gzip stream without buffering; wrapping the file in a bufio.Reader (which is not a Seeker).","solutions":["Pass an io.ReadSeeker (*os.File, bytes.Reader); if the source is a stream, buffer it fully with io.ReadAll first and wrap in bytes.NewReader.","Use LoadContentFromFS for embedded files instead of ad-hoc stream handling.","Unwrap the error to find the underlying seek failure."],"exampleFix":"// before\ncontent, err := xdb.LoadContent(cmd.StdoutPipe()) // not seekable\n// after\nraw, _ := io.ReadAll(cmd.StdoutPipe())\ncontent, err := xdb.LoadContent(bytes.NewReader(raw))","handlingStrategy":"type-guard","validationCode":"func isSeekable(r io.Reader) bool {\n    _, ok := r.(io.Seeker)\n    return ok\n}","typeGuard":"func asReadSeeker(r io.Reader) (io.ReadSeeker, bool) {\n    s, ok := r.(io.ReadSeeker)\n    return s, ok\n}","tryCatchPattern":"cBuff, err := xdb.LoadContent(handle)\nif err != nil {\n    if strings.Contains(err.Error(), \"seek to get xdb file length\") {\n        return fmt.Errorf(\"source is not seekable: %w\", err)\n    }\n    return err\n}","preventionTips":["Wrap stream sources in bytes.NewReader after full buffering","Do not pass bufio.Reader, pipes, or HTTP bodies directly","Prefer the FromFile/FromFS loaders for disk and embedded sources"],"tags":["golang","io","seek","xdb"],"backgroundTag":"seek-failed-nonseekable-stream","analyzedSha":"c1a1fc7d5941760db3f8431dc05c48cf7f0e30a1","analyzedAt":"2026-09-02T16:58:39.988Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}