Changeset 58898d1d in mainline
- Timestamp:
- 2017-03-24T20:31:54Z (8 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 8e9b2534
- Parents:
- c9e3692
- Location:
- uspace
- Files:
-
- 33 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/cmds/modules/cat/cat.c
rc9e3692 r58898d1d 31 31 #include <stdlib.h> 32 32 #include <unistd.h> 33 #include <sys/stat.h> 33 34 #include <getopt.h> 34 35 #include <str.h> … … 187 188 size_t offset = 0, copied_bytes = 0; 188 189 off64_t file_size = 0, length = 0; 190 aoff64_t pos = 0; 189 191 190 192 bool reading_stdin = dash_represents_stdin && (str_cmp(fname, "-") == 0); … … 205 207 close(fd); 206 208 printf("Unable to allocate enough memory to read %s\n", 207 209 fname); 208 210 return 1; 209 211 } 210 212 211 213 if (tail != CAT_FULL_FILE) { 212 file_size = lseek(fd, 0, SEEK_END); 214 struct stat st; 215 216 if (fstat(fd, &st) != EOK) { 217 close(fd); 218 free(buff); 219 printf("Unable to fstat %d\n", fd); 220 return 1; 221 } 222 file_size = st.size; 213 223 if (head == CAT_FULL_FILE) { 214 224 head = file_size; … … 223 233 224 234 if (tail_first) { 225 lseek(fd, (tail >= file_size) ? 0 : (file_size - tail), SEEK_SET);235 pos = (tail >= file_size) ? 0 : (file_size - tail); 226 236 } else { 227 lseek(fd, ((head - tail) >= file_size) ? 0 : (head - tail), SEEK_SET);237 pos = ((head - tail) >= file_size) ? 0 : (head - tail); 228 238 } 229 239 } else … … 243 253 } 244 254 245 bytes = read(fd, buff + copied_bytes, bytes_to_read);255 bytes = read(fd, &pos, buff + copied_bytes, bytes_to_read); 246 256 copied_bytes = 0; 247 257 -
uspace/app/bdsh/cmds/modules/cmp/cmp.c
rc9e3692 r58898d1d 79 79 char buffer[2][CMP_BUFLEN]; 80 80 ssize_t offset[2]; 81 aoff64_t pos[2] = {}; 81 82 82 83 for (int i = 0; i < 2; i++) { … … 94 95 ssize_t size; 95 96 do { 96 size = read(fd[i], buffer[i] + offset[i], 97 size = read(fd[i], &pos[i], 98 buffer[i] + offset[i], 97 99 CMP_BUFLEN - offset[i]); 98 100 if (size < 0) { -
uspace/app/bdsh/cmds/modules/cp/cp.c
rc9e3692 r58898d1d 377 377 int64_t copied = 0; 378 378 char *buff = NULL; 379 aoff64_t posr = 0, posw = 0; 380 struct stat st; 379 381 380 382 if (vb) … … 392 394 } 393 395 394 total = lseek(fd1, 0, SEEK_END); 395 396 if (fstat(fd1, &st) != EOK) { 397 printf("Unable to fstat %d\n", fd1); 398 close(fd1); 399 close(fd2); 400 return -1; 401 } 402 403 total = st.size; 396 404 if (vb) 397 405 printf("%" PRIu64 " bytes to copy\n", total); 398 399 lseek(fd1, 0, SEEK_SET);400 406 401 407 if (NULL == (buff = (char *) malloc(blen))) { … … 406 412 } 407 413 408 while ((bytes = read(fd1, buff, blen)) > 0) {409 if ((bytes = write(fd2, buff, bytes)) < 0)414 while ((bytes = read(fd1, &posr, buff, blen)) > 0) { 415 if ((bytes = write(fd2, &posw, buff, bytes)) < 0) 410 416 break; 411 417 copied += bytes; -
uspace/app/bdsh/cmds/modules/mkfile/mkfile.c
rc9e3692 r58898d1d 121 121 void *buffer; 122 122 bool create_sparse = false; 123 aoff64_t pos = 0; 123 124 124 125 file_size = 0; … … 164 165 if (create_sparse && file_size > 0) { 165 166 const char byte = 0x00; 166 167 if ((rc2 = lseek(fd, file_size - 1, SEEK_SET)) < 0) { 168 close(fd); 169 goto error; 170 } 171 172 rc2 = write(fd, &byte, sizeof(char)); 167 168 pos = file_size - 1; 169 rc2 = write(fd, &pos, &byte, sizeof(char)); 173 170 if (rc2 < 0) { 174 171 close(fd); … … 187 184 while (total_written < file_size) { 188 185 to_write = min(file_size - total_written, BUFFER_SIZE); 189 rc = write(fd, buffer, to_write);186 rc = write(fd, &pos, buffer, to_write); 190 187 if (rc <= 0) { 191 188 printf("%s: Error writing file (%d).\n", cmdname, errno); -
uspace/app/sysinst/futil.c
rc9e3692 r58898d1d 60 60 ssize_t nr, nw; 61 61 int rc; 62 aoff64_t posr = 0, posw = 0; 62 63 63 64 printf("Copy '%s' to '%s'.\n", srcp, destp); … … 72 73 73 74 do { 74 nr = read(sf, buf, BUF_SIZE);75 nr = read(sf, &posr, buf, BUF_SIZE); 75 76 if (nr == 0) 76 77 break; … … 78 79 return EIO; 79 80 80 nw = write(df, buf, nr);81 nw = write(df, &posw, buf, nr); 81 82 if (nw <= 0) 82 83 return EIO; … … 157 158 int sf; 158 159 ssize_t nr; 159 off64_t off;160 160 size_t fsize; 161 161 char *data; 162 struct stat st; 162 163 163 164 sf = open(srcp, O_RDONLY); … … 165 166 return ENOENT; 166 167 167 off = lseek(sf, 0, SEEK_END);168 if (off == (off64_t)-1)168 if (fstat(sf, &st) != EOK) { 169 close(sf); 169 170 return EIO; 171 } 170 172 171 fsize = (size_t)off; 172 173 off = lseek(sf, 0, SEEK_SET); 174 if (off == (off64_t)-1) 175 return EIO; 173 fsize = st.size; 176 174 177 175 data = calloc(fsize, 1); 178 if (data == NULL) 176 if (data == NULL) { 177 close(sf); 179 178 return ENOMEM; 179 } 180 180 181 nr = read(sf, data, fsize); 182 if (nr != (ssize_t)fsize) 181 nr = read(sf, (aoff64_t []) { 0 }, data, fsize); 182 if (nr != (ssize_t)fsize) { 183 close(sf); 184 free(data); 183 185 return EIO; 186 } 184 187 185 188 (void) close(sf); -
uspace/app/taskdump/elf_core.c
rc9e3692 r58898d1d 67 67 68 68 static off64_t align_foff_up(off64_t, uintptr_t, size_t); 69 static int align_pos(int, size_t); 70 static int write_mem_area(int, as_area_info_t *, async_sess_t *); 69 static int write_mem_area(int, aoff64_t *, as_area_info_t *, async_sess_t *); 71 70 72 71 #define BUFFER_SIZE 0x1000 … … 97 96 elf_note_t note; 98 97 size_t word_size; 98 aoff64_t pos = 0; 99 99 100 100 int fd; … … 207 207 } 208 208 209 rc = write(fd, & elf_hdr, sizeof(elf_hdr));209 rc = write(fd, &pos, &elf_hdr, sizeof(elf_hdr)); 210 210 if (rc != sizeof(elf_hdr)) { 211 211 printf("Failed writing ELF header.\n"); … … 215 215 216 216 for (i = 0; i < n_ph; ++i) { 217 rc = write(fd, &p _hdr[i], sizeof(p_hdr[i]));217 rc = write(fd, &pos, &p_hdr[i], sizeof(p_hdr[i])); 218 218 if (rc != sizeof(p_hdr[i])) { 219 219 printf("Failed writing program header.\n"); … … 223 223 } 224 224 225 if (lseek(fd, p_hdr[0].p_offset, SEEK_SET) == (off64_t) -1) { 226 printf("Failed writing memory data.\n"); 227 free(p_hdr); 228 return EIO; 229 } 225 pos = p_hdr[0].p_offset; 230 226 231 227 /* … … 236 232 note.type = NT_PRSTATUS; 237 233 238 rc = write(fd, & note, sizeof(elf_note_t));234 rc = write(fd, &pos, ¬e, sizeof(elf_note_t)); 239 235 if (rc != sizeof(elf_note_t)) { 240 236 printf("Failed writing note header.\n"); … … 243 239 } 244 240 245 rc = write(fd, "CORE", note.namesz);241 rc = write(fd, &pos, "CORE", note.namesz); 246 242 if (rc != (ssize_t) note.namesz) { 247 243 printf("Failed writing note header.\n"); … … 250 246 } 251 247 252 rc = align_pos(fd, word_size); 253 if (rc != EOK) { 254 printf("Failed writing note header.\n"); 255 free(p_hdr); 256 return EIO; 257 } 258 259 rc = write(fd, &pr_status, sizeof(elf_prstatus_t)); 248 pos = ALIGN_UP(pos, word_size); 249 250 rc = write(fd, &pos, &pr_status, sizeof(elf_prstatus_t)); 260 251 if (rc != sizeof(elf_prstatus_t)) { 261 252 printf("Failed writing register data.\n"); … … 265 256 266 257 for (i = 1; i < n_ph; ++i) { 267 if (lseek(fd, p_hdr[i].p_offset, SEEK_SET) == (off64_t) -1) { 268 printf("Failed writing memory data.\n"); 269 free(p_hdr); 270 return EIO; 271 } 272 if (write_mem_area(fd, &ainfo[i - 1], sess) != EOK) { 258 pos = p_hdr[i].p_offset; 259 if (write_mem_area(fd, &pos, &ainfo[i - 1], sess) != EOK) { 273 260 printf("Failed writing memory data.\n"); 274 261 free(p_hdr); … … 297 284 * 298 285 * @param fd File to write to. 286 * @param pos Pointer to the position to write to. 299 287 * @param area Memory area info structure. 300 288 * @param sess Debugging session. … … 303 291 * 304 292 */ 305 static int write_mem_area(int fd, as_area_info_t *area, async_sess_t *sess) 293 static int write_mem_area(int fd, aoff64_t *pos, as_area_info_t *area, 294 async_sess_t *sess) 306 295 { 307 296 size_t to_copy; … … 321 310 } 322 311 323 rc = write(fd, buffer, to_copy);312 rc = write(fd, pos, buffer, to_copy); 324 313 if (rc != (ssize_t) to_copy) { 325 314 printf("Failed writing memory contents.\n"); … … 334 323 } 335 324 336 static int align_pos(int fd, size_t align)337 {338 off64_t cur_pos;339 size_t rem, adv;340 341 cur_pos = lseek(fd, 0, SEEK_CUR);342 if (cur_pos < 0)343 return -1;344 345 rem = cur_pos % align;346 adv = align - rem;347 348 cur_pos = lseek(fd, adv, SEEK_CUR);349 if (cur_pos < 0)350 return -1;351 352 return EOK;353 }354 355 325 /** @} 356 326 */ -
uspace/app/taskdump/symtab.c
rc9e3692 r58898d1d 68 68 char *shstrt, *sec_name; 69 69 void *data; 70 aoff64_t pos = 0; 70 71 71 72 int fd; … … 88 89 } 89 90 90 rc = read(fd, & elf_hdr, sizeof(elf_header_t));91 rc = read(fd, &pos, &elf_hdr, sizeof(elf_header_t)); 91 92 if (rc != sizeof(elf_header_t)) { 92 93 printf("failed reading elf header\n"); … … 304 305 { 305 306 int rc; 306 307 rc = lseek(fd, elf_hdr->e_shoff + idx * sizeof(elf_section_header_t), 308 SEEK_SET); 309 if (rc == (off64_t) -1) 310 return EIO; 311 312 rc = read(fd, sec_hdr, sizeof(elf_section_header_t)); 307 aoff64_t pos = elf_hdr->e_shoff + idx * sizeof(elf_section_header_t); 308 309 rc = read(fd, &pos, sec_hdr, sizeof(elf_section_header_t)); 313 310 if (rc != sizeof(elf_section_header_t)) 314 311 return EIO; … … 331 328 { 332 329 ssize_t rc; 333 off64_t offs; 334 335 offs = lseek(fd, start, SEEK_SET); 336 if (offs == (off64_t) -1) { 337 printf("failed seeking chunk\n"); 338 *ptr = NULL; 339 return EIO; 340 } 330 aoff64_t pos = start; 341 331 342 332 *ptr = malloc(size); … … 346 336 } 347 337 348 rc = read(fd, *ptr, size);338 rc = read(fd, &pos, *ptr, size); 349 339 if (rc != (ssize_t) size) { 350 340 printf("failed reading chunk\n"); -
uspace/app/tester/mm/pager1.c
rc9e3692 r58898d1d 52 52 return NULL; 53 53 (void) unlink(TEST_FILE); 54 if (write(fd, text, sizeof(text)) != sizeof(text)) { 54 55 if (write(fd, (aoff64_t []) {0}, text, sizeof(text)) != sizeof(text)) { 55 56 close(fd); 56 57 return NULL; -
uspace/app/tester/vfs/vfs1.c
rc9e3692 r58898d1d 70 70 const char *test_vfs1(void) 71 71 { 72 aoff64_t pos = 0; 73 72 74 if (mkdir(TEST_DIRECTORY, 0) != 0) { 73 75 TPRINTF("rc=%d\n", errno); … … 82 84 83 85 size_t size = sizeof(text); 84 ssize_t cnt = write(fd0, text, size);86 ssize_t cnt = write(fd0, &pos, text, size); 85 87 if (cnt < 0) 86 88 return "write() failed"; 87 89 TPRINTF("Written %zd bytes\n", cnt); 88 89 if (lseek(fd0, 0, SEEK_SET) != 0) 90 return "lseek() failed"; 91 TPRINTF("Sought to position 0\n"); 90 91 pos = 0; 92 92 93 93 char buf[BUF_SIZE]; 94 94 TPRINTF("read..\n"); 95 while ((cnt = read(fd0, buf, BUF_SIZE))) {95 while ((cnt = read(fd0, &pos, buf, BUF_SIZE))) { 96 96 TPRINTF("read returns %zd\n", cnt); 97 97 if (cnt < 0) -
uspace/app/trace/trace.c
rc9e3692 r58898d1d 701 701 702 702 p = proto_new("vfs"); 703 o = oper_new("read", 1, arg_def, V_ERRNO, 1, resp_def);703 o = oper_new("read", 3, arg_def, V_ERRNO, 1, resp_def); 704 704 proto_add_oper(p, VFS_IN_READ, o); 705 o = oper_new("write", 1, arg_def, V_ERRNO, 1, resp_def);705 o = oper_new("write", 3, arg_def, V_ERRNO, 1, resp_def); 706 706 proto_add_oper(p, VFS_IN_WRITE, o); 707 o = oper_new("seek", 3, arg_def, V_ERRNO, 0, resp_def);708 proto_add_oper(p, VFS_IN_SEEK, o);709 707 o = oper_new("truncate", 5, arg_def, V_ERRNO, 0, resp_def); 710 708 proto_add_oper(p, VFS_IN_TRUNCATE, o); -
uspace/app/viewer/viewer.c
rc9e3692 r58898d1d 126 126 return false; 127 127 } 128 129 ssize_t rd = read(fd, tga, stat.size);128 129 ssize_t rd = read(fd, (aoff64_t []) {0}, tga, stat.size); 130 130 if ((rd < 0) || (rd != (ssize_t) stat.size)) { 131 131 free(tga); -
uspace/app/websrv/websrv.c
rc9e3692 r58898d1d 238 238 return rc; 239 239 240 aoff64_t pos = 0; 240 241 while (true) { 241 ssize_t nr = read(fd, fbuf, BUFFER_SIZE);242 ssize_t nr = read(fd, &pos, fbuf, BUFFER_SIZE); 242 243 if (nr == 0) 243 244 break; -
uspace/drv/bus/isa/isa.c
rc9e3692 r58898d1d 254 254 size_t len; 255 255 ssize_t r; 256 struct stat st; 256 257 257 258 fd = open(conf_path, O_RDONLY); … … 263 264 opened = true; 264 265 265 len = lseek(fd, 0, SEEK_END); 266 lseek(fd, 0, SEEK_SET); 266 if (fstat(fd, &st) != EOK) { 267 ddf_msg(LVL_ERROR, "Unable to fstat %d", fd); 268 goto cleanup; 269 } 270 271 len = st.size; 267 272 if (len == 0) { 268 273 ddf_msg(LVL_ERROR, "Configuration file '%s' is empty.", … … 277 282 } 278 283 279 r = read(fd, buf, len);284 r = read(fd, (aoff64_t []) {0}, buf, len); 280 285 if (r < 0) { 281 286 ddf_msg(LVL_ERROR, "Unable to read file '%s'.", conf_path); -
uspace/lib/bithenge/src/file.c
rc9e3692 r58898d1d 78 78 if (offset > blob->size) 79 79 return ELIMIT; 80 if (lseek(blob->fd, offset, SEEK_SET) < 0)81 return errno == EINVAL ? EIO : errno;82 80 83 ssize_t amount_read; 84 aoff64_t remaining_size = *size; 85 *size = 0; 86 do { 87 amount_read = read(blob->fd, buffer, remaining_size); 88 if (amount_read < 0) 89 return errno; 90 buffer += amount_read; 91 *size += amount_read; 92 remaining_size -= amount_read; 93 } while (remaining_size && amount_read); 81 ssize_t amount_read = read(blob->fd, &offset, buffer, *size); 82 if (amount_read < 0) 83 return errno; 84 *size += amount_read; 94 85 return EOK; 95 86 } -
uspace/lib/c/generic/elf/elf_mod.c
rc9e3692 r58898d1d 135 135 elf_header_t header_buf; 136 136 elf_header_t *header = &header_buf; 137 aoff64_t pos = 0; 137 138 int i, rc; 138 139 139 rc = read(elf->fd, header, sizeof(elf_header_t));140 rc = read(elf->fd, &pos, header, sizeof(elf_header_t)); 140 141 if (rc != sizeof(elf_header_t)) { 141 142 DPRINTF("Read error.\n"); … … 195 196 elf_segment_header_t segment_hdr; 196 197 197 /* Seek to start of segment header */ 198 lseek(elf->fd, header->e_phoff 199 + i * sizeof(elf_segment_header_t), SEEK_SET); 200 201 rc = read(elf->fd, &segment_hdr, 198 pos = header->e_phoff + i * sizeof(elf_segment_header_t); 199 rc = read(elf->fd, &pos, &segment_hdr, 202 200 sizeof(elf_segment_header_t)); 203 201 if (rc != sizeof(elf_segment_header_t)) { … … 217 215 elf_section_header_t section_hdr; 218 216 219 /* Seek to start of section header */ 220 lseek(elf->fd, header->e_shoff 221 + i * sizeof(elf_section_header_t), SEEK_SET); 222 223 rc = read(elf->fd, §ion_hdr, 217 pos = header->e_shoff + i * sizeof(elf_section_header_t); 218 rc = read(elf->fd, &pos, §ion_hdr, 224 219 sizeof(elf_section_header_t)); 225 220 if (rc != sizeof(elf_section_header_t)) { … … 333 328 uintptr_t seg_addr; 334 329 size_t mem_sz; 330 aoff64_t pos; 335 331 ssize_t rc; 336 332 … … 390 386 * Load segment data 391 387 */ 392 rc = lseek(elf->fd, entry->p_offset, SEEK_SET); 393 if (rc < 0) { 394 printf("seek error\n"); 395 return EE_INVALID; 396 } 397 398 rc = read(elf->fd, seg_ptr, entry->p_filesz); 388 pos = entry->p_offset; 389 rc = read(elf->fd, &pos, seg_ptr, entry->p_filesz); 399 390 if (rc < 0) { 400 391 DPRINTF("read error\n"); -
uspace/lib/c/generic/io/io.c
rc9e3692 r58898d1d 41 41 #include <stdbool.h> 42 42 #include <malloc.h> 43 #include <sys/stat.h> 43 44 #include <async.h> 44 45 #include <io/kio.h> … … 56 57 static FILE stdin_null = { 57 58 .fd = -1, 59 .pos = 0, 58 60 .error = true, 59 61 .eof = true, … … 70 72 static FILE stdout_kio = { 71 73 .fd = -1, 74 .pos = 0, 72 75 .error = false, 73 76 .eof = false, … … 84 87 static FILE stderr_kio = { 85 88 .fd = -1, 89 .pos = 0, 86 90 .error = false, 87 91 .eof = false, … … 288 292 } 289 293 294 stream->pos = 0; 290 295 stream->error = false; 291 296 stream->eof = false; … … 311 316 312 317 stream->fd = fd; 318 stream->pos = 0; 313 319 stream->error = false; 314 320 stream->eof = false; … … 396 402 static size_t _fread(void *buf, size_t size, size_t nmemb, FILE *stream) 397 403 { 398 size_t left, done;399 400 404 if (size == 0 || nmemb == 0) 401 405 return 0; 402 406 403 left = size * nmemb; 404 done = 0; 405 406 while ((left > 0) && (!stream->error) && (!stream->eof)) { 407 ssize_t rd = read(stream->fd, buf + done, left); 408 409 if (rd < 0) { 410 /* errno was set by read() */ 411 stream->error = true; 412 } else if (rd == 0) { 413 stream->eof = true; 414 } else { 415 left -= rd; 416 done += rd; 417 } 418 } 419 420 return (done / size); 407 ssize_t rd = read(stream->fd, &stream->pos, buf, size * nmemb); 408 if (rd < 0) { 409 /* errno was set by read() */ 410 stream->error = true; 411 rd = 0; 412 } else if (rd == 0) { 413 stream->eof = true; 414 } 415 416 return (rd / size); 421 417 } 422 418 … … 433 429 static size_t _fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream) 434 430 { 435 size_t left;436 size_t done;437 int rc;438 439 431 if (size == 0 || nmemb == 0) 440 432 return 0; 441 433 442 left = size * nmemb; 443 done = 0; 444 445 while ((left > 0) && (!stream->error)) { 446 ssize_t wr; 447 size_t uwr; 448 449 if (stream->kio) { 450 uwr = 0; 451 rc = kio_write(buf + done, left, &uwr); 452 if (rc != EOK) 453 errno = rc; 434 ssize_t wr; 435 if (stream->kio) { 436 size_t nwritten; 437 wr = kio_write(buf, size * nmemb, &nwritten); 438 if (wr != EOK) { 439 stream->error = true; 440 wr = 0; 454 441 } else { 455 wr = write(stream->fd, buf + done, left); 456 if (wr >= 0) { 457 uwr = (size_t)wr; 458 rc = EOK; 459 } else { 460 /* errno was set by write */ 461 uwr = 0; 462 rc = errno; 463 } 464 } 465 466 if (rc != EOK) { 467 /* errno was set above */ 442 wr = nwritten; 443 } 444 } else { 445 wr = write(stream->fd, &stream->pos, buf, size * nmemb); 446 if (wr < 0) { 447 /* errno was set by write() */ 468 448 stream->error = true; 469 } else { 470 left -= uwr; 471 done += uwr; 472 } 473 } 474 475 if (done > 0) 449 wr = 0; 450 } 451 } 452 453 if (wr > 0) 476 454 stream->need_sync = true; 477 455 478 return ( done/ size);456 return (wr / size); 479 457 } 480 458 … … 489 467 stream->buf_head = stream->buf_tail = stream->buf; 490 468 491 rc = read(stream->fd, stream->buf, stream->buf_size);469 rc = read(stream->fd, &stream->pos, stream->buf, stream->buf_size); 492 470 if (rc < 0) { 493 471 /* errno was set by read() */ … … 516 494 517 495 /* If buffer has prefetched read data, we need to seek back. */ 518 if (bytes_used > 0 && stream->buf_state == _bs_read) { 519 off64_t rc; 520 rc = lseek(stream->fd, - (ssize_t) bytes_used, SEEK_CUR); 521 if (rc == (off64_t)-1) { 522 /* errno was set by lseek */ 523 stream->error = 1; 524 return; 525 } 526 } 496 if (bytes_used > 0 && stream->buf_state == _bs_read) 497 stream->pos -= bytes_used; 527 498 528 499 /* If buffer has unwritten data, we need to write them out. */ … … 796 767 int fseek(FILE *stream, off64_t offset, int whence) 797 768 { 798 off64_t rc;799 800 769 if (stream->error) 801 return EOF;770 return -1; 802 771 803 772 _fflushbuf(stream); 804 773 if (stream->error) { 805 774 /* errno was set by _fflushbuf() */ 806 return EOF;775 return -1; 807 776 } 808 777 809 778 stream->ungetc_chars = 0; 810 779 811 rc = lseek(stream->fd, offset, whence); 812 if (rc == (off64_t) (-1)) { 813 /* errno has been set by lseek() */ 814 return EOF; 780 struct stat st; 781 switch (whence) { 782 case SEEK_SET: 783 stream->pos = offset; 784 break; 785 case SEEK_CUR: 786 stream->pos += offset; 787 break; 788 case SEEK_END: 789 if (fstat(stream->fd, &st) != EOK) { 790 /* errno was set by fstat() */ 791 stream->error = true; 792 return -1; 793 } 794 stream->pos = st.size + offset; 795 break; 815 796 } 816 797 … … 821 802 off64_t ftell(FILE *stream) 822 803 { 823 off64_t pos;824 825 804 if (stream->error) 826 805 return EOF; … … 832 811 } 833 812 834 pos = lseek(stream->fd, 0, SEEK_CUR); 835 if (pos == (off64_t) -1) { 836 /* errno was set by lseek */ 837 return (off64_t) -1; 838 } 839 840 return pos - stream->ungetc_chars; 813 return stream->pos - stream->ungetc_chars; 841 814 } 842 815 -
uspace/lib/c/generic/private/stdio.h
rc9e3692 r58898d1d 49 49 /** Underlying file descriptor. */ 50 50 int fd; 51 52 /** File position. */ 53 aoff64_t pos; 51 54 52 55 /** Error indicator. */ -
uspace/lib/c/generic/vfs/vfs.c
rc9e3692 r58898d1d 444 444 * 445 445 * @param fildes File descriptor 446 * @param pos Position to read from 446 447 * @param buf Buffer 447 448 * @param nbyte Maximum number of bytes to read … … 450 451 * @return EOK on success, non-zero error code on error. 451 452 */ 452 static int _read_short(int fildes, void *buf, size_t nbyte, ssize_t *nread) 453 static int _read_short(int fildes, aoff64_t pos, void *buf, size_t nbyte, 454 ssize_t *nread) 453 455 { 454 456 sysarg_t rc; … … 461 463 async_exch_t *exch = vfs_exchange_begin(); 462 464 463 req = async_send_1(exch, VFS_IN_READ, fildes, &answer); 465 req = async_send_3(exch, VFS_IN_READ, fildes, LOWER32(pos), 466 UPPER32(pos), &answer); 464 467 rc = async_data_read_start(exch, (void *) buf, nbyte); 465 468 … … 484 487 * 485 488 * @param fildes File descriptor 489 * @param pos Position to write to 486 490 * @param buf Buffer 487 491 * @param nbyte Maximum number of bytes to write … … 490 494 * @return EOK on success, non-zero error code on error. 491 495 */ 492 static int _write_short(int fildes, const void *buf, size_t nbyte,496 static int _write_short(int fildes, aoff64_t pos, const void *buf, size_t nbyte, 493 497 ssize_t *nwritten) 494 498 { … … 502 506 async_exch_t *exch = vfs_exchange_begin(); 503 507 504 req = async_send_1(exch, VFS_IN_WRITE, fildes, &answer); 508 req = async_send_3(exch, VFS_IN_WRITE, fildes, LOWER32(pos), 509 UPPER32(pos), &answer); 505 510 rc = async_data_write_start(exch, (void *) buf, nbyte); 506 511 … … 525 530 * 526 531 * @param fildes File descriptor 532 * @param pos Pointer to position to read from 527 533 * @param buf Buffer, @a nbytes bytes long 528 534 * @param nbytes Number of bytes to read … … 531 537 * On failure, -1 and sets errno. 532 538 */ 533 ssize_t read(int fildes, void *buf, size_t nbyte)539 ssize_t read(int fildes, aoff64_t *pos, void *buf, size_t nbyte) 534 540 { 535 541 ssize_t cnt = 0; … … 541 547 bp += cnt; 542 548 nread += cnt; 543 rc = _read_short(fildes, bp, nbyte - nread, &cnt); 549 *pos += cnt; 550 rc = _read_short(fildes, *pos, bp, nbyte - nread, &cnt); 544 551 } while (rc == EOK && cnt > 0 && (nbyte - nread - cnt) > 0); 545 552 … … 549 556 } 550 557 558 *pos += cnt; 551 559 return nread + cnt; 552 560 } … … 557 565 * 558 566 * @param fildes File descriptor 567 * @param pos Pointer to position to write to 559 568 * @param buf Data, @a nbytes bytes long 560 569 * @param nbytes Number of bytes to write … … 563 572 * On failure, -1 and sets errno. 564 573 */ 565 ssize_t write(int fildes, const void *buf, size_t nbyte)574 ssize_t write(int fildes, aoff64_t *pos, const void *buf, size_t nbyte) 566 575 { 567 576 ssize_t cnt = 0; … … 573 582 bp += cnt; 574 583 nwritten += cnt; 575 rc = _write_short(fildes, bp, nbyte - nwritten, &cnt); 584 *pos += cnt; 585 rc = _write_short(fildes, *pos, bp, nbyte - nwritten, &cnt); 576 586 } while (rc == EOK && ((ssize_t )nbyte - nwritten - cnt) > 0); 577 587 … … 581 591 } 582 592 593 *pos += cnt; 583 594 return nbyte; 584 595 } … … 601 612 602 613 return 0; 603 }604 605 /** Seek to a position.606 *607 * @param fildes File descriptor608 * @param offset Offset609 * @param whence SEEK_SET, SEEK_CUR or SEEK_END610 *611 * @return On success the nonnegative offset from start of file. On error612 * returns (off64_t)-1 and sets errno.613 */614 off64_t lseek(int fildes, off64_t offset, int whence)615 {616 async_exch_t *exch = vfs_exchange_begin();617 618 sysarg_t newoff_lo;619 sysarg_t newoff_hi;620 sysarg_t rc = async_req_4_2(exch, VFS_IN_SEEK, fildes,621 LOWER32(offset), UPPER32(offset), whence,622 &newoff_lo, &newoff_hi);623 624 vfs_exchange_end(exch);625 626 if (rc != EOK) {627 errno = rc;628 return (off64_t) -1;629 }630 631 return (off64_t) MERGE_LOUP32(newoff_lo, newoff_hi);632 614 } 633 615 … … 757 739 758 740 dirp->fd = fd; 741 dirp->pos = 0; 759 742 return dirp; 760 743 } … … 771 754 ssize_t len; 772 755 773 rc = _read_short(dirp->fd, &dirp->res.d_name[0], NAME_MAX + 1, &len); 756 rc = _read_short(dirp->fd, dirp->pos, &dirp->res.d_name[0], 757 NAME_MAX + 1, &len); 774 758 if (rc != EOK) { 775 759 errno = rc; … … 777 761 } 778 762 779 (void) len; 763 dirp->pos += len; 764 780 765 return &dirp->res; 781 766 } … … 787 772 void rewinddir(DIR *dirp) 788 773 { 789 (void) lseek(dirp->fd, 0, SEEK_SET);774 dirp->pos = 0; 790 775 } 791 776 -
uspace/lib/c/include/dirent.h
rc9e3692 r58898d1d 38 38 #define NAME_MAX 256 39 39 40 #include <sys/types.h> 41 40 42 struct dirent { 41 43 char d_name[NAME_MAX + 1]; … … 45 47 int fd; 46 48 struct dirent res; 49 aoff64_t pos; 47 50 } DIR; 48 51 -
uspace/lib/c/include/ipc/vfs.h
rc9e3692 r58898d1d 65 65 VFS_IN_READ = IPC_FIRST_USER_METHOD, 66 66 VFS_IN_WRITE, 67 VFS_IN_SEEK,68 67 VFS_IN_TRUNCATE, 69 68 VFS_IN_FSTAT, -
uspace/lib/c/include/stdio.h
rc9e3692 r58898d1d 54 54 (void) __SYSCALL3(SYS_KIO, KIO_WRITE, (sysarg_t) _buf, str_size(_buf)); \ 55 55 } 56 57 #ifndef SEEK_SET58 #define SEEK_SET 059 #endif60 61 #ifndef SEEK_CUR62 #define SEEK_CUR 163 #endif64 65 #ifndef SEEK_END66 #define SEEK_END 267 #endif68 56 69 57 enum _buffer_type { -
uspace/lib/c/include/unistd.h
rc9e3692 r58898d1d 60 60 extern int dup2(int, int); 61 61 62 extern ssize_t write(int, const void *, size_t);63 extern ssize_t read(int, void *, size_t);62 extern ssize_t write(int, aoff64_t *, const void *, size_t); 63 extern ssize_t read(int, aoff64_t *, void *, size_t); 64 64 65 extern off64_t lseek(int, off64_t, int);66 65 extern int ftruncate(int, aoff64_t); 67 66 -
uspace/lib/c/include/vfs/vfs.h
rc9e3692 r58898d1d 43 43 #include <async.h> 44 44 45 #define MAX_OPEN_FILES 128 46 45 47 enum vfs_change_state_type { 46 48 VFS_PASS_HANDLE -
uspace/lib/pcut/src/os/helenos.c
rc9e3692 r58898d1d 214 214 fibril_mutex_unlock(&forced_termination_mutex); 215 215 216 read(tempfile, extra_output_buffer, OUTPUT_BUFFER_SIZE); 216 aoff64_t pos = 0; 217 read(tempfile, &pos, extra_output_buffer, OUTPUT_BUFFER_SIZE); 217 218 218 219 leave_close_tempfile: -
uspace/lib/posix/source/internal/common.h
rc9e3692 r58898d1d 38 38 #include <stdio.h> 39 39 #include <stdlib.h> 40 #include <sys/types.h> 41 #include <vfs/vfs.h> 40 42 41 43 #define not_implemented() do { \ … … 57 59 }) 58 60 61 extern aoff64_t posix_pos[MAX_OPEN_FILES]; 62 59 63 #endif /* LIBPOSIX_COMMON_H_ */ 60 64 -
uspace/lib/posix/source/stdio.c
rc9e3692 r58898d1d 344 344 static int _dprintf_str_write(const char *str, size_t size, void *fd) 345 345 { 346 ssize_t wr = write(*(int *) fd, str, size); 346 const int fildes = *(int *) fd; 347 ssize_t wr = write(fildes, &posix_pos[fildes], str, size); 347 348 if (wr < 0) 348 349 return errno; … … 371 372 } 372 373 373 if (write(*(int *) fd, buf, sz) != (ssize_t) sz) { 374 const int fildes = *(int *) fd; 375 if (write(fildes, &posix_pos[fildes], buf, sz) != (ssize_t) sz) 374 376 break; 375 }376 377 377 378 chars++; -
uspace/lib/posix/source/unistd.c
rc9e3692 r58898d1d 47 47 #include "libc/stats.h" 48 48 #include "libc/malloc.h" 49 #include "libc/vfs/vfs.h" 50 #include "libc/sys/stat.h" 51 52 aoff64_t posix_pos[MAX_OPEN_FILES]; 49 53 50 54 /* Array of environment variable strings (NAME=VALUE). */ … … 175 179 int posix_close(int fildes) 176 180 { 181 posix_pos[fildes] = 0; 177 182 return negerrno(close, fildes); 178 183 } … … 188 193 ssize_t posix_read(int fildes, void *buf, size_t nbyte) 189 194 { 190 return negerrno(read, fildes, buf, nbyte);195 return negerrno(read, fildes, &posix_pos[fildes], buf, nbyte); 191 196 } 192 197 … … 201 206 ssize_t posix_write(int fildes, const void *buf, size_t nbyte) 202 207 { 203 return negerrno(write, fildes, buf, nbyte);208 return negerrno(write, fildes, &posix_pos[fildes], buf, nbyte); 204 209 } 205 210 … … 215 220 posix_off_t posix_lseek(int fildes, posix_off_t offset, int whence) 216 221 { 217 return negerrno(lseek, fildes, offset, whence); 222 struct stat st; 223 224 switch (whence) { 225 case SEEK_SET: 226 posix_pos[fildes] = offset; 227 break; 228 case SEEK_CUR: 229 posix_pos[fildes] += offset; 230 break; 231 case SEEK_END: 232 if (fstat(fildes, &st) != EOK) { 233 errno = -errno; 234 return -1; 235 } 236 posix_pos[fildes] = st.size + offset; 237 break; 238 } 239 return posix_pos[fildes]; 218 240 } 219 241 -
uspace/srv/devman/match.c
rc9e3692 r58898d1d 201 201 int fd; 202 202 size_t len = 0; 203 struct stat st; 203 204 204 205 fd = open(conf_path, O_RDONLY); … … 210 211 opened = true; 211 212 212 len = lseek(fd, 0, SEEK_END); 213 lseek(fd, 0, SEEK_SET); 213 if (fstat(fd, &st) != EOK) { 214 log_msg(LOG_DEFAULT, LVL_ERROR, "Unable to fstat %d: %s.", fd, 215 str_error(errno)); 216 goto cleanup; 217 } 218 len = st.size; 214 219 if (len == 0) { 215 220 log_msg(LOG_DEFAULT, LVL_ERROR, "Configuration file '%s' is empty.", … … 225 230 } 226 231 227 ssize_t read_bytes = read(fd, buf, len);232 ssize_t read_bytes = read(fd, (aoff64_t []) {0}, buf, len); 228 233 if (read_bytes <= 0) { 229 234 log_msg(LOG_DEFAULT, LVL_ERROR, "Unable to read file '%s' (%d).", conf_path, -
uspace/srv/vfs/vfs.h
rc9e3692 r58898d1d 142 142 /** Append on write. */ 143 143 bool append; 144 145 /** Current absolute position in the file. */146 int64_t pos;147 144 } vfs_file_t; 148 145 … … 190 187 extern int64_t vfs_node_get_size(vfs_node_t *node); 191 188 extern bool vfs_node_has_children(vfs_node_t *node); 192 193 #define MAX_OPEN_FILES 128194 189 195 190 extern void *vfs_client_data_create(void); … … 216 211 extern int vfs_op_mtab_get(void); 217 212 extern int vfs_op_open2(int fd, int flags); 218 extern int vfs_op_read(int fd, size_t *out_bytes);213 extern int vfs_op_read(int fd, aoff64_t, size_t *out_bytes); 219 214 extern int vfs_op_rename(int basefd, char *old, char *new); 220 extern int vfs_op_seek(int fd, int64_t offset, int whence, int64_t *out_offset);221 215 extern int vfs_op_statfs(int fd); 222 216 extern int vfs_op_sync(int fd); … … 226 220 extern int vfs_op_wait_handle(bool high_fd); 227 221 extern int vfs_op_walk(int parentfd, int flags, char *path, int *out_fd); 228 extern int vfs_op_write(int fd, size_t *out_bytes);222 extern int vfs_op_write(int fd, aoff64_t, size_t *out_bytes); 229 223 230 224 extern void vfs_register(ipc_callid_t, ipc_call_t *); … … 237 231 } rdwr_io_chunk_t; 238 232 239 extern int vfs_rdwr_internal(int, bool, rdwr_io_chunk_t *);233 extern int vfs_rdwr_internal(int, aoff64_t, bool, rdwr_io_chunk_t *); 240 234 241 235 extern void vfs_connection(ipc_callid_t iid, ipc_call_t *icall, void *arg); -
uspace/srv/vfs/vfs_file.c
rc9e3692 r58898d1d 45 45 #include <adt/list.h> 46 46 #include <task.h> 47 #include <vfs/vfs.h> 47 48 #include "vfs.h" 48 49 -
uspace/srv/vfs/vfs_ipc.c
rc9e3692 r58898d1d 122 122 { 123 123 int fd = IPC_GET_ARG1(*request); 124 aoff64_t pos = MERGE_LOUP32(IPC_GET_ARG2(*request), 125 IPC_GET_ARG3(*request)); 124 126 125 127 size_t bytes = 0; 126 int rc = vfs_op_read(fd, &bytes);128 int rc = vfs_op_read(fd, pos, &bytes); 127 129 async_answer_1(rid, rc, bytes); 128 130 } … … 170 172 if (new) 171 173 free(new); 172 }173 174 static void vfs_in_seek(ipc_callid_t rid, ipc_call_t *request)175 {176 int fd = (int) IPC_GET_ARG1(*request);177 int64_t off = (int64_t) MERGE_LOUP32(IPC_GET_ARG2(*request), IPC_GET_ARG3(*request));178 int whence = (int) IPC_GET_ARG4(*request);179 180 int64_t new_offset = 0;181 int rc = vfs_op_seek(fd, off, whence, &new_offset);182 async_answer_2(rid, rc, LOWER32(new_offset), UPPER32(new_offset));183 174 } 184 175 … … 256 247 { 257 248 int fd = IPC_GET_ARG1(*request); 249 aoff64_t pos = MERGE_LOUP32(IPC_GET_ARG2(*request), 250 IPC_GET_ARG3(*request)); 258 251 259 252 size_t bytes = 0; 260 int rc = vfs_op_write(fd, &bytes);253 int rc = vfs_op_write(fd, pos, &bytes); 261 254 async_answer_1(rid, rc, bytes); 262 255 } … … 308 301 vfs_in_rename(callid, &call); 309 302 break; 310 case VFS_IN_SEEK:311 vfs_in_seek(callid, &call);312 break;313 303 case VFS_IN_STATFS: 314 304 vfs_in_statfs(callid, &call); -
uspace/srv/vfs/vfs_ops.c
rc9e3692 r58898d1d 343 343 } 344 344 345 typedef int (* rdwr_ipc_cb_t)(async_exch_t *, vfs_file_t *, ipc_call_t *,346 bool, void *);347 348 static int rdwr_ipc_client(async_exch_t *exch, vfs_file_t *file, 345 typedef int (* rdwr_ipc_cb_t)(async_exch_t *, vfs_file_t *, aoff64_t, 346 ipc_call_t *, bool, void *); 347 348 static int rdwr_ipc_client(async_exch_t *exch, vfs_file_t *file, aoff64_t pos, 349 349 ipc_call_t *answer, bool read, void *data) 350 350 { … … 363 363 rc = async_data_read_forward_4_1(exch, VFS_OUT_READ, 364 364 file->node->service_id, file->node->index, 365 LOWER32( file->pos), UPPER32(file->pos), answer);365 LOWER32(pos), UPPER32(pos), answer); 366 366 } else { 367 367 rc = async_data_write_forward_4_1(exch, VFS_OUT_WRITE, 368 368 file->node->service_id, file->node->index, 369 LOWER32( file->pos), UPPER32(file->pos), answer);369 LOWER32(pos), UPPER32(pos), answer); 370 370 } 371 371 … … 374 374 } 375 375 376 static int rdwr_ipc_internal(async_exch_t *exch, vfs_file_t *file, 376 static int rdwr_ipc_internal(async_exch_t *exch, vfs_file_t *file, aoff64_t pos, 377 377 ipc_call_t *answer, bool read, void *data) 378 378 { … … 383 383 384 384 aid_t msg = async_send_fast(exch, read ? VFS_OUT_READ : VFS_OUT_WRITE, 385 file->node->service_id, file->node->index, LOWER32( file->pos),386 UPPER32( file->pos), answer);385 file->node->service_id, file->node->index, LOWER32(pos), 386 UPPER32(pos), answer); 387 387 if (msg == 0) 388 388 return EINVAL; … … 402 402 } 403 403 404 static int vfs_rdwr(int fd, bool read, rdwr_ipc_cb_t ipc_cb, void *ipc_cb_data) 404 static int vfs_rdwr(int fd, aoff64_t pos, bool read, rdwr_ipc_cb_t ipc_cb, 405 void *ipc_cb_data) 405 406 { 406 407 /* … … 463 464 async_exch_t *fs_exch = vfs_exchange_grab(file->node->fs_handle); 464 465 465 if (!read && file->append) 466 file->pos = file->node->size; 466 if (!read && file->append) { 467 if (file->node->size == -1) 468 file->node->size = vfs_node_get_size(file->node); 469 pos = file->node->size; 470 } 467 471 468 472 /* … … 470 474 */ 471 475 ipc_call_t answer; 472 int rc = ipc_cb(fs_exch, file, &answer, read, ipc_cb_data);476 int rc = ipc_cb(fs_exch, file, pos, &answer, read, ipc_cb_data); 473 477 474 478 vfs_exchange_release(fs_exch); 475 476 size_t bytes = IPC_GET_ARG1(answer);477 479 478 480 if (file->node->type == VFS_NODE_DIRECTORY) … … 491 493 } 492 494 493 /* Update the position pointer and unlock the open file. */494 if (rc == EOK)495 file->pos += bytes;496 495 vfs_file_put(file); 497 496 … … 499 498 } 500 499 501 int vfs_rdwr_internal(int fd, bool read, rdwr_io_chunk_t *chunk)502 { 503 return vfs_rdwr(fd, read, rdwr_ipc_internal, chunk);504 } 505 506 int vfs_op_read(int fd, size_t *out_bytes)507 { 508 return vfs_rdwr(fd, true, rdwr_ipc_client, out_bytes);500 int vfs_rdwr_internal(int fd, aoff64_t pos, bool read, rdwr_io_chunk_t *chunk) 501 { 502 return vfs_rdwr(fd, pos, read, rdwr_ipc_internal, chunk); 503 } 504 505 int vfs_op_read(int fd, aoff64_t pos, size_t *out_bytes) 506 { 507 return vfs_rdwr(fd, pos, true, rdwr_ipc_client, out_bytes); 509 508 } 510 509 … … 608 607 fibril_rwlock_write_unlock(&namespace_rwlock); 609 608 return EOK; 610 }611 612 int vfs_op_seek(int fd, int64_t offset, int whence, int64_t *out_offset)613 {614 vfs_file_t *file = vfs_file_get(fd);615 if (!file)616 return EBADF;617 618 switch (whence) {619 case SEEK_SET:620 if (offset < 0) {621 vfs_file_put(file);622 return EINVAL;623 }624 file->pos = offset;625 *out_offset = offset;626 vfs_file_put(file);627 return EOK;628 case SEEK_CUR:629 if (offset > 0 && file->pos > (INT64_MAX - offset)) {630 vfs_file_put(file);631 return EOVERFLOW;632 }633 634 if (offset < 0 && -file->pos > offset) {635 vfs_file_put(file);636 return EOVERFLOW;637 }638 639 file->pos += offset;640 *out_offset = file->pos;641 vfs_file_put(file);642 return EOK;643 case SEEK_END:644 fibril_rwlock_read_lock(&file->node->contents_rwlock);645 int64_t size = vfs_node_get_size(file->node);646 fibril_rwlock_read_unlock(&file->node->contents_rwlock);647 648 if (offset > 0 && size > (INT64_MAX - offset)) {649 vfs_file_put(file);650 return EOVERFLOW;651 }652 653 if (offset < 0 && -size > offset) {654 vfs_file_put(file);655 return EOVERFLOW;656 }657 658 file->pos = size + offset;659 *out_offset = file->pos;660 vfs_file_put(file);661 return EOK;662 }663 664 vfs_file_put(file);665 return EINVAL;666 609 } 667 610 … … 953 896 } 954 897 955 int vfs_op_write(int fd, size_t *out_bytes)956 { 957 return vfs_rdwr(fd, false, rdwr_ipc_client, out_bytes);898 int vfs_op_write(int fd, aoff64_t pos, size_t *out_bytes) 899 { 900 return vfs_rdwr(fd, pos, false, rdwr_ipc_client, out_bytes); 958 901 } 959 902 -
uspace/srv/vfs/vfs_pager.c
rc9e3692 r58898d1d 50 50 int rc; 51 51 52 vfs_file_t *file = vfs_file_get(fd);53 if (!file) {54 async_answer_0(rid, ENOENT);55 return;56 }57 58 52 page = as_area_create(AS_AREA_ANY, page_size, 59 53 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE, … … 61 55 62 56 if (page == AS_MAP_FAILED) { 63 vfs_file_put(file);64 57 async_answer_0(rid, ENOMEM); 65 58 return; … … 71 64 }; 72 65 73 file->pos = offset;74 vfs_file_put(file);75 76 66 size_t total = 0; 67 aoff64_t pos = offset; 77 68 do { 78 rc = vfs_rdwr_internal(fd, true, &chunk);69 rc = vfs_rdwr_internal(fd, pos, true, &chunk); 79 70 if (rc != EOK) 80 71 break; … … 82 73 break; 83 74 total += chunk.size; 75 pos += chunk.size; 84 76 chunk.buffer += chunk.size; 85 77 chunk.size = page_size - total;
Note:
See TracChangeset
for help on using the changeset viewer.