1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2001 Silicon Graphics, Inc.
14 #include <sys/types.h>
18 /*---------------------------------------------------------------------------
20 Test program used to test the DMAPI function dm_read_invis(). The
23 read_invis [-o offset] [-l length] [-s sid] [-c char] \
24 [-S storefile] {pathname|handle}
27 'offset' is the offset of the start of the write (0 is the default),
28 'length' is the length of the write in bytes (1 is the default),
29 'sid' is the session ID whose events you you are interested in.
30 'pathname' is the name of the file to be written.
31 'char' is ignored--it just allows read_invis and write_invis to have
32 interchangeable commandlines without having to fuss with the params.
34 ----------------------------------------------------------------------------*/
37 extern char *sys_errlist[];
49 fprintf(stderr, "usage:\t%s [-o offset] [-l length] "
51 "[-S storefile] {pathname|handle}\n", Progname);
61 dm_sessid_t sid = DM_NO_SESSION;
66 unsigned long long ulltemp;
74 char *storefile = NULL;
78 Progname = strrchr(argv[0], '/');
85 /* Crack and validate the command line options. */
87 while ((opt = getopt(argc, argv, "o:l:s:c:S:")) != EOF) {
90 sscanf(optarg, "%lld", &lltemp);
91 offset = (dm_off_t) lltemp;
94 sscanf(optarg, "%llu", &ulltemp);
95 length = (dm_size_t) ulltemp;
101 /* This is a no-op, it just allows read_invis
102 * and write_invis to have interchangeable
103 * commandlines, without having to fuss with
114 if (optind + 1 != argc)
116 object = argv[optind];
118 if (dm_init_service(&name) == -1) {
119 fprintf(stderr, "Can't initialize the DMAPI\n");
122 if (sid == DM_NO_SESSION)
123 find_test_session(&sid);
125 /* Get the file's handle. */
127 if (opaque_to_handle(object, &hanp, &hlen)) {
128 fprintf(stderr, "can't get handle for %s\n", object);
133 /* In case it is a realtime file, align the buffer on a
134 sufficiently big boundary.
136 if ((bufp = memalign(4096, length)) == NULL) {
137 fprintf(stderr, "malloc of %llu bytes failed\n",
138 (unsigned long long) length);
141 memset(bufp, '\0', length);
147 if ((storefd = open(storefile, O_WRONLY|O_CREAT, 0777)) == -1) {
148 fprintf(stderr, "unable to open store file for write (%s), errno = %d\n", storefile, errno);
151 lret = lseek(storefd, offset, SEEK_SET);
153 fprintf(stderr, "unable to lseek(%s) to offset %lld, errno = %d\n",
154 storefile, (long long)lret, errno);
159 rc = dm_read_invis(sid, hanp, hlen, DM_NO_TOKEN, offset, length, bufp);
162 fprintf(stderr, "dm_read_invis failed, %s\n", strerror(errno));
164 } else if (rc != length) {
165 fprintf(stderr, "dm_read_invis expected to read %llu bytes, actually "
166 "read %lld\n", (unsigned long long) length,
173 sret = write(storefd, bufp, rc);
175 fprintf(stderr, "unable to write to store file (%s), errno = %d\n", storefile, errno);
178 else if (sret != rc) {
179 fprintf(stderr, "write(%s) returned %lld, expected %lld\n",
180 storefile, (long long)sret, (long long)rc);
186 for (i = 0; i < rc; i++) {
187 if (isprint(bufp[i])) {
188 fprintf(stdout, "%c", bufp[i]);
190 fprintf(stdout, "\\%03d", bufp[i]);
194 dm_handle_free(hanp, hlen);