1/*
2  This file contains some kit-wide typedefs and structs that basically
3  emulate most of a normal posix-y type system.  The purpose of hiding
4  everything behind these typedefs is to avoid inconsistencies between
5  various systems (such as the difference in size between off_t on BeOS
6  and some versions of Unix).  To further avoid complications I've also
7  hidden the stat and dirent structs since those vary even more widely.
8
9  THIS CODE COPYRIGHT DOMINIC GIAMPAOLO.  NO WARRANTY IS EXPRESSED
10  OR IMPLIED.  YOU MAY USE THIS CODE AND FREELY DISTRIBUTE IT FOR
11  NON-COMMERCIAL USE AS LONG AS THIS NOTICE REMAINS ATTACHED.
12
13  FOR COMMERCIAL USE, CONTACT DOMINIC GIAMPAOLO (dbg@be.com).
14
15  Dominic Giampaolo
16  dbg@be.com
17*/
18
19
20#ifndef _COMPAT_H
21#define _COMPAT_H
22
23#define _FS_INTERFACE_H
24	// don't include that file
25
26#if (!defined(__BEOS__) && !defined(__HAIKU__))
27#	define dprintf build_platform_dprintf
28#	include <stdio.h>
29#	undef dprintf
30
31typedef struct dirent dirent_t;
32typedef struct iovec iovec;
33
34#endif	// __BEOS__
35
36#include <errno.h>
37#ifndef _ERRORS_H
38#	define _ERRORS_H
39	// don't include <Errors.h>, we use the platform <errno.h>
40#endif
41
42#include <dirent.h>
43#include <stdlib.h>
44#include <stdarg.h>
45#include <string.h>
46#include <fcntl.h>
47#include <time.h>
48
49#if (defined(__BEOS__) || defined(__HAIKU__))
50#	include <OS.h>				/* for typedefs and prototypes */
51#	include <image.h>			/* for a few typedefs */
52#	include <Drivers.h>			/* for various ioctl structs, etc */
53#	include <iovec.h>			/* because we're boneheads sometimes */
54#elif HAIKU_HOST_PLATFORM_FREEBSD
55/* BSD Compilant stat.h */
56#	define __BSD_VISIBLE 1
57#undef __XSI_VISIBLE
58#	define __XSI_VISIBLE 1
59	/* for mknod */
60#	define __POSIX_VISIBLE 200112
61	/* for S_ISLNK, S_ISSOCK and lstat */
62#	include <sys/stat.h>
63#	include <sys/uio.h>
64#	include <image.h>			/* for a few typedefs */
65#	include <Drivers.h>			/* for various ioctl structs, etc */
66typedef unsigned long ulong;
67#else
68#	include <sys/uio.h>
69#	include <image.h>			/* for a few typedefs */
70#	include <Drivers.h>			/* for various ioctl structs, etc */
71#endif
72
73#include <fs_attr.h>
74
75#include "errors.h"
76
77
78/*
79  By default (for portability reasons) the size of off_t's and ino_t's
80  is 32-bit.  You can change the file system to be 64-bit if you want
81  by defining OFF_T_SIZE to be 8.
82
83  NOTE: if you change the size of OFF_T_SIZE to be 8 you will have to
84        go through the code and change any calls to printf() to use the
85        appropriate format for 64-bit integers on your OS.  I have seen
86        4 different formats now: %Ld (BeOS and Linux), %qd (FreeBSD),
87        %lld (Irix) and %I64d (NT).
88*/
89#define OFF_T_SIZE 8
90
91#if OFF_T_SIZE == 4
92typedef long fs_off_t;
93typedef long my_ino_t;
94#elif OFF_T_SIZE == 8
95typedef long long fs_off_t;
96typedef long long my_ino_t;
97#else
98#error OFF_T_SIZE must be either 4 or 8.
99#endif
100
101typedef int my_dev_t;
102typedef int my_mode_t;
103typedef int my_uid_t;
104typedef int my_gid_t;
105
106/* This is the maximum length of a file name.  Adjust it as you see fit */
107#define FILE_NAME_LENGTH    256
108
109/* This is maximum name size for naming a volume or semaphore/lock */
110#define IDENT_NAME_LENGTH   32
111
112
113typedef struct my_dirent {
114    my_dev_t        d_dev;
115    my_dev_t        d_pdev;
116    my_ino_t        d_ino;
117    my_ino_t        d_pino;
118    unsigned short  d_reclen;
119    char            d_name[1];
120} my_dirent_t;
121
122typedef struct {
123    int                 fd;
124    struct my_dirent    ent;
125} MY_DIR;
126
127
128/*
129  This is a pretty regular stat structure but it's our "internal"
130  version since if we depended on the host version we'd be exposed
131  to all sorts of nasty things (different sized ino_t's, etc).
132  We also can't use the normal naming style of "st_" for each field
133  name because on some systems fields like st_atime are really just
134  define's that expand to all sorts of weird stuff.
135*/
136struct my_stat {
137    my_dev_t        dev;        /* "device" that this file resides on */
138    my_ino_t        ino;        /* this file's inode #, unique per device */
139    my_mode_t       mode;       /* mode bits (rwx for user, group, etc) */
140    int             nlink;      /* number of hard links to this file */
141    my_uid_t        uid;        /* user id of the owner of this file */
142    my_gid_t        gid;        /* group id of the owner of this file */
143    fs_off_t        size;       /* size in bytes of this file */
144	dev_t			rdev;		/* device type (not used) */
145    size_t          blksize;    /* preferred block size for i/o */
146    time_t          atime;      /* last access time */
147    time_t          mtime;      /* last modification time */
148    time_t          ctime;      /* last change time, not creation time */
149    time_t          crtime;     /* creation time; not posix but useful */
150};
151
152
153#define MY_S_ATTR_DIR          01000000000 /* attribute directory */
154#define MY_S_ATTR              02000000000 /* attribute */
155#define MY_S_INDEX_DIR         04000000000 /* index (or index directory) */
156#define MY_S_STR_INDEX         00100000000 /* string index */
157#define MY_S_INT_INDEX         00200000000 /* int32 index */
158#define MY_S_UINT_INDEX        00400000000 /* uint32 index */
159#define MY_S_LONG_LONG_INDEX   00010000000 /* int64 index */
160#define MY_S_ULONG_LONG_INDEX  00020000000 /* uint64 index */
161#define MY_S_FLOAT_INDEX       00040000000 /* float index */
162#define MY_S_DOUBLE_INDEX      00001000000 /* double index */
163#define MY_S_ALLOW_DUPS        00002000000 /* allow duplicate entries (currently unused) */
164
165#define MY_S_LINK_SELF_HEALING 00001000000 /* link will be updated if you move its target */
166#define MY_S_LINK_AUTO_DELETE  00002000000 /* link will be deleted if you delete its target */
167
168#define     MY_S_IFMT        00000170000 /* type of file */
169#define     MY_S_IFLNK       00000120000 /* symbolic link */
170#define     MY_S_IFREG       00000100000 /* regular */
171#define     MY_S_IFBLK       00000060000 /* block special */
172#define     MY_S_IFDIR       00000040000 /* directory */
173#define     MY_S_IFCHR       00000020000 /* character special */
174#define     MY_S_IFIFO       00000010000 /* fifo */
175
176#define     MY_S_ISREG(m)    (((m) & MY_S_IFMT) == MY_S_IFREG)
177#define     MY_S_ISLNK(m)    (((m) & MY_S_IFMT) == MY_S_IFLNK)
178#define     MY_S_ISBLK(m)    (((m) & MY_S_IFMT) == MY_S_IFBLK)
179#define     MY_S_ISDIR(m)    (((m) & MY_S_IFMT) == MY_S_IFDIR)
180#define     MY_S_ISCHR(m)    (((m) & MY_S_IFMT) == MY_S_IFCHR)
181#define     MY_S_ISFIFO(m)   (((m) & MY_S_IFMT) == MY_S_IFIFO)
182
183#define MY_S_IUMSK 07777     /* user settable bits */
184
185#define MY_S_ISUID 04000     /* set user id on execution */
186#define MY_S_ISGID 02000     /* set group id on execution */
187
188#define MY_S_ISVTX 01000     /* save swapped text even after use */
189
190#define MY_S_IRWXU 00700     /* read, write, execute: owner */
191#define MY_S_IRUSR 00400     /* read permission: owner */
192#define MY_S_IWUSR 00200     /* write permission: owner */
193#define MY_S_IXUSR 00100     /* execute permission: owner */
194#define MY_S_IRWXG 00070     /* read, write, execute: group */
195#define MY_S_IRGRP 00040     /* read permission: group */
196#define MY_S_IWGRP 00020     /* write permission: group */
197#define MY_S_IXGRP 00010     /* execute permission: group */
198#define MY_S_IRWXO 00007     /* read, write, execute: other */
199#define MY_S_IROTH 00004     /* read permission: other */
200#define MY_S_IWOTH 00002     /* write permission: other */
201#define MY_S_IXOTH 00001     /* execute permission: other */
202
203#define MY_O_RDONLY        0   /* read only */
204#define MY_O_WRONLY        1   /* write only */
205#define MY_O_RDWR          2   /* read and write */
206#define MY_O_RWMASK        3   /* Mask to get open mode */
207
208#define MY_O_CLOEXEC       0x0040  /* close fd on exec */
209#define MY_O_NONBLOCK      0x0080  /* non blocking io */
210#define MY_O_EXCL          0x0100  /* exclusive creat */
211#define MY_O_CREAT         0x0200  /* create and open file */
212#define MY_O_TRUNC         0x0400  /* open with truncation */
213#define MY_O_APPEND        0x0800  /* to end of file */
214#define MY_O_NOCTTY        0x1000  /* currently unsupported */
215#define MY_O_NOTRAVERSE    0x2000  /* do not traverse leaf link */
216#define MY_O_ACCMODE       0x0003  /* currently unsupported */
217#define MY_O_TEXT          0x4000  /* CR-LF translation    */
218#define MY_O_BINARY        0x8000  /* no translation   */
219
220#define MY_SEEK_SET 0
221#define MY_SEEK_CUR 1
222#define MY_SEEK_END 2
223
224// O_NOTRAVERSE is called O_NOFOLLOW under Linux
225#ifndef O_NOTRAVERSE
226	#ifdef O_NOFOLLOW
227		#define O_NOTRAVERSE O_NOFOLLOW
228	#else
229		#define O_NOTRAVERSE 0
230	#endif
231#endif
232#ifndef S_IUMSK
233	#define S_IUMSK (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
234#endif
235
236
237#if (defined(__BEOS__) || defined(__HAIKU__))
238
239typedef attr_info my_attr_info;
240
241#else	// ! __BEOS__
242
243typedef struct my_attr_info
244{
245	uint32		type;
246	fs_off_t	size;
247} my_attr_info;
248
249#endif	// ! __BEOS__
250
251
252#ifndef TRUE
253#define TRUE 1
254#endif
255
256#ifndef FALSE
257#define FALSE 0
258#endif
259
260#if (!defined(__BEOS__) && !defined(__HAIKU__))
261
262#ifdef __cplusplus
263extern "C" {
264#endif
265
266ssize_t    read_pos(int fd, fs_off_t _pos, void *data,  size_t nbytes);
267ssize_t    write_pos(int fd, fs_off_t _pos, const void *data,  size_t nbytes);
268ssize_t    readv_pos(int fd, fs_off_t _pos, const struct iovec *iov, int count);
269ssize_t    writev_pos(int fd, fs_off_t _pos, const struct iovec *iov,  int count);
270
271#ifdef __cplusplus
272}
273#endif
274
275#endif /* ! __BEOS__ */
276
277#ifdef __cplusplus
278extern "C" {
279#endif
280
281void     panic(const char *msg, ...);
282int      device_is_read_only(const char *device);
283int      get_device_block_size(int fd);
284fs_off_t get_num_device_blocks(int fd);
285int      device_is_removeable(int fd);
286int      lock_removeable_device(int fd, bool on_or_off);
287void     hexdump(void *address, int size);
288
289#ifdef __cplusplus
290}
291#endif
292
293#endif /* _COMPAT_H */
294