2 * Copyright 2002-2009, Haiku Inc.
3 * Distributed under the terms of the MIT License.
7 * Ingo Weinhold, bonefish@users.sf.net
14 #include <Directory.h>
17 #include <fs_interface.h>
18 #include <NodeMonitor.h>
19 #include "storage_support.h"
25 // Creates an uninitialized BFile.
33 // Creates a copy of the supplied BFile.
34 BFile::BFile(const BFile
& file
)
42 // Creates a BFile and initializes it to the file referred to by
43 // the supplied entry_ref and according to the specified open mode.
44 BFile::BFile(const entry_ref
* ref
, uint32 openMode
)
52 // Creates a BFile and initializes it to the file referred to by
53 // the supplied BEntry and according to the specified open mode.
54 BFile::BFile(const BEntry
* entry
, uint32 openMode
)
58 SetTo(entry
, openMode
);
62 // Creates a BFile and initializes it to the file referred to by
63 // the supplied path name and according to the specified open mode.
64 BFile::BFile(const char* path
, uint32 openMode
)
68 SetTo(path
, openMode
);
72 // Creates a BFile and initializes it to the file referred to by
73 // the supplied path name relative to the specified BDirectory and
74 // according to the specified open mode.
75 BFile::BFile(const BDirectory
*dir
, const char* path
, uint32 openMode
)
79 SetTo(dir
, path
, openMode
);
83 // Frees all allocated resources.
86 // Also called by the BNode destructor, but we rather try to avoid
87 // problems with calling virtual functions in the base class destructor.
88 // Depending on the compiler implementation an object may be degraded to
89 // an object of the base class after the destructor of the derived class
95 // Re-initializes the BFile to the file referred to by the
96 // supplied entry_ref and according to the specified open mode.
98 BFile::SetTo(const entry_ref
* ref
, uint32 openMode
)
103 return (fCStatus
= B_BAD_VALUE
);
105 // if ref->name is absolute, let the path-only SetTo() do the job
106 if (BPrivate::Storage::is_absolute_path(ref
->name
))
107 return SetTo(ref
->name
, openMode
);
109 openMode
|= O_CLOEXEC
;
111 int fd
= _kern_open_entry_ref(ref
->device
, ref
->directory
, ref
->name
,
112 openMode
, DEFFILEMODE
& ~__gUmask
);
124 // Re-initializes the BFile to the file referred to by the
125 // supplied BEntry and according to the specified open mode.
127 BFile::SetTo(const BEntry
* entry
, uint32 openMode
)
132 return (fCStatus
= B_BAD_VALUE
);
133 if (entry
->InitCheck() != B_OK
)
134 return (fCStatus
= entry
->InitCheck());
136 openMode
|= O_CLOEXEC
;
138 int fd
= _kern_open(entry
->fDirFd
, entry
->fName
, openMode
| O_CLOEXEC
,
139 DEFFILEMODE
& ~__gUmask
);
151 // Re-initializes the BFile to the file referred to by the
152 // supplied path name and according to the specified open mode.
154 BFile::SetTo(const char* path
, uint32 openMode
)
159 return (fCStatus
= B_BAD_VALUE
);
161 openMode
|= O_CLOEXEC
;
163 int fd
= _kern_open(-1, path
, openMode
, DEFFILEMODE
& ~__gUmask
);
175 // Re-initializes the BFile to the file referred to by the
176 // supplied path name relative to the specified BDirectory and
177 // according to the specified open mode.
179 BFile::SetTo(const BDirectory
* dir
, const char* path
, uint32 openMode
)
184 return (fCStatus
= B_BAD_VALUE
);
186 openMode
|= O_CLOEXEC
;
188 int fd
= _kern_open(dir
->fDirFd
, path
, openMode
, DEFFILEMODE
& ~__gUmask
);
200 // Reports whether or not the file is readable.
202 BFile::IsReadable() const
204 return InitCheck() == B_OK
205 && ((fMode
& O_RWMASK
) == O_RDONLY
|| (fMode
& O_RWMASK
) == O_RDWR
);
209 // Reports whether or not the file is writable.
211 BFile::IsWritable() const
213 return InitCheck() == B_OK
214 && ((fMode
& O_RWMASK
) == O_WRONLY
|| (fMode
& O_RWMASK
) == O_RDWR
);
218 // Reads a number of bytes from the file into a buffer.
220 BFile::Read(void* buffer
, size_t size
)
222 if (InitCheck() != B_OK
)
224 return _kern_read(get_fd(), -1, buffer
, size
);
228 // Reads a number of bytes from a certain position within the file
231 BFile::ReadAt(off_t location
, void* buffer
, size_t size
)
233 if (InitCheck() != B_OK
)
238 return _kern_read(get_fd(), location
, buffer
, size
);
242 // Writes a number of bytes from a buffer into the file.
244 BFile::Write(const void* buffer
, size_t size
)
246 if (InitCheck() != B_OK
)
248 return _kern_write(get_fd(), -1, buffer
, size
);
252 // Writes a number of bytes from a buffer at a certain position
255 BFile::WriteAt(off_t location
, const void* buffer
, size_t size
)
257 if (InitCheck() != B_OK
)
262 return _kern_write(get_fd(), location
, buffer
, size
);
266 // Seeks to another read/write position within the file.
268 BFile::Seek(off_t offset
, uint32 seekMode
)
270 if (InitCheck() != B_OK
)
272 return _kern_seek(get_fd(), offset
, seekMode
);
276 // Gets the current read/write position within the file.
278 BFile::Position() const
280 if (InitCheck() != B_OK
)
282 return _kern_seek(get_fd(), 0, SEEK_CUR
);
286 // Sets the size of the file.
288 BFile::SetSize(off_t size
)
290 if (InitCheck() != B_OK
)
294 struct stat statData
;
295 statData
.st_size
= size
;
296 return set_stat(statData
, B_STAT_SIZE
| B_STAT_SIZE_INSECURE
);
300 // Gets the size of the file.
302 BFile::GetSize(off_t
* size
) const
304 return BStatable::GetSize(size
);
308 // Assigns another BFile to this BFile.
310 BFile::operator=(const BFile
&file
)
313 // no need to assign us to ourselves
315 if (file
.InitCheck() == B_OK
) {
316 // duplicate the file descriptor
317 int fd
= _kern_dup(file
.get_fd());
332 void BFile::_PhiloFile1() {}
333 void BFile::_PhiloFile2() {}
334 void BFile::_PhiloFile3() {}
335 void BFile::_PhiloFile4() {}
336 void BFile::_PhiloFile5() {}
337 void BFile::_PhiloFile6() {}
340 /*! Gets the file descriptor of the BFile.
342 To be used instead of accessing the BNode's private \c fFd member directly.
344 \returns The file descriptor, or -1 if not properly initialized.
347 BFile::get_fd() const
353 //! Overrides BNode::close_fd() for binary compatibility with BeOS R5.