Skip to content

Commit

Permalink
Made the lib cache files re-locatable
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Sep 20, 2024
1 parent 021416b commit 1acb971
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
57 changes: 45 additions & 12 deletions internal/arduino/libraries/libraries.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (library *Library) String() string {
return library.Name + "@" + library.Version.String()
}

func (library *Library) MarshalBinary(out io.Writer) error {
func (library *Library) MarshalBinary(out io.Writer, prefix *paths.Path) error {
writeString := func(in string) error {
inBytes := []byte(in)
if err := binary.Write(out, binary.NativeEndian, uint16(len(inBytes))); err != nil {
Expand Down Expand Up @@ -129,10 +129,23 @@ func (library *Library) MarshalBinary(out io.Writer) error {
writePath := func(in *paths.Path) error {
if in == nil {
return writeString("")
} else if p, err := in.RelFrom(prefix); err != nil {
return err
} else {
return writeString(in.String())
return writeString(p.String())
}
}
writePathList := func(in []*paths.Path) error {
if err := binary.Write(out, binary.NativeEndian, uint16(len(in))); err != nil {
return err
}
for _, p := range in {
if err := writePath(p); err != nil {
return err
}
}
return nil
}
if err := writeString(library.Name); err != nil {
return err
}
Expand Down Expand Up @@ -204,7 +217,7 @@ func (library *Library) MarshalBinary(out io.Writer) error {
return err
}
//writeStringArray(library.Properties.AsSlice())
if err := writeStringArray(library.Examples.AsStrings()); err != nil {
if err := writePathList(library.Examples); err != nil {
return err
}
if err := writeStringArray(library.declaredHeaders); err != nil {
Expand All @@ -219,7 +232,7 @@ func (library *Library) MarshalBinary(out io.Writer) error {
return nil
}

func (library *Library) UnmarshalBinary(in io.Reader) error {
func (library *Library) UnmarshalBinary(in io.Reader, prefix *paths.Path) error {
readString := func() (string, error) {
var len uint16

Check failure on line 237 in internal/arduino/libraries/libraries.go

View workflow job for this annotation

GitHub Actions / check-style (./)

redefines-builtin-id: redefinition of the built-in function len (revive)
if err := binary.Read(in, binary.NativeEndian, &len); err != nil {
Expand Down Expand Up @@ -265,6 +278,30 @@ func (library *Library) UnmarshalBinary(in io.Reader) error {
}
return res, nil
}
readPath := func() (*paths.Path, error) {
if p, err := readString(); err != nil {
return nil, err
} else if p == "" {
return nil, nil
} else {
return prefix.Join(p), nil
}
}
readPathList := func() (paths.PathList, error) {
var len uint16
if err := binary.Read(in, binary.NativeEndian, &len); err != nil {
return nil, err
}
list := paths.NewPathList()
for range len {
if p, err := readPath(); err != nil {
return nil, err
} else {
list.Add(p)
}
}
return list, nil
}
var err error
library.Name, err = readString()
if err != nil {
Expand Down Expand Up @@ -302,25 +339,22 @@ func (library *Library) UnmarshalBinary(in io.Reader) error {
if err != nil {
return err
}
installDir, err := readString()
library.InstallDir, err = readPath()
if err != nil {
return err
}
library.InstallDir = paths.New(installDir)
library.DirName, err = readString()
if err != nil {
return err
}
sourceDir, err := readString()
library.SourceDir = paths.New(sourceDir)
library.SourceDir, err = readPath()
if err != nil {
return err
}
utilityDir, err := readString()
library.UtilityDir, err = readPath()
if err != nil {
return err
}
library.UtilityDir = paths.New(utilityDir)
var location int32
if err := binary.Read(in, binary.NativeEndian, &location); err != nil {
return err
Expand Down Expand Up @@ -368,11 +402,10 @@ func (library *Library) UnmarshalBinary(in io.Reader) error {
// if err != nil {
// return err
// }
examples, err := readStringArray()
library.Examples, err = readPathList()
if err != nil {
return err
}
library.Examples = paths.NewPathList(examples...)
library.declaredHeaders, err = readStringArray()
if err != nil {
return err
Expand Down
9 changes: 5 additions & 4 deletions internal/arduino/libraries/librarieslist.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io"
"sort"

"github.com/arduino/go-paths-helper"
semver "go.bug.st/relaxed-semver"
)

Expand All @@ -44,15 +45,15 @@ func (list *List) Add(libs ...*Library) {
}
}

func (list *List) UnmarshalBinary(in io.Reader) error {
func (list *List) UnmarshalBinary(in io.Reader, prefix *paths.Path) error {
var n int32
if err := binary.Read(in, binary.NativeEndian, &n); err != nil {
return err
}
res := make([]*Library, n)
for i := range res {
var lib Library
if err := lib.UnmarshalBinary(in); err != nil {
if err := lib.UnmarshalBinary(in, prefix); err != nil {
return err
}
res[i] = &lib
Expand All @@ -61,12 +62,12 @@ func (list *List) UnmarshalBinary(in io.Reader) error {
return nil
}

func (list *List) MarshalBinary(out io.Writer) error {
func (list *List) MarshalBinary(out io.Writer, prefix *paths.Path) error {
if err := binary.Write(out, binary.NativeEndian, int32(len(*list))); err != nil {
return err
}
for _, lib := range *list {
if err := lib.MarshalBinary(out); err != nil {
if err := lib.MarshalBinary(out, prefix); err != nil {
return fmt.Errorf("could not encode lib data of %s: %w", lib.InstallDir.String(), err)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (lm *LibrariesManager) loadLibrariesFromDir(librariesDir *LibrariesDir) []*
}
defer cache.Close()

if err := loadedLibs.UnmarshalBinary(cache); err != nil {
if err := loadedLibs.UnmarshalBinary(cache, librariesDir.Path); err != nil {
s := status.Newf(codes.FailedPrecondition, "reading lib cache %[1]s: %[2]s", cacheFilePath, err)
return append(statuses, s)
}
Expand Down Expand Up @@ -251,7 +251,7 @@ func (lm *LibrariesManager) loadLibrariesFromDir(librariesDir *LibrariesDir) []*
s := status.Newf(codes.FailedPrecondition, "creating lib cache %[1]s: %[2]s", cacheFilePath, err)
return append(statuses, s)
}
err = loadedLibs.MarshalBinary(cache)
err = loadedLibs.MarshalBinary(cache, librariesDir.Path)
cache.Close()
if err != nil {
cacheFilePath.Remove()
Expand Down

0 comments on commit 1acb971

Please sign in to comment.