Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

55 Zeilen
1.6 KiB

  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package driver
  15. import (
  16. "fmt"
  17. "os"
  18. "path/filepath"
  19. "sync"
  20. )
  21. // newTempFile returns a new output file in dir with the provided prefix and suffix.
  22. func newTempFile(dir, prefix, suffix string) (*os.File, error) {
  23. for index := 1; index < 10000; index++ {
  24. path := filepath.Join(dir, fmt.Sprintf("%s%03d%s", prefix, index, suffix))
  25. if _, err := os.Stat(path); err != nil {
  26. return os.Create(path)
  27. }
  28. }
  29. // Give up
  30. return nil, fmt.Errorf("could not create file of the form %s%03d%s", prefix, 1, suffix)
  31. }
  32. var tempFiles []string
  33. var tempFilesMu = sync.Mutex{}
  34. // deferDeleteTempFile marks a file to be deleted by next call to Cleanup()
  35. func deferDeleteTempFile(path string) {
  36. tempFilesMu.Lock()
  37. tempFiles = append(tempFiles, path)
  38. tempFilesMu.Unlock()
  39. }
  40. // cleanupTempFiles removes any temporary files selected for deferred cleaning.
  41. func cleanupTempFiles() {
  42. tempFilesMu.Lock()
  43. for _, f := range tempFiles {
  44. os.Remove(f)
  45. }
  46. tempFiles = nil
  47. tempFilesMu.Unlock()
  48. }