btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / system / libroot / os / fs_attr.cpp
blobfecfdd0773ac7e87d4d642661818aa23d8270c92
1 /*
2 * Copyright 2002-2011, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include <fs_attr.h>
9 #include <syscall_utils.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <stdlib.h>
14 #include <dirent_private.h>
15 #include <errno_private.h>
16 #include <syscalls.h>
17 #include <syscall_utils.h>
20 // TODO: think about adding special syscalls for the read/write/stat functions
21 // to speed them up
24 static DIR *
25 open_attr_dir(int file, const char *path, bool traverse)
27 DIR *dir;
29 int fd = _kern_open_attr_dir(file, path, traverse);
30 if (fd < 0) {
31 __set_errno(fd);
32 return NULL;
35 // allocate the DIR structure
36 if ((dir = __create_dir_struct(fd)) == NULL) {
37 _kern_close(fd);
38 return NULL;
41 return dir;
45 // #pragma mark -
48 extern "C" ssize_t
49 fs_read_attr(int fd, const char* attribute, uint32 /*type*/, off_t pos,
50 void* buffer, size_t readBytes)
52 ssize_t bytes = _kern_read_attr(fd, attribute, pos, buffer, readBytes);
53 RETURN_AND_SET_ERRNO(bytes);
57 extern "C" ssize_t
58 fs_write_attr(int fd, const char* attribute, uint32 type, off_t pos,
59 const void* buffer, size_t writeBytes)
61 // TODO: move this documentation into the Haiku book!
63 // NOTE: This call is deprecated in Haiku and has a number of problems:
64 // On BeOS, it was documented that the "pos" argument is ignored, however,
65 // that did not actually happen if the attribute was backed up by a real
66 // file.
67 // Also, it will truncate any existing attribute, disregarding the specified
68 // position.
70 // The implementation of this function tries to stay compatible with
71 // BeOS in that it clobbers the existing attribute when you write at offset
72 // 0, but it also tries to support programs which continue to write more
73 // chunks.
74 // The new Haiku way is to use fs_open_attr() to get a regular file handle
75 // and use that for writing, then use fs_close_attr() when done. As you
76 // see from this implementation, it saves 2 syscalls per writing a chunk
77 // of data.
79 ssize_t bytes = _kern_write_attr(fd, attribute, type, pos, buffer,
80 writeBytes);
81 RETURN_AND_SET_ERRNO(bytes);
85 extern "C" int
86 fs_remove_attr(int fd, const char* attribute)
88 status_t status = _kern_remove_attr(fd, attribute);
90 RETURN_AND_SET_ERRNO(status);
94 extern "C" int
95 fs_stat_attr(int fd, const char* attribute, struct attr_info* attrInfo)
97 status_t status = _kern_stat_attr(fd, attribute, attrInfo);
98 RETURN_AND_SET_ERRNO(status);
103 fs_open_attr(const char *path, const char *attribute, uint32 type, int openMode)
105 status_t status = _kern_open_attr(-1, path, attribute, type, openMode);
106 RETURN_AND_SET_ERRNO(status);
110 extern "C" int
111 fs_fopen_attr(int fd, const char* attribute, uint32 type, int openMode)
113 status_t status = _kern_open_attr(fd, NULL, attribute, type, openMode);
114 RETURN_AND_SET_ERRNO(status);
118 extern "C" int
119 fs_close_attr(int fd)
121 status_t status = _kern_close(fd);
123 RETURN_AND_SET_ERRNO(status);
127 extern "C" DIR*
128 fs_open_attr_dir(const char* path)
130 return open_attr_dir(-1, path, true);
134 extern "C" DIR*
135 fs_lopen_attr_dir(const char* path)
137 return open_attr_dir(-1, path, false);
140 extern "C" DIR*
141 fs_fopen_attr_dir(int fd)
143 return open_attr_dir(fd, NULL, false);
147 extern "C" int
148 fs_close_attr_dir(DIR* dir)
150 return closedir(dir);
154 extern "C" struct dirent*
155 fs_read_attr_dir(DIR* dir)
157 return readdir(dir);
161 extern "C" void
162 fs_rewind_attr_dir(DIR* dir)
164 rewinddir(dir);