Skip to content

Commit

Permalink
Added magic number to binary encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Oct 3, 2024
1 parent 05c9574 commit aac9f24
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
15 changes: 15 additions & 0 deletions internal/arduino/libraries/librarieslist.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package libraries

import (
"encoding/binary"
"errors"
"fmt"
"io"
"sort"
Expand Down Expand Up @@ -45,7 +46,18 @@ func (list *List) Add(libs ...*Library) {
}
}

func (list *List) binaryMagicNumber() uint32 {
return 0xAD000001
}

func (list *List) UnmarshalBinary(in io.Reader, prefix *paths.Path) error {
var magic uint32
if err := binary.Read(in, binary.NativeEndian, &magic); err != nil {
return err
}
if magic != list.binaryMagicNumber() {
return errors.New("invalid cache version")
}
var n int32
if err := binary.Read(in, binary.NativeEndian, &n); err != nil {
return err
Expand All @@ -63,6 +75,9 @@ func (list *List) UnmarshalBinary(in io.Reader, prefix *paths.Path) error {
}

func (list *List) MarshalBinary(out io.Writer, prefix *paths.Path) error {
if err := binary.Write(out, binary.NativeEndian, list.binaryMagicNumber()); err != nil {
return err
}
if err := binary.Write(out, binary.NativeEndian, int32(len(*list))); err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@ func (lm *LibrariesManager) loadLibrariesFromDir(librariesDir *LibrariesDir) []*
s := status.Newf(codes.FailedPrecondition, "creating lib cache %[1]s: %[2]s", cacheFilePath, err)
return append(statuses, s)
}
// Preload source files and header
for _, lib := range loadedLibs {
lib.SourceHeaders()
}
// Write the cache
err = loadedLibs.MarshalBinary(cache, librariesDir.Path)
cache.Close()
if err != nil {
Expand Down

0 comments on commit aac9f24

Please sign in to comment.