file.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2015 CoreOS, Inc.
  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 system
  15. import (
  16. "fmt"
  17. "io/ioutil"
  18. "log"
  19. "os"
  20. "os/exec"
  21. "path"
  22. "strconv"
  23. "github.com/rancher/os/config/cloudinit/config"
  24. )
  25. // File is a top-level structure which embeds its underlying configuration,
  26. // config.File, and provides the system-specific Permissions().
  27. type File struct {
  28. config.File
  29. }
  30. func (f *File) Permissions() (os.FileMode, error) {
  31. if f.RawFilePermissions == "" {
  32. return os.FileMode(0644), nil
  33. }
  34. // Parse string representation of file mode as integer
  35. perm, err := strconv.ParseInt(f.RawFilePermissions, 8, 32)
  36. if err != nil {
  37. return 0, fmt.Errorf("Unable to parse file permissions %q as integer", f.RawFilePermissions)
  38. }
  39. return os.FileMode(perm), nil
  40. }
  41. // WriteFile writes given endecoded file to the filesystem
  42. func WriteFile(f *File, root string) (string, error) {
  43. if f.Encoding != "" {
  44. return "", fmt.Errorf("Unable to write file with encoding %s", f.Encoding)
  45. }
  46. fullpath := path.Join(root, f.Path)
  47. dir := path.Dir(fullpath)
  48. log.Printf("Writing file to %q", fullpath)
  49. if err := EnsureDirectoryExists(dir); err != nil {
  50. return "", err
  51. }
  52. perm, err := f.Permissions()
  53. if err != nil {
  54. return "", err
  55. }
  56. var tmp *os.File
  57. // Create a temporary file in the same directory to ensure it's on the same filesystem
  58. if tmp, err = ioutil.TempFile(dir, "cloudinit-temp"); err != nil {
  59. return "", err
  60. }
  61. if err := ioutil.WriteFile(tmp.Name(), []byte(f.Content), perm); err != nil {
  62. return "", err
  63. }
  64. if err := tmp.Close(); err != nil {
  65. return "", err
  66. }
  67. // Ensure the permissions are as requested (since WriteFile can be affected by sticky bit)
  68. if err := os.Chmod(tmp.Name(), perm); err != nil {
  69. return "", err
  70. }
  71. if f.Owner != "" {
  72. // We shell out since we don't have a way to look up unix groups natively
  73. cmd := exec.Command("chown", f.Owner, tmp.Name())
  74. if err := cmd.Run(); err != nil {
  75. return "", err
  76. }
  77. }
  78. if err := os.Rename(tmp.Name(), fullpath); err != nil {
  79. return "", err
  80. }
  81. log.Printf("Wrote file to %q", fullpath)
  82. return fullpath, nil
  83. }
  84. func EnsureDirectoryExists(dir string) error {
  85. info, err := os.Stat(dir)
  86. if err == nil {
  87. if !info.IsDir() {
  88. return fmt.Errorf("%s is not a directory", dir)
  89. }
  90. } else {
  91. err = os.MkdirAll(dir, 0755)
  92. if err != nil {
  93. return err
  94. }
  95. }
  96. return nil
  97. }