summaryrefslogtreecommitdiff
path: root/minix/lib/libsffs/write.c
blob: d18a5831add5b58b217c0a654142507c11c741cf (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* This file contains file writing system call handlers.
 *
 * The entry points into this file are:
 *   do_write		perform the WRITE file system call
 *   do_trunc		perform the TRUNC file system call
 *
 * Created:
 *   April 2009 (D.C. van Moolenbroek)
 */

#include "inc.h"

/*===========================================================================*
 *				write_file				     *
 *===========================================================================*/
static ssize_t write_file(struct inode *ino, off_t pos, size_t count,
	struct fsdriver_data *data)
{
/* Write data or zeroes to a file, depending on whether a valid pointer to
 * a data grant was provided.
 */
  size_t size, off, chunk;
  char *ptr;
  int r;

  if (pos < 0)
	return EINVAL;

  assert(!IS_DIR(ino));

  if ((r = get_handle(ino)) != OK)
	return r;

  assert(count > 0);

  /* Use the buffer from below to eliminate extra copying. */
  size = sffs_table->t_writebuf(&ptr);
  off = 0;

  while (count > 0) {
	chunk = MIN(count, size);

	if (data != NULL) {
		if ((r = fsdriver_copyin(data, off, ptr, chunk)) != OK)
			break;
	} else {
		/* Do this every time. We don't know what happens below. */
		memset(ptr, 0, chunk);
	}

	if ((r = sffs_table->t_write(ino->i_file, ptr, chunk, pos)) <= 0)
		break;

	count -= r;
	off += r;
	pos += r;
  }

  if (r < 0)
	return r;

  return off;
}

/*===========================================================================*
 *				do_write				     *
 *===========================================================================*/
ssize_t do_write(ino_t ino_nr, struct fsdriver_data *data, size_t count,
	off_t pos, int call)
{
/* Write data to a file.
 */
  struct inode *ino;

  if (read_only)
	return EROFS;

  if ((ino = find_inode(ino_nr)) == NULL)
	return EINVAL;

  if (IS_DIR(ino)) return EISDIR;

  if (count == 0) return 0;

  return write_file(ino, pos, count, data);
}

/*===========================================================================*
 *				do_trunc				     *
 *===========================================================================*/
int do_trunc(ino_t ino_nr, off_t start, off_t end)
{
/* Change file size or create file holes.
 */
  char path[PATH_MAX];
  struct inode *ino;
  struct sffs_attr attr;
  uint64_t delta;
  ssize_t r;

  if (read_only)
	return EROFS;

  if ((ino = find_inode(ino_nr)) == NULL)
	return EINVAL;

  if (IS_DIR(ino)) return EISDIR;

  if (end == 0) {
	/* Truncate or expand the file. */
	if ((r = verify_inode(ino, path, NULL)) != OK)
		return r;

	attr.a_mask = SFFS_ATTR_SIZE;
	attr.a_size = start;

	r = sffs_table->t_setattr(path, &attr);
  } else {
	/* Write zeroes to the file. We can't create holes. */
	if (end <= start) return EINVAL;

	delta = (uint64_t)end - (uint64_t)start;

	if (delta > SSIZE_MAX) return EINVAL;

	if ((r = write_file(ino, start, (size_t)delta, NULL)) >= 0)
		r = OK;
  }

  return r;
}