update writing index files to writeAtomicFile

Signed-off-by: Artem Vdovin <arte.vdovin@gmail.com>
pull/30984/head
Artem Vdovin 2 months ago
parent 351bb78ee5
commit 314bd19d11

@ -17,7 +17,7 @@ limitations under the License.
package repo // import "helm.sh/helm/v4/pkg/repo"
import (
"context"
"bytes"
"crypto/rand"
"encoding/base64"
"encoding/json"
@ -28,10 +28,8 @@ import (
"os"
"path/filepath"
"strings"
"time"
"github.com/gofrs/flock"
"helm.sh/helm/v4/internal/fileutil"
"helm.sh/helm/v4/pkg/getter"
"helm.sh/helm/v4/pkg/helmpath"
)
@ -110,42 +108,15 @@ func (r *ChartRepository) DownloadIndexFile() (string, error) {
for name := range indexFile.Entries {
fmt.Fprintln(&charts, name)
}
chartsFile := filepath.Join(r.CachePath, helmpath.CacheChartsFile(r.Config.Name))
os.MkdirAll(filepath.Dir(chartsFile), 0755)
fname := filepath.Join(r.CachePath, helmpath.CacheIndexFile(r.Config.Name))
os.MkdirAll(filepath.Dir(fname), 0755)
// context for lock files
lockCtx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
defer cancel()
// lock the charts file
chLock := flock.New(filepath.Join(r.CachePath, helmpath.CacheChartsFile(r.Config.Name)+".lock"))
chLocked, err := chLock.TryLockContext(lockCtx, 500*time.Millisecond)
if err == nil && chLocked {
defer chLock.Unlock()
}
if err != nil {
return "", err
}
// write charts files after lock
os.WriteFile(chartsFile, []byte(charts.String()), 0644)
// lock the index file
idxLock := flock.New(filepath.Join(r.CachePath, helmpath.CacheIndexFile(r.Config.Name)+".lock"))
idxLocked, err := idxLock.TryLockContext(lockCtx, 500*time.Millisecond)
if err == nil && idxLocked {
defer idxLock.Unlock()
}
if err != nil {
return "", err
}
fileutil.AtomicWriteFile(chartsFile, bytes.NewReader([]byte(charts.String())), 0644)
// Create the index file in the cache directory
return fname, os.WriteFile(fname, index, 0644)
fname := filepath.Join(r.CachePath, helmpath.CacheIndexFile(r.Config.Name))
os.MkdirAll(filepath.Dir(fname), 0755)
return fname, fileutil.AtomicWriteFile(fname, bytes.NewReader(index), 0644)
}
type findChartInRepoURLOptions struct {

@ -18,7 +18,6 @@ package repo
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
@ -31,7 +30,6 @@ import (
"time"
"github.com/Masterminds/semver/v3"
"github.com/gofrs/flock"
"sigs.k8s.io/yaml"
"helm.sh/helm/v4/internal/fileutil"
@ -105,19 +103,6 @@ func NewIndexFile() *IndexFile {
// LoadIndexFile takes a file at the given path and returns an IndexFile object
func LoadIndexFile(path string) (*IndexFile, error) {
lockCtx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
defer cancel()
idxLock := flock.New(filepath.Join(path + ".lock"))
idxLocked, err := idxLock.TryRLockContext(lockCtx, 500*time.Millisecond)
if err == nil && idxLocked {
defer idxLock.Unlock()
}
if err != nil {
return nil, err
}
b, err := os.ReadFile(path)
if err != nil {
return nil, err
@ -240,19 +225,6 @@ func (i IndexFile) Get(name, version string) (*ChartVersion, error) {
//
// The mode on the file is set to 'mode'.
func (i IndexFile) WriteFile(dest string, mode os.FileMode) error {
lockCtx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
defer cancel()
idxLock := flock.New(dest + ".lock")
idxLocked, err := idxLock.TryLockContext(lockCtx, 500*time.Millisecond)
if err == nil && idxLocked {
defer idxLock.Unlock()
}
if err != nil {
return err
}
b, err := yaml.Marshal(i)
if err != nil {
return err
@ -265,19 +237,6 @@ func (i IndexFile) WriteFile(dest string, mode os.FileMode) error {
//
// The mode on the file is set to 'mode'.
func (i IndexFile) WriteJSONFile(dest string, mode os.FileMode) error {
lockCtx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
defer cancel()
idxLock := flock.New(dest + ".lock")
idxLocked, err := idxLock.TryLockContext(lockCtx, 500*time.Millisecond)
if err == nil && idxLocked {
defer idxLock.Unlock()
}
if err != nil {
return err
}
b, err := json.MarshalIndent(i, "", " ")
if err != nil {
return err

Loading…
Cancel
Save