sparse

sparse file tool
git clone http://frotz.net/git/sparse.git
Log | Files | Refs | README

util.c (1425B)


      1 /* Copyright 2013 Brian Swetland <swetland@frotz.net>
      2  *
      3  * Licensed under the Apache License, Version 2.0 (the "License");
      4  * you may not use this file except in compliance with the License.
      5  * You may obtain a copy of the License at
      6  *
      7  *     http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software
     10  * distributed under the License is distributed on an "AS IS" BASIS,
     11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12  * See the License for the specific language governing permissions and
     13  * limitations under the License.
     14  */
     15 
     16 #include <stdio.h>
     17 #include <unistd.h>
     18 #include <fcntl.h>
     19 #include <errno.h>
     20 
     21 #include "util.h"
     22 
     23 int readx(int fd, void *_data, size_t len) {
     24 	ssize_t r;
     25 	u8 *data = _data;
     26 	while (len > 0) {
     27 		r = read(fd, data, len);
     28 		if (r < 0) {
     29 			if (errno == EINTR)
     30 				continue;
     31 			return -1;
     32 		}
     33 		len -= r;
     34 		data += r;
     35 	}
     36 	return 0;
     37 }
     38 
     39 int writex(int fd, void *_data, size_t len) {
     40 	ssize_t r;
     41 	u8 *data = _data;
     42 	while (len > 0) {
     43 		r = write(fd, data, len);
     44 		if (r < 0) {
     45 			if (errno == EINTR)
     46 				continue;
     47 			return -1;
     48 		}
     49 		len -= r;
     50 		data += r;
     51 	}
     52 	return 0;
     53 }
     54 
     55 int copyx(int fin, int fout, size_t len, u8 *buf, size_t max) {
     56 	while (len > 0) {
     57 		size_t xfer = max > len ? len : max;
     58 		if (readx(fin, buf, xfer))
     59 			return -1;
     60 		if (writex(fout, buf, xfer))
     61 			return -1;
     62 		len -= xfer;
     63 	}
     64 	return 0;
     65 }