2008-07-09 15:16:00 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2002-2008, Axel Dörfler, axeld@pinc-software.de.
|
2004-12-08 05:15:09 +03:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
2002-10-16 20:14:14 +04:00
|
|
|
|
|
|
|
|
|
|
|
#include <fs_attr.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2008-07-09 15:16:00 +04:00
|
|
|
#include "dirent_private.h"
|
2002-10-16 20:14:14 +04:00
|
|
|
#include "syscalls.h"
|
|
|
|
|
|
|
|
|
2008-07-09 15:16:00 +04:00
|
|
|
// TODO: think about adding special syscalls for the read/write/stat functions
|
|
|
|
// to speed them up
|
2004-12-08 05:15:09 +03:00
|
|
|
|
2002-10-17 23:22:04 +04:00
|
|
|
#define RETURN_AND_SET_ERRNO(status) \
|
|
|
|
{ \
|
|
|
|
if (status < 0) { \
|
|
|
|
errno = status; \
|
|
|
|
return -1; \
|
|
|
|
} \
|
|
|
|
return status; \
|
|
|
|
}
|
|
|
|
|
2002-10-16 20:14:14 +04:00
|
|
|
|
2008-07-09 15:16:00 +04:00
|
|
|
ssize_t
|
2004-12-08 05:15:09 +03:00
|
|
|
fs_read_attr(int fd, const char *attribute, uint32 type,
|
|
|
|
off_t pos, void *buffer, size_t readBytes)
|
2002-10-16 20:14:14 +04:00
|
|
|
{
|
|
|
|
ssize_t bytes;
|
|
|
|
|
2004-06-15 19:49:34 +04:00
|
|
|
int attr = _kern_open_attr(fd, attribute, O_RDONLY);
|
2002-10-16 20:14:14 +04:00
|
|
|
if (attr < 0)
|
2002-10-17 23:22:04 +04:00
|
|
|
RETURN_AND_SET_ERRNO(attr);
|
2002-10-16 20:14:14 +04:00
|
|
|
|
2002-10-17 23:22:04 +04:00
|
|
|
// type is not used at all in this function
|
2002-10-16 20:14:14 +04:00
|
|
|
(void)type;
|
|
|
|
|
2004-12-08 05:15:09 +03:00
|
|
|
bytes = _kern_read(attr, pos, buffer, readBytes);
|
2004-06-15 19:49:34 +04:00
|
|
|
_kern_close(attr);
|
2002-10-16 20:14:14 +04:00
|
|
|
|
2002-10-17 23:22:04 +04:00
|
|
|
RETURN_AND_SET_ERRNO(bytes);
|
2002-10-16 20:14:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-09 15:16:00 +04:00
|
|
|
ssize_t
|
2004-12-08 05:15:09 +03:00
|
|
|
fs_write_attr(int fd, const char *attribute, uint32 type,
|
|
|
|
off_t pos, const void *buffer, size_t writeBytes)
|
2002-10-16 20:14:14 +04:00
|
|
|
{
|
|
|
|
ssize_t bytes;
|
|
|
|
|
2007-05-22 13:24:19 +04:00
|
|
|
int attr = _kern_create_attr(fd, attribute, type, O_WRONLY | O_TRUNC);
|
2002-10-16 20:14:14 +04:00
|
|
|
if (attr < 0)
|
2002-10-17 23:22:04 +04:00
|
|
|
RETURN_AND_SET_ERRNO(attr);
|
2002-10-16 20:14:14 +04:00
|
|
|
|
2004-12-08 05:15:09 +03:00
|
|
|
bytes = _kern_write(attr, pos, buffer, writeBytes);
|
2004-06-15 19:49:34 +04:00
|
|
|
_kern_close(attr);
|
2002-10-16 20:14:14 +04:00
|
|
|
|
2002-10-17 23:22:04 +04:00
|
|
|
RETURN_AND_SET_ERRNO(bytes);
|
2002-10-16 20:14:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-09 15:16:00 +04:00
|
|
|
int
|
2002-10-16 20:14:14 +04:00
|
|
|
fs_remove_attr(int fd, const char *attribute)
|
|
|
|
{
|
2004-06-15 19:49:34 +04:00
|
|
|
status_t status = _kern_remove_attr(fd, attribute);
|
2002-10-17 23:22:04 +04:00
|
|
|
|
|
|
|
RETURN_AND_SET_ERRNO(status);
|
2002-10-16 20:14:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-09 15:16:00 +04:00
|
|
|
int
|
2002-10-16 20:14:14 +04:00
|
|
|
fs_stat_attr(int fd, const char *attribute, struct attr_info *attrInfo)
|
|
|
|
{
|
|
|
|
struct stat stat;
|
|
|
|
status_t status;
|
|
|
|
|
2004-06-15 19:49:34 +04:00
|
|
|
int attr = _kern_open_attr(fd, attribute, O_RDONLY);
|
2002-10-16 20:14:14 +04:00
|
|
|
if (attr < 0)
|
2002-10-17 23:22:04 +04:00
|
|
|
RETURN_AND_SET_ERRNO(attr);
|
2002-10-16 20:14:14 +04:00
|
|
|
|
2004-08-29 00:45:00 +04:00
|
|
|
status = _kern_read_stat(attr, NULL, false, &stat, sizeof(struct stat));
|
2002-10-16 20:14:14 +04:00
|
|
|
if (status == B_OK) {
|
|
|
|
attrInfo->type = stat.st_type;
|
|
|
|
attrInfo->size = stat.st_size;
|
|
|
|
}
|
2004-06-15 19:49:34 +04:00
|
|
|
_kern_close(attr);
|
2002-10-16 20:14:14 +04:00
|
|
|
|
2002-10-17 23:22:04 +04:00
|
|
|
RETURN_AND_SET_ERRNO(status);
|
2002-10-16 20:14:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2008-07-09 15:16:00 +04:00
|
|
|
int
|
2002-10-16 20:14:14 +04:00
|
|
|
fs_open_attr(const char *path, const char *attribute, uint32 type, int openMode)
|
|
|
|
{
|
2008-07-09 15:16:00 +04:00
|
|
|
// TODO: implement fs_open_attr() - or remove it completely
|
2002-10-16 20:14:14 +04:00
|
|
|
// if it will be implemented, rename the current fs_open_attr() to fs_fopen_attr()
|
|
|
|
return B_ERROR;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2008-07-09 15:16:00 +04:00
|
|
|
int
|
2002-10-16 20:14:14 +04:00
|
|
|
fs_open_attr(int fd, const char *attribute, uint32 type, int openMode)
|
|
|
|
{
|
2002-10-17 23:22:04 +04:00
|
|
|
status_t status;
|
|
|
|
|
2002-10-16 20:14:14 +04:00
|
|
|
if (openMode & O_CREAT)
|
2004-06-15 19:49:34 +04:00
|
|
|
status = _kern_create_attr(fd, attribute, type, openMode);
|
2002-10-17 23:22:04 +04:00
|
|
|
else
|
2004-06-15 19:49:34 +04:00
|
|
|
status = _kern_open_attr(fd, attribute, openMode);
|
2002-10-16 20:14:14 +04:00
|
|
|
|
2002-10-17 23:22:04 +04:00
|
|
|
RETURN_AND_SET_ERRNO(status);
|
2002-10-16 20:14:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-09 15:16:00 +04:00
|
|
|
int
|
2002-10-16 20:14:14 +04:00
|
|
|
fs_close_attr(int fd)
|
|
|
|
{
|
2004-06-15 19:49:34 +04:00
|
|
|
status_t status = _kern_close(fd);
|
2002-10-17 23:22:04 +04:00
|
|
|
|
|
|
|
RETURN_AND_SET_ERRNO(status);
|
2002-10-16 20:14:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static DIR *
|
|
|
|
open_attr_dir(int file, const char *path)
|
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
|
2004-06-15 19:49:34 +04:00
|
|
|
int fd = _kern_open_attr_dir(file, path);
|
2002-10-17 23:22:04 +04:00
|
|
|
if (fd < 0) {
|
|
|
|
errno = fd;
|
2002-10-16 20:14:14 +04:00
|
|
|
return NULL;
|
2002-10-17 23:22:04 +04:00
|
|
|
}
|
2002-10-16 20:14:14 +04:00
|
|
|
|
|
|
|
/* allocate the memory for the DIR structure */
|
2008-07-09 15:16:00 +04:00
|
|
|
if ((dir = (DIR *)malloc(DIR_BUFFER_SIZE)) == NULL) {
|
2002-10-17 23:22:04 +04:00
|
|
|
errno = B_NO_MEMORY;
|
2004-06-15 19:49:34 +04:00
|
|
|
_kern_close(fd);
|
2002-10-16 20:14:14 +04:00
|
|
|
return NULL;
|
2002-10-17 23:22:04 +04:00
|
|
|
}
|
2002-10-16 20:14:14 +04:00
|
|
|
|
|
|
|
dir->fd = fd;
|
2008-07-09 15:16:00 +04:00
|
|
|
dir->entries_left = 0;
|
2002-10-16 20:14:14 +04:00
|
|
|
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DIR *
|
|
|
|
fs_open_attr_dir(const char *path)
|
|
|
|
{
|
|
|
|
return open_attr_dir(-1, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DIR *
|
|
|
|
fs_fopen_attr_dir(int fd)
|
|
|
|
{
|
|
|
|
return open_attr_dir(fd, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-09 15:16:00 +04:00
|
|
|
int
|
2002-10-16 20:14:14 +04:00
|
|
|
fs_close_attr_dir(DIR *dir)
|
|
|
|
{
|
2004-06-15 19:49:34 +04:00
|
|
|
int status = _kern_close(dir->fd);
|
2002-10-16 20:14:14 +04:00
|
|
|
|
|
|
|
free(dir);
|
|
|
|
|
2002-10-17 23:22:04 +04:00
|
|
|
RETURN_AND_SET_ERRNO(status);
|
2002-10-16 20:14:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct dirent *
|
|
|
|
fs_read_attr_dir(DIR *dir)
|
|
|
|
{
|
2008-07-09 15:16:00 +04:00
|
|
|
return readdir(dir);
|
2002-10-16 20:14:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-09 15:16:00 +04:00
|
|
|
void
|
2002-10-16 20:14:14 +04:00
|
|
|
fs_rewind_attr_dir(DIR *dir)
|
|
|
|
{
|
2008-07-09 15:16:00 +04:00
|
|
|
rewinddir(dir);
|
2002-10-16 20:14:14 +04:00
|
|
|
}
|
|
|
|
|