os.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright © 2014 Steve Francia <spf@spf13.com>.
  2. // Copyright 2013 tsuru authors. All rights reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  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 afero
  15. import (
  16. "os"
  17. "time"
  18. )
  19. var _ Lstater = (*OsFs)(nil)
  20. // OsFs is a Fs implementation that uses functions provided by the os package.
  21. //
  22. // For details in any method, check the documentation of the os package
  23. // (http://golang.org/pkg/os/).
  24. type OsFs struct{}
  25. func NewOsFs() Fs {
  26. return &OsFs{}
  27. }
  28. func (OsFs) Name() string { return "OsFs" }
  29. func (OsFs) Create(name string) (File, error) {
  30. f, e := os.Create(name)
  31. if f == nil {
  32. // while this looks strange, we need to return a bare nil (of type nil) not
  33. // a nil value of type *os.File or nil won't be nil
  34. return nil, e
  35. }
  36. return f, e
  37. }
  38. func (OsFs) Mkdir(name string, perm os.FileMode) error {
  39. return os.Mkdir(name, perm)
  40. }
  41. func (OsFs) MkdirAll(path string, perm os.FileMode) error {
  42. return os.MkdirAll(path, perm)
  43. }
  44. func (OsFs) Open(name string) (File, error) {
  45. f, e := os.Open(name)
  46. if f == nil {
  47. // while this looks strange, we need to return a bare nil (of type nil) not
  48. // a nil value of type *os.File or nil won't be nil
  49. return nil, e
  50. }
  51. return f, e
  52. }
  53. func (OsFs) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
  54. f, e := os.OpenFile(name, flag, perm)
  55. if f == nil {
  56. // while this looks strange, we need to return a bare nil (of type nil) not
  57. // a nil value of type *os.File or nil won't be nil
  58. return nil, e
  59. }
  60. return f, e
  61. }
  62. func (OsFs) Remove(name string) error {
  63. return os.Remove(name)
  64. }
  65. func (OsFs) RemoveAll(path string) error {
  66. return os.RemoveAll(path)
  67. }
  68. func (OsFs) Rename(oldname, newname string) error {
  69. return os.Rename(oldname, newname)
  70. }
  71. func (OsFs) Stat(name string) (os.FileInfo, error) {
  72. return os.Stat(name)
  73. }
  74. func (OsFs) Chmod(name string, mode os.FileMode) error {
  75. return os.Chmod(name, mode)
  76. }
  77. func (OsFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
  78. return os.Chtimes(name, atime, mtime)
  79. }
  80. func (OsFs) LstatIfPossible(name string) (os.FileInfo, bool, error) {
  81. fi, err := os.Lstat(name)
  82. return fi, true, err
  83. }