Skip to content

Commit

Permalink
Added error check on cache write
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaglie committed Sep 18, 2024
1 parent bbc8aa2 commit 5ae2c30
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 43 deletions.
153 changes: 111 additions & 42 deletions internal/arduino/libraries/libraries.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,63 +93,132 @@ func (library *Library) String() string {
return library.Name + "@" + library.Version.String()
}

func (library *Library) MarshalBinary() []byte {
func (library *Library) MarshalBinary() ([]byte, error) {
buffer := bytes.NewBuffer(make([]byte, 0, 4096))
writeString := func(in string) {
writeString := func(in string) error {
inBytes := []byte(in)
binary.Write(buffer, binary.NativeEndian, uint16(len(inBytes)))
buffer.Write(inBytes)
if err := binary.Write(buffer, binary.NativeEndian, uint16(len(inBytes))); err != nil {
return err
}
_, err := buffer.Write(inBytes)
return err
}
writeStringArray := func(in []string) {
binary.Write(buffer, binary.NativeEndian, uint16(len(in)))
writeStringArray := func(in []string) error {
if err := binary.Write(buffer, binary.NativeEndian, uint16(len(in))); err != nil {
return err
}
for _, i := range in {
writeString(i)
if err := writeString(i); err != nil {
return err
}
}
return nil
}
writeMap := func(in map[string]bool) {
binary.Write(buffer, binary.NativeEndian, uint16(len(in)))
writeMap := func(in map[string]bool) error {
if err := binary.Write(buffer, binary.NativeEndian, uint16(len(in))); err != nil {
return err
}
for k, v := range in {
writeString(k)
binary.Write(buffer, binary.NativeEndian, v)
if err := writeString(k); err != nil {
return err
}
if err := binary.Write(buffer, binary.NativeEndian, v); err != nil {
return err
}
}
return nil
}
writePath := func(in *paths.Path) {
writePath := func(in *paths.Path) error {
if in == nil {
writeString("")
return writeString("")
} else {
writeString(in.String())
return writeString(in.String())
}
}
writeString(library.Name)
writeString(library.Author)
writeString(library.Maintainer)
writeString(library.Sentence)
writeString(library.Paragraph)
writeString(library.Website)
writeString(library.Category)
writeStringArray(library.Architectures)
writeStringArray(library.Types)
writePath(library.InstallDir)
writeString(library.DirName)
writePath(library.SourceDir)
writePath(library.UtilityDir)
binary.Write(buffer, binary.NativeEndian, int32(library.Location))
if err := writeString(library.Name); err != nil {
return nil, err
}
if err := writeString(library.Author); err != nil {
return nil, err
}
if err := writeString(library.Maintainer); err != nil {
return nil, err
}
if err := writeString(library.Sentence); err != nil {
return nil, err
}
if err := writeString(library.Paragraph); err != nil {
return nil, err
}
if err := writeString(library.Website); err != nil {
return nil, err
}
if err := writeString(library.Category); err != nil {
return nil, err
}
if err := writeStringArray(library.Architectures); err != nil {
return nil, err
}
if err := writeStringArray(library.Types); err != nil {
return nil, err
}
if err := writePath(library.InstallDir); err != nil {
return nil, err
}
if err := writeString(library.DirName); err != nil {
return nil, err
}
if err := writePath(library.SourceDir); err != nil {
return nil, err
}
if err := writePath(library.UtilityDir); err != nil {
return nil, err
}
if err := binary.Write(buffer, binary.NativeEndian, int32(library.Location)); err != nil {
return nil, err
}
// library.ContainerPlatform *cores.PlatformRelease `json:""`
binary.Write(buffer, binary.NativeEndian, int32(library.Layout))
binary.Write(buffer, binary.NativeEndian, library.DotALinkage)
binary.Write(buffer, binary.NativeEndian, library.Precompiled)
binary.Write(buffer, binary.NativeEndian, library.PrecompiledWithSources)
writeString(library.LDflags)
binary.Write(buffer, binary.NativeEndian, library.IsLegacy)
binary.Write(buffer, binary.NativeEndian, library.InDevelopment)
writeString(library.Version.String())
writeString(library.License)
if err := binary.Write(buffer, binary.NativeEndian, int32(library.Layout)); err != nil {
return nil, err
}
if err := binary.Write(buffer, binary.NativeEndian, library.DotALinkage); err != nil {
return nil, err
}
if err := binary.Write(buffer, binary.NativeEndian, library.Precompiled); err != nil {
return nil, err
}
if err := binary.Write(buffer, binary.NativeEndian, library.PrecompiledWithSources); err != nil {
return nil, err
}
if err := writeString(library.LDflags); err != nil {
return nil, err
}
if err := binary.Write(buffer, binary.NativeEndian, library.IsLegacy); err != nil {
return nil, err
}
if err := binary.Write(buffer, binary.NativeEndian, library.InDevelopment); err != nil {
return nil, err
}
if err := writeString(library.Version.String()); err != nil {
return nil, err
}
if err := writeString(library.License); err != nil {
return nil, err
}
//writeStringArray(library.Properties.AsSlice())
writeStringArray(library.Examples.AsStrings())
writeStringArray(library.declaredHeaders)
writeStringArray(library.sourceHeaders)
writeMap(library.CompatibleWith)
return buffer.Bytes()
if err := writeStringArray(library.Examples.AsStrings()); err != nil {
return nil, err
}
if err := writeStringArray(library.declaredHeaders); err != nil {
return nil, err
}
if err := writeStringArray(library.sourceHeaders); err != nil {
return nil, err
}
if err := writeMap(library.CompatibleWith); err != nil {
return nil, err
}
return buffer.Bytes(), nil
}

func (library *Library) UnmarshalBinary(in io.Reader) error {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,11 @@ func (lm *LibrariesManager) loadLibrariesFromDir(librariesDir *LibrariesDir) []*
return append(statuses, s)
}
for _, lib := range loadedLibs {
if _, err := cache.Write(lib.MarshalBinary()); err != nil {
data, err := lib.MarshalBinary()
if err != nil {
panic("could not encode lib data for: " + lib.InstallDir.String())
}
if _, err := cache.Write(data); err != nil {
cacheFilePath.Remove()
s := status.Newf(codes.FailedPrecondition, "writing lib cache %[1]s: %[2]s", cacheFilePath, err)
return append(statuses, s)
Expand Down

0 comments on commit 5ae2c30

Please sign in to comment.