1 /* This file contains open file and directory handle management functions.
2 *
3 * The entry points into this file are:
4 * get_handle open a handle for an inode and store the handle
5 * put_handle close any handles associated with an inode
6 *
7 * Created:
8 * April 2009 (D.C. van Moolenbroek)
9 */
10
11 #include "inc.h"
12
13 #include <fcntl.h>
14
15 /*===========================================================================*
16 * get_handle *
17 *===========================================================================*/
get_handle(struct inode * ino)18 int get_handle(struct inode *ino)
19 {
20 /* Get an open file or directory handle for an inode.
21 */
22 char path[PATH_MAX];
23 int r;
24
25 /* If we don't have a file handle yet, try to open the file now. */
26 if (ino->i_flags & I_HANDLE)
27 return OK;
28
29 if ((r = verify_inode(ino, path, NULL)) != OK)
30 return r;
31
32 if (IS_DIR(ino)) {
33 r = sffs_table->t_opendir(path, &ino->i_dir);
34 }
35 else {
36 if (!read_only)
37 r = sffs_table->t_open(path, O_RDWR, 0, &ino->i_file);
38
39 /* Protection or mount status might prevent us from writing. With the
40 * information that we have available, this is the best we can do..
41 */
42 if (read_only || r != OK)
43 r = sffs_table->t_open(path, O_RDONLY, 0, &ino->i_file);
44 }
45
46 if (r != OK)
47 return r;
48
49 ino->i_flags |= I_HANDLE;
50
51 return OK;
52 }
53
54 /*===========================================================================*
55 * put_handle *
56 *===========================================================================*/
put_handle(struct inode * ino)57 void put_handle(struct inode *ino)
58 {
59 /* Close an open file or directory handle associated with an inode.
60 */
61 int r;
62
63 if (!(ino->i_flags & I_HANDLE))
64 return;
65
66 /* We ignore any errors here, because we can't deal with them anyway. */
67 if (IS_DIR(ino))
68 r = sffs_table->t_closedir(ino->i_dir);
69 else
70 r = sffs_table->t_close(ino->i_file);
71
72 if (r != OK)
73 printf("%s: put_handle: handle close operation returned %d\n",
74 sffs_name, r);
75
76 ino->i_flags &= ~I_HANDLE;
77 }
78