File: wasmfs_seek.c

package info (click to toggle)
emscripten 3.1.69%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 121,872 kB
  • sloc: ansic: 636,110; cpp: 425,974; javascript: 78,401; python: 58,404; sh: 49,154; pascal: 5,237; makefile: 3,365; asm: 2,415; lisp: 1,869
file content (121 lines) | stat: -rw-r--r-- 3,636 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright 2021 The Emscripten Authors.  All rights reserved.
 * Emscripten is available under two separate licenses, the MIT license and the
 * University of Illinois/NCSA Open Source License.  Both these licenses can be
 * found in the LICENSE file.
 */

#include <assert.h>
#include <emscripten.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/uio.h>
#include <unistd.h>

// FIXME: The existing io.c file implements devices, which haven't been
// implemented in the new FS yet.
// This test simply implements the seek portion.

int main() {

  char readBuffer[256] = {0};
  char writeBuffer[] = "writeme";

  int f = open("/file", O_RDWR | O_CREAT);
  const char* msg = "1234567890";
  write(f, msg, strlen(msg));
  lseek(f, 0, SEEK_SET);

  printf("read from file: %zd\n", read(f, readBuffer, sizeof readBuffer));
  printf("data: %s\n", readBuffer);
  memset(readBuffer, 0, sizeof readBuffer);
  printf("errno: %d\n\n", errno);
  errno = 0;

  printf("pread past end of file: %zd\n",
         pread(f, readBuffer, sizeof readBuffer, 999999999));
  printf("data: %s\n", readBuffer);
  memset(readBuffer, 0, sizeof readBuffer);
  printf("errno: %d\n\n", errno);
  errno = 0;

  printf("seek: %lld\n", lseek(f, 3, SEEK_SET));
  printf("errno: %d\n\n", errno);
  printf("partial read from file: %zd\n", read(f, readBuffer, 3));
  printf("data: %s\n", readBuffer);
  memset(readBuffer, 0, sizeof readBuffer);
  printf("errno: %d\n\n", errno);
  errno = 0;

  printf("seek: %lld\n", lseek(f, -2, SEEK_END));
  printf("errno: %d\n", errno);
  errno = 0;
  printf("partial read from end of file: %zd\n", read(f, readBuffer, 3));
  printf("data: %s\n", readBuffer);
  memset(readBuffer, 0, sizeof readBuffer);
  printf("errno: %d\n\n", errno);
  errno = 0;

  printf("before bad seek: %lld\n", lseek(f, 0, SEEK_CUR));
  printf("errno: %d\n", errno);
  errno = 0;

  printf("bad seek: %lld\n", lseek(f, -15, SEEK_CUR));
  printf("errno: %d\n", errno);
  errno = 0;
  printf("after bad seek: %lld\n", lseek(f, 0, SEEK_CUR));
  printf("errno: %d\n", errno);
  errno = 0;
  printf("partial read from before start of file: %zd\n",
         read(f, readBuffer, 3));
  printf("data: %s\n", readBuffer);
  memset(readBuffer, 0, sizeof readBuffer);
  printf("errno: %d\n\n", errno);
  errno = 0;

  printf("seek: %lld\n", lseek(f, 0, SEEK_SET));
  printf("write to start of file: %zd\n", write(f, writeBuffer, 3));
  printf("errno: %d\n\n", errno);
  errno = 0;

  printf("seek: %lld\n", lseek(f, 0, SEEK_END));
  printf("write to end of file: %zd\n", write(f, writeBuffer, 3));
  printf("errno: %d\n\n", errno);
  errno = 0;

  printf("seek: %lld\n", lseek(f, 10, SEEK_END));
  printf("write after end of file: %zd\n",
         write(f, writeBuffer, sizeof writeBuffer));
  printf("errno: %d\n\n", errno);
  errno = 0;

  printf("pwrite to the middle of file: %zd\n",
         pwrite(f, writeBuffer + 2, 3, 17));
  printf("errno: %d\n", errno);
  printf("seek: %lld\n\n", lseek(f, 0, SEEK_CUR));
  errno = 0;

  printf("pwrite past end of file: %zd\n", pwrite(f, writeBuffer, 5, 32));
  printf("errno: %d\n", errno);
  printf("seek: %lld\n\n", lseek(f, 0, SEEK_CUR));
  errno = 0;

  ssize_t bytesRead;
  printf("seek: %lld\n", lseek(f, 0, SEEK_SET));
  printf("read after write: %zd\n",
         bytesRead = read(f, readBuffer, sizeof readBuffer));
  printf("errno: %d\n", errno);
  errno = 0;
  printf("final: ");
  for (ssize_t i = 0; i < bytesRead; i++) {
    if (readBuffer[i] == 0) {
      printf("\\0");
    } else {
      printf("%c", readBuffer[i]);
    }
  }
  printf("\n");
  return 0;
}