comparison dbutil.c @ 357:9e2ad1023978

Handle failure reading a file (such as a key file)
author Matt Johnston <matt@ucc.asn.au>
date Sun, 20 Aug 2006 12:16:13 +0000
parents e17f0333c21e
children e81d3bc1dc78
comparison
equal deleted inserted replaced
350:01e4180895ba 357:9e2ad1023978
525 /* reads the contents of filename into the buffer buf, from the current 525 /* reads the contents of filename into the buffer buf, from the current
526 * position, either to the end of the file, or the buffer being full. 526 * position, either to the end of the file, or the buffer being full.
527 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */ 527 * Returns DROPBEAR_SUCCESS or DROPBEAR_FAILURE */
528 int buf_readfile(buffer* buf, const char* filename) { 528 int buf_readfile(buffer* buf, const char* filename) {
529 529
530 int fd; 530 int fd = -1;
531 int len; 531 int len;
532 int maxlen; 532 int maxlen;
533 ret = DROPBEAR_FAILURE;
533 534
534 fd = open(filename, O_RDONLY); 535 fd = open(filename, O_RDONLY);
535 536
536 if (fd < 0) { 537 if (fd < 0) {
537 close(fd); 538 goto out;
538 return DROPBEAR_FAILURE;
539 } 539 }
540 540
541 do { 541 do {
542 maxlen = buf->size - buf->pos; 542 maxlen = buf->size - buf->pos;
543 len = read(fd, buf_getwriteptr(buf, maxlen), 543 len = read(fd, buf_getwriteptr(buf, maxlen), maxlen);
544 maxlen); 544 if (len < 0) {
545 if (errno == EINTR || errno == EAGAIN) {
546 continue;
547 }
548 goto out;
549 }
545 buf_incrwritepos(buf, len); 550 buf_incrwritepos(buf, len);
546 } while (len < maxlen && len > 0); 551 } while (len < maxlen && len > 0);
547 552
548 close(fd); 553 ret = DROPBEAR_SUCCESS;
549 return DROPBEAR_SUCCESS; 554
555 out:
556 if (fd >= 0) {
557 m_close(fd);
558 }
559 return ret;
550 } 560 }
551 561
552 /* get a line from the file into buffer in the style expected for an 562 /* get a line from the file into buffer in the style expected for an
553 * authkeys file. 563 * authkeys file.
554 * Will return DROPBEAR_SUCCESS if data is read, or DROPBEAR_FAILURE on EOF.*/ 564 * Will return DROPBEAR_SUCCESS if data is read, or DROPBEAR_FAILURE on EOF.*/