Refactored fs_open_query() calls a bit - there is no only one internal
implementation that the two public functions call. Adapted to the changes to _kern_open_query(). Minor cleanup. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@10398 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -1,46 +1,41 @@
|
|||||||
/*
|
/*
|
||||||
** Copyright 2004, Axel Dörfler, [email protected]. All rights reserved.
|
* Copyright 2004, Axel Dörfler, [email protected]. All rights reserved.
|
||||||
** Distributed under the terms of the Haiku License.
|
* Distributed under the terms of the MIT License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#include <fs_query.h>
|
#include <fs_query.h>
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#include "syscalls.h"
|
#include "syscalls.h"
|
||||||
|
|
||||||
#define RETURN_AND_SET_ERRNO(status) \
|
|
||||||
{ \
|
|
||||||
if (status < 0) { \
|
|
||||||
errno = status; \
|
|
||||||
return -1; \
|
|
||||||
} \
|
|
||||||
return status; \
|
|
||||||
}
|
|
||||||
|
|
||||||
// for the DIR structure
|
// for the DIR structure
|
||||||
#define BUFFER_SIZE 2048
|
#define BUFFER_SIZE 2048
|
||||||
|
|
||||||
|
|
||||||
DIR *
|
static DIR *
|
||||||
fs_open_query(dev_t device, const char *query, uint32 flags)
|
open_query_etc(dev_t device, const char *query,
|
||||||
|
uint32 flags, port_id port, int32 token)
|
||||||
{
|
{
|
||||||
// check parameters
|
if (device < 0 || query == NULL || query[0] == '\0') {
|
||||||
if (device < 0 || !query) {
|
|
||||||
errno = B_BAD_VALUE;
|
errno = B_BAD_VALUE;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// open
|
// open
|
||||||
int fd = _kern_open_query(device, query, flags, -1, -1);
|
int fd = _kern_open_query(device, query, strlen(query), flags, port, token);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
errno = fd;
|
errno = fd;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// allocate a DIR
|
// allocate a DIR
|
||||||
DIR *dir = (DIR*)malloc(BUFFER_SIZE);
|
DIR *dir = (DIR *)malloc(BUFFER_SIZE);
|
||||||
if (!dir) {
|
if (!dir) {
|
||||||
_kern_close(fd);
|
_kern_close(fd);
|
||||||
errno = B_NO_MEMORY;
|
errno = B_NO_MEMORY;
|
||||||
@@ -51,56 +46,53 @@ fs_open_query(dev_t device, const char *query, uint32 flags)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
DIR *
|
||||||
|
fs_open_query(dev_t device, const char *query, uint32 flags)
|
||||||
|
{
|
||||||
|
return open_query_etc(device, query, flags, -1, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
DIR *
|
DIR *
|
||||||
fs_open_live_query(dev_t device, const char *query,
|
fs_open_live_query(dev_t device, const char *query,
|
||||||
uint32 flags, port_id port, int32 token)
|
uint32 flags, port_id port, int32 token)
|
||||||
{
|
{
|
||||||
// check parameters
|
// check parameters
|
||||||
if (device < 0 || !query || port < 0) {
|
if (port < 0) {
|
||||||
errno = B_BAD_VALUE;
|
errno = B_BAD_VALUE;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
flags |= B_LIVE_QUERY;
|
|
||||||
// open
|
return open_query_etc(device, query, flags | B_LIVE_QUERY, port, token);
|
||||||
int fd = _kern_open_query(device, query, flags, port, token);
|
|
||||||
if (fd < 0) {
|
|
||||||
errno = fd;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
// allocate a DIR
|
|
||||||
DIR *dir = (DIR*)malloc(BUFFER_SIZE);
|
|
||||||
if (!dir) {
|
|
||||||
_kern_close(fd);
|
|
||||||
errno = B_NO_MEMORY;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
dir->fd = fd;
|
|
||||||
return dir;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
fs_close_query(DIR *d)
|
fs_close_query(DIR *dir)
|
||||||
{
|
{
|
||||||
if (!d)
|
if (dir == NULL) {
|
||||||
RETURN_AND_SET_ERRNO(B_BAD_VALUE);
|
errno = B_BAD_VALUE;
|
||||||
int fd = d->fd;
|
return -1;
|
||||||
free(d);
|
}
|
||||||
|
|
||||||
|
int fd = dir->fd;
|
||||||
|
free(dir);
|
||||||
return _kern_close(fd);
|
return _kern_close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
struct dirent *
|
struct dirent *
|
||||||
fs_read_query(DIR *d)
|
fs_read_query(DIR *dir)
|
||||||
{
|
{
|
||||||
// check parameters
|
// check parameters
|
||||||
if (!d) {
|
if (dir == NULL) {
|
||||||
errno = B_BAD_VALUE;
|
errno = B_BAD_VALUE;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// read
|
// read
|
||||||
int32 bufferSize = BUFFER_SIZE - ((uint8*)&d->ent - (uint8*)d);
|
int32 bufferSize = BUFFER_SIZE - ((uint8 *)&dir->ent - (uint8 *)dir);
|
||||||
ssize_t result = _kern_read_dir(d->fd, &d->ent, bufferSize, 1);
|
ssize_t result = _kern_read_dir(dir->fd, &dir->ent, bufferSize, 1);
|
||||||
if (result < 0) {
|
if (result < 0) {
|
||||||
errno = result;
|
errno = result;
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -109,16 +101,17 @@ fs_read_query(DIR *d)
|
|||||||
errno = B_ENTRY_NOT_FOUND;
|
errno = B_ENTRY_NOT_FOUND;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return &d->ent;
|
return &dir->ent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
status_t
|
status_t
|
||||||
get_path_for_dirent(struct dirent *dent, char *buf, size_t len)
|
get_path_for_dirent(struct dirent *dent, char *buffer, size_t length)
|
||||||
{
|
{
|
||||||
if (!dent || !buf)
|
if (dent == NULL || buffer == NULL)
|
||||||
return B_BAD_VALUE;
|
return B_BAD_VALUE;
|
||||||
|
|
||||||
return _kern_entry_ref_to_path(dent->d_pdev, dent->d_pino, dent->d_name,
|
return _kern_entry_ref_to_path(dent->d_pdev, dent->d_pino, dent->d_name,
|
||||||
buf, len);
|
buffer, length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user