File: io.c

package info (click to toggle)
i2util 1.6-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 748 kB
  • ctags: 473
  • sloc: ansic: 5,290; perl: 1,151; makefile: 74; sh: 17
file content (296 lines) | stat: -rw-r--r-- 6,256 bytes parent folder | download
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
**      $Id$
*/
/************************************************************************
*									*
*			     Copyright (C)  2002			*
*				Internet2				*
*			     All Rights Reserved			*
*									*
************************************************************************/
/*
**	File:		io.c
**
**	Author:		Anatoly Karp
**                      Jeff W. Boote
**
**	Date:		Wed Apr  24 10:42:12  2002
**
**	Description:	This file contains the private functions to
**			to facilitate IO that the library needs to do.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
*/
#include <I2util/utilP.h>

#include <sys/types.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <string.h>

/*
 * Function:	I2Readni
 *
 * Description:	
 * 	Read n bytes from the given fd. If !*retn_on_intr - then the function
 * 	will continue to read even through interupts. (This is set to a
 * 	pointer value so it can be modified via a signal handler. So, you
 * 	can make this function return based on signals you care about.)
 *
 * 	If this function returns due to an interrupt - *retn_on_intr will
 * 	be non-zero (although this function won't modify the value). And,
 * 	this function will return -1. errno will also be set to EINTR.
 *
 * 	This function will return -1 for all other error conditions
 * 	as well. (and errno will be set)
 *
 * 	Short reads returned indicate the fd has reached EOF. (i.e. the
 * 	socket is closed if fd is attached to a socket)
 *
 * In Args:	
 *
 * Out Args:	
 *
 * Scope:	
 * Returns:	
 * Side Effect:	
 */
ssize_t
I2Readni(
	int	fd,
	void	*vptr,
	size_t	n,
	int	*retn_on_intr	/* pointer so it can be modified via signals */
	)
{
	size_t	nleft;
	ssize_t	nread;
	char	*ptr;

	ptr = vptr;
	nleft = n;
	while(nleft > 0){
		if((nread = read(fd, ptr, nleft)) < 0){
			if((errno == EINTR) && !*retn_on_intr){
				/* call read again */
				nread = 0;
			}
			else{
				return(-1);
			}
		} else if(nread == 0){
			/* EOF */
			break;
		}

		nleft -= nread;
		ptr   += nread;
	}
	return(n - nleft);		/* return >= 0 */
}

/*
 * Function:	I2Readn
 *
 * Description:	
 * 	Wrapper function for I2Readni if you really don't ever want to
 * 	return early from read due to an interrupt.
 *
 * In Args:	
 *
 * Out Args:	
 *
 * Scope:	
 * Returns:	
 * Side Effect:	
 */
ssize_t
I2Readn(
	int	fd,
	void	*vptr,
	size_t	n
	)
{
	int	intr=0;

	return I2Readni(fd,vptr,n,&intr);
}

/*
 * Function:	I2Writeni
 *
 * Description:	
 * 	Write n bytes to the given fd. If !*retn_on_intr - then the function
 * 	will continue to write even through interupts. (This is set to a
 * 	pointer value so it can be modified via a signal handler. So, you
 * 	can make this function return based on signals you care about.)
 *
 * 	If this function returns due to an interrupt - *retn_on_intr will
 * 	be non-zero (although this function won't modify the value). And,
 * 	this function will return -1. errno will also be set to EINTR.
 *
 * 	This function will return -1 for all other error conditions
 * 	as well. (and errno will be set)
 *
 * 	Short reads returned indicate the fd has reached EOF. (i.e. the
 * 	socket is closed if fd is attached to a socket)
 *
 *
 * In Args:	
 *
 * Out Args:	
 *
 * Scope:	
 * Returns:	
 * Side Effect:	
 */
ssize_t
I2Writeni(
	int		fd,
	const void	*vptr,
	size_t		n,
	int		*retn_on_intr
	)
{
	size_t		nleft;
	ssize_t		nwritten;
	const char	*ptr;

	ptr = vptr;
	nleft = n;
	while(nleft > 0){
		if((nwritten = write(fd, ptr, nleft)) <= 0){
			if((errno == EINTR) && !*retn_on_intr){
				/* call write again */
				nwritten = 0;
			}
			else{
				return(-1);
			}
		}

		nleft -= nwritten;
		ptr   += nwritten;
	}

	return(n);
}

/*
 * Function:	I2Writen
 *
 * Description:	
 * 	Wrapper function for I2Writeni if you really don't ever want to
 * 	return early from write due to an interrupt.
 *
 * In Args:	
 *
 * Out Args:	
 *
 * Scope:	
 * Returns:	
 * Side Effect:	
 */
ssize_t
I2Writen(
	int		fd,
	const void	*vptr,
	size_t		n
	)
{
	int	intr=0;

	return I2Writeni(fd,vptr,n,&intr);
}

/*
 * Function:    I2CopyFile
 *
 * Description:    
 *              Copy one file to another using mmap for speed.
 *
 * In Args:    
 *
 * Out Args:    
 *
 * Scope:    
 * Returns:    
 *              0 on success
 * Side Effect:    
 */
int
I2CopyFile(
        I2ErrHandle eh,
        int         tofd,
        int         fromfd,
        size_t      len
        )
{
    struct stat sbuf;
    int         rc;
    void        *fptr,*tptr;
    size_t      sbufsize;

    if((rc = fstat(fromfd,&sbuf)) != 0){
        I2ErrLog(eh,"I2CopyFile: fstat: %M, status of from file");
        return rc;
    }

    sbufsize = (size_t)sbuf.st_size;
    if(sbuf.st_size != (off_t)sbufsize){
        I2ErrLog(eh,"I2CopyFile: filesystem blocksize too large for mmap");
        return -1;
    }

    if(len == 0){
        len = sbufsize;
    }
    else{
        len = MIN(len,sbufsize);
    }

    if((rc = ftruncate(tofd,(off_t)len)) != 0){
        I2ErrLog(eh,"I2CopyFile: ftrunctate(%llu): %M, sizing to file",len);
        return rc;
    }

    fptr = mmap(NULL,len,PROT_READ|PROT_WRITE,MAP_SHARED,fromfd,(off_t)0);
    if(fptr == MAP_FAILED){
        I2ErrLog(eh,"I2CopyFile: mmap(from file): %M");
        return -1;
    }

    tptr = mmap(NULL,len,PROT_READ|PROT_WRITE,MAP_SHARED,tofd,(off_t)0);
    if(tptr == MAP_FAILED){
        I2ErrLog(eh,"I2CopyFile: mmap(to file): %M");
        return -1;
    }

    memcpy(tptr,fptr,len);

    if((rc = munmap(fptr,len)) != 0){
        I2ErrLog(eh,"I2CopyFile: munmap(from file): %M");
        return -1;
    }

    if((rc = munmap(tptr,len)) != 0){
        I2ErrLog(eh,"I2CopyFile: munmap(to file): %M");
        return -1;
    }

    return 0;
}