xref: /minix3/minix/lib/libhgfs/dir.c (revision 94e65446c4f963450ea94b8becc02cec062675cd)
1433d6423SLionel Sambuc /* Part of libhgfs - (c) 2009, D.C. van Moolenbroek */
2433d6423SLionel Sambuc 
3433d6423SLionel Sambuc #include "inc.h"
4433d6423SLionel Sambuc 
5433d6423SLionel Sambuc /*===========================================================================*
6433d6423SLionel Sambuc  *				hgfs_opendir				     *
7433d6423SLionel Sambuc  *===========================================================================*/
hgfs_opendir(const char * path,sffs_dir_t * handle)8*94e65446SDavid van Moolenbroek int hgfs_opendir(const char *path, sffs_dir_t *handle)
9433d6423SLionel Sambuc {
10433d6423SLionel Sambuc /* Open a directory. Store a directory handle upon success.
11433d6423SLionel Sambuc  */
12433d6423SLionel Sambuc   int r;
13433d6423SLionel Sambuc 
14433d6423SLionel Sambuc   RPC_REQUEST(HGFS_REQ_OPENDIR);
15433d6423SLionel Sambuc 
16433d6423SLionel Sambuc   path_put(path);
17433d6423SLionel Sambuc 
18433d6423SLionel Sambuc   if ((r = rpc_query()) != OK)
19433d6423SLionel Sambuc 	return r;
20433d6423SLionel Sambuc 
21433d6423SLionel Sambuc   *handle = (sffs_dir_t)RPC_NEXT32;
22433d6423SLionel Sambuc 
23433d6423SLionel Sambuc   return OK;
24433d6423SLionel Sambuc }
25433d6423SLionel Sambuc 
26433d6423SLionel Sambuc /*===========================================================================*
27433d6423SLionel Sambuc  *				hgfs_readdir				     *
28433d6423SLionel Sambuc  *===========================================================================*/
hgfs_readdir(sffs_dir_t handle,unsigned int index,char * buf,size_t size,struct sffs_attr * attr)29433d6423SLionel Sambuc int hgfs_readdir(sffs_dir_t handle, unsigned int index, char *buf,
30433d6423SLionel Sambuc 	size_t size, struct sffs_attr *attr)
31433d6423SLionel Sambuc {
32433d6423SLionel Sambuc /* Read a directory entry from an open directory, using a zero-based index
33433d6423SLionel Sambuc  * number. Upon success, the resulting path name is stored in the given buffer
34433d6423SLionel Sambuc  * and the given attribute structure is filled selectively as requested. Upon
35433d6423SLionel Sambuc  * error, the contents of the path buffer and attribute structure are
36433d6423SLionel Sambuc  * undefined. ENOENT is returned upon end of directory.
37433d6423SLionel Sambuc  */
38433d6423SLionel Sambuc   int r;
39433d6423SLionel Sambuc 
40433d6423SLionel Sambuc   RPC_REQUEST(HGFS_REQ_READDIR);
41433d6423SLionel Sambuc   RPC_NEXT32 = (u32_t)handle;
42433d6423SLionel Sambuc   RPC_NEXT32 = index;
43433d6423SLionel Sambuc 
44433d6423SLionel Sambuc   /* EINVAL signifies end of directory. */
45433d6423SLionel Sambuc   if ((r = rpc_query()) != OK)
46433d6423SLionel Sambuc 	return (r == EINVAL) ? ENOENT : OK;
47433d6423SLionel Sambuc 
48433d6423SLionel Sambuc   attr_get(attr);
49433d6423SLionel Sambuc 
50433d6423SLionel Sambuc   if ((r = path_get(buf, size)) != OK)
51433d6423SLionel Sambuc 	return r;
52433d6423SLionel Sambuc 
53433d6423SLionel Sambuc   /* VMware Player 3 returns an empty name, instead of EINVAL, when reading
54433d6423SLionel Sambuc    * from an EOF position right after opening the directory handle. Seems to be
55433d6423SLionel Sambuc    * a newly introduced bug..
56433d6423SLionel Sambuc    */
57433d6423SLionel Sambuc   return (!buf[0]) ? ENOENT : OK;
58433d6423SLionel Sambuc }
59433d6423SLionel Sambuc 
60433d6423SLionel Sambuc /*===========================================================================*
61433d6423SLionel Sambuc  *				hgfs_closedir				     *
62433d6423SLionel Sambuc  *===========================================================================*/
hgfs_closedir(sffs_dir_t handle)63433d6423SLionel Sambuc int hgfs_closedir(sffs_dir_t handle)
64433d6423SLionel Sambuc {
65433d6423SLionel Sambuc /* Close an open directory.
66433d6423SLionel Sambuc  */
67433d6423SLionel Sambuc 
68433d6423SLionel Sambuc   RPC_REQUEST(HGFS_REQ_CLOSEDIR);
69433d6423SLionel Sambuc   RPC_NEXT32 = (u32_t)handle;
70433d6423SLionel Sambuc 
71433d6423SLionel Sambuc   return rpc_query();
72433d6423SLionel Sambuc }
73