os.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. // OsFs is a Fs implementation that uses functions provided by the os package.
  20. //
  21. // For details in any method, check the documentation of the os package
  22. // (http://golang.org/pkg/os/).
  23. type OsFs struct{}
  24. func NewOsFs() Fs {
  25. return &OsFs{}
  26. }
  27. func (OsFs) Name() string { return "OsFs" }
  28. func (OsFs) Create(name string) (File, error) {
  29. f, e := os.Create(name)
  30. if f == nil {
  31. // while this looks strange, we need to return a bare nil (of type nil) not
  32. // a nil value of type *os.File or nil won't be nil
  33. return nil, e
  34. }
  35. return f, e
  36. }
  37. func (OsFs) Mkdir(name string, perm os.FileMode) error {
  38. return os.Mkdir(name, perm)
  39. }
  40. func (OsFs) MkdirAll(path string, perm os.FileMode) error {
  41. return os.MkdirAll(path, perm)
  42. }
  43. func (OsFs) Open(name string) (File, error) {
  44. f, e := os.Open(name)
  45. if f == nil {
  46. // while this looks strange, we need to return a bare nil (of type nil) not
  47. // a nil value of type *os.File or nil won't be nil
  48. return nil, e
  49. }
  50. return f, e
  51. }
  52. func (OsFs) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
  53. f, e := os.OpenFile(name, flag, perm)
  54. if f == nil {
  55. // while this looks strange, we need to return a bare nil (of type nil) not
  56. // a nil value of type *os.File or nil won't be nil
  57. return nil, e
  58. }
  59. return f, e
  60. }
  61. func (OsFs) Remove(name string) error {
  62. return os.Remove(name)
  63. }
  64. func (OsFs) RemoveAll(path string) error {
  65. return os.RemoveAll(path)
  66. }
  67. func (OsFs) Rename(oldname, newname string) error {
  68. return os.Rename(oldname, newname)
  69. }
  70. func (OsFs) Stat(name string) (os.FileInfo, error) {
  71. return os.Stat(name)
  72. }
  73. func (OsFs) Chmod(name string, mode os.FileMode) error {
  74. return os.Chmod(name, mode)
  75. }
  76. func (OsFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
  77. return os.Chtimes(name, atime, mtime)
  78. }