xref: /minix3/minix/lib/libsffs/handle.c (revision 289b04677a1b234d09a045a727f5e614a6c8d716)
1433d6423SLionel Sambuc /* This file contains open file and directory handle management functions.
2433d6423SLionel Sambuc  *
3433d6423SLionel Sambuc  * The entry points into this file are:
4433d6423SLionel Sambuc  *   get_handle		open a handle for an inode and store the handle
5433d6423SLionel Sambuc  *   put_handle		close any handles associated with an inode
6433d6423SLionel Sambuc  *
7433d6423SLionel Sambuc  * Created:
8433d6423SLionel Sambuc  *   April 2009 (D.C. van Moolenbroek)
9433d6423SLionel Sambuc  */
10433d6423SLionel Sambuc 
11433d6423SLionel Sambuc #include "inc.h"
12433d6423SLionel Sambuc 
13433d6423SLionel Sambuc #include <fcntl.h>
14433d6423SLionel Sambuc 
15433d6423SLionel Sambuc /*===========================================================================*
16433d6423SLionel Sambuc  *				get_handle				     *
17433d6423SLionel Sambuc  *===========================================================================*/
get_handle(struct inode * ino)18433d6423SLionel Sambuc int get_handle(struct inode *ino)
19433d6423SLionel Sambuc {
20433d6423SLionel Sambuc /* Get an open file or directory handle for an inode.
21433d6423SLionel Sambuc  */
22433d6423SLionel Sambuc   char path[PATH_MAX];
23433d6423SLionel Sambuc   int r;
24433d6423SLionel Sambuc 
25433d6423SLionel Sambuc   /* If we don't have a file handle yet, try to open the file now. */
26433d6423SLionel Sambuc   if (ino->i_flags & I_HANDLE)
27433d6423SLionel Sambuc 	return OK;
28433d6423SLionel Sambuc 
29433d6423SLionel Sambuc   if ((r = verify_inode(ino, path, NULL)) != OK)
30433d6423SLionel Sambuc 	return r;
31433d6423SLionel Sambuc 
32433d6423SLionel Sambuc   if (IS_DIR(ino)) {
33433d6423SLionel Sambuc 	r = sffs_table->t_opendir(path, &ino->i_dir);
34433d6423SLionel Sambuc   }
35433d6423SLionel Sambuc   else {
36*289b0467SDavid van Moolenbroek 	if (!read_only)
37433d6423SLionel Sambuc 		r = sffs_table->t_open(path, O_RDWR, 0, &ino->i_file);
38433d6423SLionel Sambuc 
39433d6423SLionel Sambuc 	/* Protection or mount status might prevent us from writing. With the
40433d6423SLionel Sambuc 	 * information that we have available, this is the best we can do..
41433d6423SLionel Sambuc 	 */
42*289b0467SDavid van Moolenbroek 	if (read_only || r != OK)
43433d6423SLionel Sambuc 		r = sffs_table->t_open(path, O_RDONLY, 0, &ino->i_file);
44433d6423SLionel Sambuc   }
45433d6423SLionel Sambuc 
46433d6423SLionel Sambuc   if (r != OK)
47433d6423SLionel Sambuc 	return r;
48433d6423SLionel Sambuc 
49433d6423SLionel Sambuc   ino->i_flags |= I_HANDLE;
50433d6423SLionel Sambuc 
51433d6423SLionel Sambuc   return OK;
52433d6423SLionel Sambuc }
53433d6423SLionel Sambuc 
54433d6423SLionel Sambuc /*===========================================================================*
55433d6423SLionel Sambuc  *				put_handle				     *
56433d6423SLionel Sambuc  *===========================================================================*/
put_handle(struct inode * ino)57433d6423SLionel Sambuc void put_handle(struct inode *ino)
58433d6423SLionel Sambuc {
59433d6423SLionel Sambuc /* Close an open file or directory handle associated with an inode.
60433d6423SLionel Sambuc  */
61433d6423SLionel Sambuc   int r;
62433d6423SLionel Sambuc 
63433d6423SLionel Sambuc   if (!(ino->i_flags & I_HANDLE))
64433d6423SLionel Sambuc 	return;
65433d6423SLionel Sambuc 
66433d6423SLionel Sambuc   /* We ignore any errors here, because we can't deal with them anyway. */
67433d6423SLionel Sambuc   if (IS_DIR(ino))
68433d6423SLionel Sambuc 	r = sffs_table->t_closedir(ino->i_dir);
69433d6423SLionel Sambuc   else
70433d6423SLionel Sambuc 	r = sffs_table->t_close(ino->i_file);
71433d6423SLionel Sambuc 
72433d6423SLionel Sambuc   if (r != OK)
73433d6423SLionel Sambuc 	printf("%s: put_handle: handle close operation returned %d\n",
74433d6423SLionel Sambuc 		sffs_name, r);
75433d6423SLionel Sambuc 
76433d6423SLionel Sambuc   ino->i_flags &= ~I_HANDLE;
77433d6423SLionel Sambuc }
78