xref: /haiku/headers/private/file_systems/fs_ops_support.h (revision ed24eb5ff12640d052171c6a7feba37fab8a75d1)
1 /*
2  * Copyright 2022, Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef FS_OPS_SUPPORT_H
6 #define FS_OPS_SUPPORT_H
7 
8 #ifndef FS_SHELL
9 #	include <kernel.h>
10 #	include <kernel/debug.h>
11 #	include <dirent.h>
12 #else
13 #	include "fssh_kernel_priv.h"
14 #endif
15 
16 
17 /*!	Converts a given open mode (e.g. O_RDONLY) into access modes (e.g. R_OK).
18  */
19 static inline int
20 open_mode_to_access(int openMode)
21 {
22 	openMode &= O_RWMASK;
23 	if (openMode == O_RDONLY)
24 		return R_OK;
25 	if (openMode == O_WRONLY)
26 		return W_OK;
27 	if (openMode == O_RDWR)
28 		return R_OK | W_OK;
29 	return 0;
30 }
31 
32 
33 /*! Computes and assigns `dirent->d_reclen`, adjusts `bufferRemaining` accordingly,
34  * and either advances to the next buffer, or returns NULL if no space remains.
35  */
36 static inline struct dirent*
37 next_dirent(struct dirent* dirent, size_t nameLength, size_t& bufferRemaining)
38 {
39 	const size_t reclen = offsetof(struct dirent, d_name) + nameLength + 1;
40 #ifdef ASSERT
41 	ASSERT(reclen <= bufferRemaining);
42 #endif
43 	dirent->d_reclen = reclen;
44 
45 	const size_t roundedReclen = ROUNDUP(reclen, alignof(struct dirent));
46 	if (roundedReclen >= bufferRemaining) {
47 		bufferRemaining -= reclen;
48 		return NULL;
49 	}
50 	dirent->d_reclen = roundedReclen;
51 	bufferRemaining -= roundedReclen;
52 
53 	return (struct dirent*)((uint8*)dirent + roundedReclen);
54 }
55 
56 
57 #endif	// FS_OPS_SUPPORT_H
58