vfs: check userland buffers before reading them.
[haiku.git] / src / system / boot / loader / file_systems / fat / File.cpp
blob60ac2e8fb5e5c2493d4514e74ee60177a12a5af2
1 /*
2 * Copyright 2003-2013, Axel Dörfler, axeld@pinc-software.de.
3 * Copyright 2008, François Revol <revol@free.fr>
4 * Distributed under the terms of the MIT License.
5 */
8 #include "File.h"
10 #include <sys/stat.h>
11 #include <unistd.h>
13 #include "Directory.h"
16 //#define TRACE(x) dprintf x
17 #define TRACE(x) do {} while (0)
20 namespace FATFS {
23 File::File(Volume &volume, off_t dirEntryOffset, uint32 cluster, off_t size,
24 const char *name)
26 fVolume(volume),
27 fStream(volume, cluster, size, name),
28 fDirEntryOffset(dirEntryOffset)
30 TRACE(("FATFS::File::()\n"));
34 File::~File()
36 TRACE(("FATFS::File::~()\n"));
40 status_t
41 File::InitCheck()
43 if (fStream.InitCheck() != B_OK)
44 return fStream.InitCheck();
46 return B_OK;
50 status_t
51 File::Open(void **_cookie, int mode)
53 TRACE(("FATFS::File::%s(, %d)\n", __FUNCTION__, mode));
54 if (fStream.InitCheck() < B_OK)
55 return fStream.InitCheck();
57 return Node::Open(_cookie, mode);
61 status_t
62 File::Close(void *cookie)
64 return Node::Close(cookie);
68 ssize_t
69 File::ReadAt(void *cookie, off_t pos, void *buffer, size_t bufferSize)
71 TRACE(("FATFS::File::%s(, %Ld,, %d)\n", __FUNCTION__, pos, bufferSize));
72 status_t err;
73 err = fStream.ReadAt(pos, buffer, &bufferSize);
74 if (err < B_OK)
75 return err;
76 return bufferSize;
80 ssize_t
81 File::WriteAt(void *cookie, off_t pos, const void *buffer, size_t bufferSize)
83 off_t streamSize = fStream.Size();
84 uint32 firstCluster = fStream.FirstCluster();
86 // write data
87 size_t written = bufferSize;
88 status_t error = fStream.WriteAt(pos, buffer, &written);
89 if (error != B_OK)
90 return error;
92 // If the file size has changed, we need to adjust the directory entry.
93 if (fStream.Size() > streamSize || fStream.FirstCluster() != firstCluster) {
94 error = Directory::UpdateDirEntry(fVolume, fDirEntryOffset,
95 fStream.FirstCluster(), fStream.Size());
96 if (error != B_OK)
97 return error;
98 // TODO: Undo the changes!
101 return written;
105 status_t
106 File::GetName(char *nameBuffer, size_t bufferSize) const
108 return fStream.GetName(nameBuffer, bufferSize);
112 status_t
113 File::GetFileMap(struct file_map_run *runs, int32 *count)
115 return fStream.GetFileMap(runs, count);
119 int32
120 File::Type() const
122 return S_IFREG;
126 off_t
127 File::Size() const
129 return fStream.Size();
133 ino_t
134 File::Inode() const
136 return fStream.FirstCluster() << 16;
140 } // namespace FATFS