@@ -3,6 +3,8 @@ package config
33import (
44 "bytes"
55 "compress/zlib"
6+ "errors"
7+ "fmt"
68 "io"
79 "os"
810
@@ -16,30 +18,34 @@ func ExtractEmbeddedConfig(
1618
1719 fd , err := os .Open (embedded_file )
1820 if err != nil {
19- return nil , err
21+ return nil , fmt . Errorf ( "%w: %v" , utils . EmbeddedConfigError , err )
2022 }
2123
2224 // Read a lot of the file into memory so we can extract the
23- // configuration. This solution only loads the first 10mb into
25+ // configuration. This solution only loads the first 100mb into
2426 // memory which should be sufficient for most practical config
2527 // files. If there are embedded binaries they will not be read and
2628 // will be ignored at this stage (thay can be extracted with the
2729 // 'me' accessor).
28- buf , err := utils .ReadAllWithLimit (fd , 10 * 1024 * 1024 )
29- if err != nil {
30- return nil , err
30+ buf , err := utils .ReadAllWithLimit (fd , 100 * 1024 * 1024 )
31+
32+ // It is ok to have a short read here.
33+ if err != nil && ! errors .Is (err , utils .MemoryBufferExceeded ) {
34+ return nil , fmt .Errorf ("%w: %v" , utils .EmbeddedConfigError , err )
3135 }
3236
3337 // Find the embedded marker in the buffer.
3438 match := embedded_re .FindIndex (buf )
3539 if match == nil {
36- return nil , noEmbeddedConfig
40+ return nil , fmt . Errorf ( "%w: Unable to find signature" , utils . EmbeddedConfigError )
3741 }
3842
3943 embedded_string := buf [match [0 ]:]
4044 return decode_embedded_config (embedded_string )
4145}
4246
47+ // Read the embedded config from this binary - just uses the static
48+ // allocated string variable.
4349func read_embedded_config () (* config_proto.Config , error ) {
4450 return decode_embedded_config (FileConfigDefaultYaml )
4551}
@@ -49,14 +55,15 @@ func decode_embedded_config(encoded_string []byte) (*config_proto.Config, error)
4955 idx := bytes .IndexByte (encoded_string , '\n' )
5056
5157 if len (encoded_string ) < idx + 10 {
52- return nil , noEmbeddedConfig
58+ return nil , fmt . Errorf ( "%w: embedded file too short" , utils . EmbeddedConfigError )
5359 }
5460
5561 // If the following line still starts with # then the file is not
5662 // repacked - the repacker will replace all further data with the
5763 // compressed string.
5864 if encoded_string [idx + 1 ] == '#' {
59- return nil , noEmbeddedConfig
65+ return nil , fmt .Errorf ("%w: embedded file is not packed yet" ,
66+ utils .EmbeddedConfigError )
6067 }
6168
6269 // Decompress the rest of the data - note that zlib will ignore
@@ -65,20 +72,20 @@ func decode_embedded_config(encoded_string []byte) (*config_proto.Config, error)
6572 // whole string here.
6673 r , err := zlib .NewReader (bytes .NewReader (encoded_string [idx + 1 :]))
6774 if err != nil {
68- return nil , err
75+ return nil , fmt . Errorf ( "%w: %v" , utils . EmbeddedConfigError , err )
6976 }
7077
7178 b := & bytes.Buffer {}
7279 _ , err = io .Copy (b , r )
7380 if err != nil {
74- return nil , err
81+ return nil , fmt . Errorf ( "%w: %v" , utils . EmbeddedConfigError , err )
7582 }
7683 r .Close ()
7784
7885 result := & config_proto.Config {}
7986 err = yaml .Unmarshal (b .Bytes (), result )
8087 if err != nil {
81- return nil , err
88+ return nil , fmt . Errorf ( "%w: %v" , utils . EmbeddedConfigError , err )
8289 }
8390 return result , nil
8491}
0 commit comments