vm: fix a null dereference on out-of-memory
[minix.git] / lib / libsffs / handle.c
blobd4663580fbfc9908b6377d68faf3b6d549e06c59
1 /* This file contains open file and directory handle management functions.
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
7 * Created:
8 * April 2009 (D.C. van Moolenbroek)
9 */
11 #include "inc.h"
13 #include <fcntl.h>
15 /*===========================================================================*
16 * get_handle *
17 *===========================================================================*/
18 int get_handle(ino)
19 struct inode *ino;
21 /* Get an open file or directory handle for an inode.
23 char path[PATH_MAX];
24 int r;
26 /* If we don't have a file handle yet, try to open the file now. */
27 if (ino->i_flags & I_HANDLE)
28 return OK;
30 if ((r = verify_inode(ino, path, NULL)) != OK)
31 return r;
33 if (IS_DIR(ino)) {
34 r = sffs_table->t_opendir(path, &ino->i_dir);
36 else {
37 if (!state.s_read_only)
38 r = sffs_table->t_open(path, O_RDWR, 0, &ino->i_file);
40 /* Protection or mount status might prevent us from writing. With the
41 * information that we have available, this is the best we can do..
43 if (state.s_read_only || r != OK)
44 r = sffs_table->t_open(path, O_RDONLY, 0, &ino->i_file);
47 if (r != OK)
48 return r;
50 ino->i_flags |= I_HANDLE;
52 return OK;
55 /*===========================================================================*
56 * put_handle *
57 *===========================================================================*/
58 void put_handle(ino)
59 struct inode *ino;
61 /* Close an open file or directory handle associated with an inode.
63 int r;
65 if (!(ino->i_flags & I_HANDLE))
66 return;
68 /* We ignore any errors here, because we can't deal with them anyway. */
69 if (IS_DIR(ino))
70 r = sffs_table->t_closedir(ino->i_dir);
71 else
72 r = sffs_table->t_close(ino->i_file);
74 if (r != OK)
75 printf("%s: put_handle: handle close operation returned %d\n",
76 sffs_name, r);
78 ino->i_flags &= ~I_HANDLE;