/
krepost
/
deckhouse
Обзор
Документация
Войти
/
krepost
/
deckhouse
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
testing/hooks/tmpdir.go
123 строки
2 KB
Pavel Okhlopkov
[deckhouse-controller] prepare to add wrapcheck linter (#16429)
20 янв 2026, 17:09
Не верифицирован
20 янв 2026, 17:09
2c3f0a3
Код
Авторство
О чём код?
/* Copyright 2021 Flant JSC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package hooks import ( "fmt" "os" "path/filepath" "strconv" "strings" "sync" "time" ) var rand uint32 var randmu sync.Mutex func reseed() uint32 { return uint32(time.Now().UnixNano() + int64(os.Getpid())) } func nextRandom() string { randmu.Lock() r := rand if r == 0 { r = reseed() } r = r*1664525 + 1013904223 // constants from Numerical Recipes rand = r randmu.Unlock() return strconv.Itoa(int(1e9 + r%1e9))[1:] } func TempFileWithPerms(dir, pattern string, perms int) (*os.File, error) { if dir == "" { dir = os.TempDir() } var prefix, suffix string if pos := strings.LastIndex(pattern, "*"); pos != -1 { prefix, suffix = pattern[:pos], pattern[pos+1:] } else { prefix = pattern } var ( nconflict = 0 f = new(os.File) err error ) for i := 0; i < 10000; i++ { name := filepath.Join(dir, prefix+nextRandom()+suffix) f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, os.FileMode(perms)) if os.IsExist(err) { if nconflict++; nconflict > 10 { randmu.Lock() rand = reseed() randmu.Unlock() } continue } break } if err != nil { return f, fmt.Errorf("open file: %w", err) } return f, nil } func TempDirWithPerms(dir, prefix string, perms int) (string, error) { if dir == "" { dir = os.TempDir() } var ( nconflict = 0 err error name string ) for i := 0; i < 10000; i++ { try := filepath.Join(dir, prefix+nextRandom()) err = os.Mkdir(try, os.FileMode(perms)) if os.IsExist(err) { if nconflict++; nconflict > 10 { randmu.Lock() rand = reseed() randmu.Unlock() } continue } if os.IsNotExist(err) { if _, err := os.Stat(dir); os.IsNotExist(err) { return "", fmt.Errorf("stat: %w", err) } } if err == nil { name = try } break } if err != nil { return name, fmt.Errorf("mkdir: %w", err) } return name, nil }