summaryrefslogtreecommitdiff
path: root/cpu/sysdeps/unix/sysv/linux/i386/sys-getdents.c
blob: e09a08d3b4ab5fb3d8d2c43fce580f562f5666f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/***************************************************************
  SYS-GETDENTS.C

       Linux/i386/ILP32 implementation of __cpu_sys_getdents().

       PART OF : CPU - library .

       USAGE   : Internal only .

       Copyright (C) 2026  by Andrew V.Kosteltsev.
       All Rights Reserved.
 ***************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <dirent.h>
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>

#include <libmcpu.h>
#include <cpu-sysdeps.h>

#define __CPU_HOST_DIRENT_BUFFER 4096U

struct __cpu_linux_dirent64
{
  uint64_t       d_ino;
  int64_t        d_off;
  unsigned short d_reclen;
  unsigned char  d_type;
  char           d_name[];
};

static int
__cpu_dirent_utf8_valid( const unsigned char *s, size_t n )
{
  size_t i = 0;

  while( i < n )
  {
    unsigned int c = s[i++];

    if( c < 0x80U ) continue;
    if( c >= 0xc2U && c <= 0xdfU )
    {
      if( i >= n || ( s[i] & 0xc0U ) != 0x80U ) return( 0 );
      ++i; continue;
    }
    if( c == 0xe0U )
    {
      if( i + 1 >= n || s[i] < 0xa0U || s[i] > 0xbfU ||
          ( s[i + 1] & 0xc0U ) != 0x80U ) return( 0 );
      i += 2; continue;
    }
    if( c >= 0xe1U && c <= 0xecU )
    {
      if( i + 1 >= n || ( s[i] & 0xc0U ) != 0x80U ||
          ( s[i + 1] & 0xc0U ) != 0x80U ) return( 0 );
      i += 2; continue;
    }
    if( c == 0xedU )
    {
      if( i + 1 >= n || s[i] < 0x80U || s[i] > 0x9fU ||
          ( s[i + 1] & 0xc0U ) != 0x80U ) return( 0 );
      i += 2; continue;
    }
    if( c >= 0xeeU && c <= 0xefU )
    {
      if( i + 1 >= n || ( s[i] & 0xc0U ) != 0x80U ||
          ( s[i + 1] & 0xc0U ) != 0x80U ) return( 0 );
      i += 2; continue;
    }
    if( c == 0xf0U )
    {
      if( i + 2 >= n || s[i] < 0x90U || s[i] > 0xbfU ||
          ( s[i + 1] & 0xc0U ) != 0x80U ||
          ( s[i + 2] & 0xc0U ) != 0x80U ) return( 0 );
      i += 3; continue;
    }
    if( c >= 0xf1U && c <= 0xf3U )
    {
      if( i + 2 >= n || ( s[i] & 0xc0U ) != 0x80U ||
          ( s[i + 1] & 0xc0U ) != 0x80U ||
          ( s[i + 2] & 0xc0U ) != 0x80U ) return( 0 );
      i += 3; continue;
    }
    if( c == 0xf4U )
    {
      if( i + 2 >= n || s[i] < 0x80U || s[i] > 0x8fU ||
          ( s[i + 1] & 0xc0U ) != 0x80U ||
          ( s[i + 2] & 0xc0U ) != 0x80U ) return( 0 );
      i += 3; continue;
    }
    return( 0 );
  }
  return( 1 );
}

static __mpu_uint64_t
__cpu_dirent_type( unsigned char type )
{
  switch( type )
  {
#ifdef DT_FIFO
  case DT_FIFO: return( __CPU_DT_FIFO );
#endif
#ifdef DT_CHR
  case DT_CHR:  return( __CPU_DT_CHR );
#endif
#ifdef DT_DIR
  case DT_DIR:  return( __CPU_DT_DIR );
#endif
#ifdef DT_BLK
  case DT_BLK:  return( __CPU_DT_BLK );
#endif
#ifdef DT_REG
  case DT_REG:  return( __CPU_DT_REG );
#endif
#ifdef DT_LNK
  case DT_LNK:  return( __CPU_DT_LNK );
#endif
#ifdef DT_SOCK
  case DT_SOCK: return( __CPU_DT_SOCK );
#endif
  default:      return( __CPU_DT_UNKNOWN );
  }
}

__mpu_int64_t
__cpu_sys_getdents( int fd, struct __cpu_dirent *dirp, __mpu_uint64_t count )
{
  unsigned char hostbuf[__CPU_HOST_DIRENT_BUFFER];
  struct stat st;
  off_t start;
  off_t resume;
  ssize_t nread;
  size_t pos = 0;
  size_t nrecords = 0;
  size_t maxrecords;

  if( !dirp ) return( -(__mpu_int64_t)__MCPU_EFAULT );
  if( count < __CPU_DIRENT_SIZE ) return( -(__mpu_int64_t)__MCPU_EINVAL );

  maxrecords = (size_t)( count / __CPU_DIRENT_SIZE );

  if( fstat( fd, &st ) < 0 ) return( -(__mpu_int64_t)__cpu_errno_from_host( errno ) );
  if( !S_ISDIR( st.st_mode ) ) return( -(__mpu_int64_t)__MCPU_ENOTDIR );

  start = lseek( fd, (off_t)0, SEEK_CUR );
  if( start == (off_t)-1 ) return( -(__mpu_int64_t)__cpu_errno_from_host( errno ) );
  resume = start;

  nread = syscall( SYS_getdents64, fd, hostbuf, sizeof( hostbuf ) );
  if( nread < 0 ) return( -(__mpu_int64_t)__cpu_errno_from_host( errno ) );
  if( nread == 0 ) return( 0 );

  while( pos < (size_t)nread )
  {
    struct __cpu_linux_dirent64 *de;
    const char *nul;
    size_t namespace;
    size_t namelen;

    if( (size_t)nread - pos < offsetof( struct __cpu_linux_dirent64, d_name ) + 1U )
      goto malformed;

    de = (struct __cpu_linux_dirent64 *)( hostbuf + pos );
    if( de->d_reclen < offsetof( struct __cpu_linux_dirent64, d_name ) + 1U ||
        de->d_reclen > (size_t)nread - pos ) goto malformed;

    if( nrecords >= maxrecords ) break;

    namespace = de->d_reclen - offsetof( struct __cpu_linux_dirent64, d_name );
    nul = (const char *)memchr( de->d_name, '\0', namespace );
    if( !nul ) goto malformed;
    namelen = (size_t)( nul - de->d_name );

    if( namelen >= __CPU_DIRENT_NAME_SIZE )
    {
      if( lseek( fd, resume, SEEK_SET ) == (off_t)-1 )
        return( -(__mpu_int64_t)__cpu_errno_from_host( errno ) );
      return( nrecords ? (__mpu_int64_t)( nrecords * __CPU_DIRENT_SIZE )
                       : -(__mpu_int64_t)__MCPU_ENAMETOOLONG );
    }

    if( !__cpu_dirent_utf8_valid( (const unsigned char *)de->d_name, namelen ) )
    {
      if( lseek( fd, resume, SEEK_SET ) == (off_t)-1 )
        return( -(__mpu_int64_t)__cpu_errno_from_host( errno ) );
      return( nrecords ? (__mpu_int64_t)( nrecords * __CPU_DIRENT_SIZE )
                       : -(__mpu_int64_t)__MCPU_EILSEQ );
    }

    dirp[nrecords].d_ino = (__mpu_uint64_t)de->d_ino;
    dirp[nrecords].d_off = (__mpu_int64_t)de->d_off;
    dirp[nrecords].d_reclen = __CPU_DIRENT_SIZE;
    dirp[nrecords].d_type = __cpu_dirent_type( de->d_type );
    memset( dirp[nrecords].d_name, 0, __CPU_DIRENT_NAME_SIZE );
    memcpy( dirp[nrecords].d_name, de->d_name, namelen );

    resume = (off_t)de->d_off;
    ++nrecords;
    pos += de->d_reclen;
  }

  if( pos < (size_t)nread )
  {
    if( lseek( fd, resume, SEEK_SET ) == (off_t)-1 )
      return( -(__mpu_int64_t)__cpu_errno_from_host( errno ) );
  }

  return( (__mpu_int64_t)( nrecords * __CPU_DIRENT_SIZE ) );

malformed:
  if( lseek( fd, start, SEEK_SET ) == (off_t)-1 )
    return( -(__mpu_int64_t)__cpu_errno_from_host( errno ) );
  return( -(__mpu_int64_t)__MCPU_EIO );

} /* End of __cpu_sys_getdents() */