bootstrap will initialize the log file with key id and baseIV. The below figure shows the layout of log file. +----------------+------------------+------------------+ | keyID(8 bytes) | baseIV(12 bytes)| entry... | +----------------+------------------+------------------+
()
| 949 | // | keyID(8 bytes) | baseIV(12 bytes)| entry... | |
| 950 | // +----------------+------------------+------------------+ |
| 951 | func (lf *logFile) bootstrap() error { |
| 952 | var err error |
| 953 | // delete all the data. because bootstrap is been called while creating vlog and as well |
| 954 | // as replaying log. While replaying log, there may be any data left. So we need to truncate |
| 955 | // everything. |
| 956 | if err = lf.fd.Truncate(0); err != nil { |
| 957 | return y.Wrapf(err, "Error while bootstraping.") |
| 958 | } |
| 959 | |
| 960 | if _, err = lf.fd.Seek(0, io.SeekStart); err != nil { |
| 961 | return y.Wrapf(err, "Error while SeekStart for the logfile %d in logFile.bootstarp", lf.fid) |
| 962 | } |
| 963 | // generate data key for the log file. |
| 964 | var dk *pb.DataKey |
| 965 | if dk, err = lf.registry.latestDataKey(); err != nil { |
| 966 | return y.Wrapf(err, "Error while retrieving datakey in logFile.bootstarp") |
| 967 | } |
| 968 | lf.dataKey = dk |
| 969 | // We'll always preserve vlogHeaderSize for key id and baseIV. |
| 970 | buf := make([]byte, vlogHeaderSize) |
| 971 | // write key id to the buf. |
| 972 | // key id will be zero if the logfile is in plain text. |
| 973 | binary.BigEndian.PutUint64(buf[:8], lf.keyID()) |
| 974 | // generate base IV. It'll be used with offset of the vptr to encrypt the entry. |
| 975 | if _, err := cryptorand.Read(buf[8:]); err != nil { |
| 976 | return y.Wrapf(err, "Error while creating base IV, while creating logfile") |
| 977 | } |
| 978 | // Initialize base IV. |
| 979 | lf.baseIV = buf[8:] |
| 980 | y.AssertTrue(len(lf.baseIV) == 12) |
| 981 | // write the key id and base IV to the file. |
| 982 | _, err = lf.fd.Write(buf) |
| 983 | return err |
| 984 | } |
| 985 | |
| 986 | func (vlog *valueLog) createVlogFile(fid uint32) (*logFile, error) { |
| 987 | path := vlog.fpath(fid) |
no test coverage detected