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
15package afero
16
17import (
18	"os"
19	"time"
20)
21
22var _ Lstater = (*OsFs)(nil)
23
24// OsFs is a Fs implementation that uses functions provided by the os package.
25//
26// For details in any method, check the documentation of the os package
27// (http://golang.org/pkg/os/).
28type OsFs struct{}
29
30func NewOsFs() Fs {
31	return &OsFs{}
32}
33
34func (OsFs) Name() string { return "OsFs" }
35
36func (OsFs) Create(name string) (File, error) {
37	f, e := os.Create(name)
38	if f == nil {
39		// while this looks strange, we need to return a bare nil (of type nil) not
40		// a nil value of type *os.File or nil won't be nil
41		return nil, e
42	}
43	return f, e
44}
45
46func (OsFs) Mkdir(name string, perm os.FileMode) error {
47	return os.Mkdir(name, perm)
48}
49
50func (OsFs) MkdirAll(path string, perm os.FileMode) error {
51	return os.MkdirAll(path, perm)
52}
53
54func (OsFs) Open(name string) (File, error) {
55	f, e := os.Open(name)
56	if f == nil {
57		// while this looks strange, we need to return a bare nil (of type nil) not
58		// a nil value of type *os.File or nil won't be nil
59		return nil, e
60	}
61	return f, e
62}
63
64func (OsFs) OpenFile(name string, flag int, perm os.FileMode) (File, error) {
65	f, e := os.OpenFile(name, flag, perm)
66	if f == nil {
67		// while this looks strange, we need to return a bare nil (of type nil) not
68		// a nil value of type *os.File or nil won't be nil
69		return nil, e
70	}
71	return f, e
72}
73
74func (OsFs) Remove(name string) error {
75	return os.Remove(name)
76}
77
78func (OsFs) RemoveAll(path string) error {
79	return os.RemoveAll(path)
80}
81
82func (OsFs) Rename(oldname, newname string) error {
83	return os.Rename(oldname, newname)
84}
85
86func (OsFs) Stat(name string) (os.FileInfo, error) {
87	return os.Stat(name)
88}
89
90func (OsFs) Chmod(name string, mode os.FileMode) error {
91	return os.Chmod(name, mode)
92}
93
94func (OsFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
95	return os.Chtimes(name, atime, mtime)
96}
97
98func (OsFs) LstatIfPossible(name string) (os.FileInfo, bool, error) {
99	fi, err := os.Lstat(name)
100	return fi, true, err
101}
102
103func (OsFs) SymlinkIfPossible(oldname, newname string) error {
104	return os.Symlink(oldname, newname)
105}
106
107func (OsFs) ReadlinkIfPossible(name string) (string, error) {
108	return os.Readlink(name)
109}
110