summaryrefslogtreecommitdiff
path: root/minix/lib/libsffs/handle.c
blob: b346175c0de9984fa6cbd9678bc9ba54f07ece18 (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
/* This file contains open file and directory handle management functions.
 *
 * The entry points into this file are:
 *   get_handle		open a handle for an inode and store the handle
 *   put_handle		close any handles associated with an inode
 *
 * Created:
 *   April 2009 (D.C. van Moolenbroek)
 */

#include "inc.h"

#include <fcntl.h>

/*===========================================================================*
 *				get_handle				     *
 *===========================================================================*/
int get_handle(struct inode *ino)
{
/* Get an open file or directory handle for an inode.
 */
  char path[PATH_MAX];
  int r;

  /* If we don't have a file handle yet, try to open the file now. */
  if (ino->i_flags & I_HANDLE)
	return OK;

  if ((r = verify_inode(ino, path, NULL)) != OK)
	return r;

  if (IS_DIR(ino)) {
	r = sffs_table->t_opendir(path, &ino->i_dir);
  }
  else {
	if (!read_only)
		r = sffs_table->t_open(path, O_RDWR, 0, &ino->i_file);

	/* Protection or mount status might prevent us from writing. With the
	 * information that we have available, this is the best we can do..
	 */
	if (read_only || r != OK)
		r = sffs_table->t_open(path, O_RDONLY, 0, &ino->i_file);
  }

  if (r != OK)
	return r;

  ino->i_flags |= I_HANDLE;

  return OK;
}

/*===========================================================================*
 *				put_handle				     *
 *===========================================================================*/
void put_handle(struct inode *ino)
{
/* Close an open file or directory handle associated with an inode.
 */
  int r;

  if (!(ino->i_flags & I_HANDLE))
	return;

  /* We ignore any errors here, because we can't deal with them anyway. */
  if (IS_DIR(ino))
	r = sffs_table->t_closedir(ino->i_dir);
  else
	r = sffs_table->t_close(ino->i_file);

  if (r != OK)
	printf("%s: put_handle: handle close operation returned %d\n",
		sffs_name, r);

  ino->i_flags &= ~I_HANDLE;
}