]>
Commit | Line | Data |
---|---|---|
578bfd0a AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
7da2b375 | 3 | // $Id: fileutl.cc,v 1.42 2002/09/14 05:29:22 jgg Exp $ |
578bfd0a AL |
4 | /* ###################################################################### |
5 | ||
6 | File Utilities | |
7 | ||
8 | CopyFile - Buffered copy of a single file | |
9 | GetLock - dpkg compatible lock file manipulation (fcntl) | |
10 | ||
614adaa0 MV |
11 | Most of this source is placed in the Public Domain, do with it what |
12 | you will | |
7da2b375 | 13 | It was originally written by Jason Gunthorpe <jgg@debian.org>. |
578bfd0a | 14 | |
614adaa0 MV |
15 | The exception is RunScripts() it is under the GPLv2 |
16 | ||
578bfd0a AL |
17 | ##################################################################### */ |
18 | /*}}}*/ | |
19 | // Include Files /*{{{*/ | |
094a497d AL |
20 | #include <apt-pkg/fileutl.h> |
21 | #include <apt-pkg/error.h> | |
b2e465d6 | 22 | #include <apt-pkg/sptr.h> |
75ef8f14 | 23 | #include <apt-pkg/configuration.h> |
b2e465d6 AL |
24 | |
25 | #include <apti18n.h> | |
578bfd0a | 26 | |
152ab79e | 27 | #include <cstdlib> |
4f333a8b MV |
28 | #include <cstring> |
29 | ||
4d055c05 | 30 | #include <iostream> |
578bfd0a | 31 | #include <unistd.h> |
2c206aa4 | 32 | #include <fcntl.h> |
578bfd0a | 33 | #include <sys/stat.h> |
578bfd0a | 34 | #include <sys/types.h> |
cc2313b7 | 35 | #include <sys/time.h> |
1ae93c94 | 36 | #include <sys/wait.h> |
54676e1a | 37 | #include <signal.h> |
65a1e968 | 38 | #include <errno.h> |
75ef8f14 | 39 | #include <set> |
578bfd0a AL |
40 | /*}}}*/ |
41 | ||
4d055c05 AL |
42 | using namespace std; |
43 | ||
614adaa0 MV |
44 | // RunScripts - Run a set of scripts from a configuration subtree /*{{{*/ |
45 | // --------------------------------------------------------------------- | |
46 | /* */ | |
47 | bool RunScripts(const char *Cnf) | |
48 | { | |
49 | Configuration::Item const *Opts = _config->Tree(Cnf); | |
50 | if (Opts == 0 || Opts->Child == 0) | |
51 | return true; | |
52 | Opts = Opts->Child; | |
53 | ||
54 | // Fork for running the system calls | |
55 | pid_t Child = ExecFork(); | |
56 | ||
57 | // This is the child | |
58 | if (Child == 0) | |
59 | { | |
60 | if (chdir("/tmp/") != 0) | |
61 | _exit(100); | |
62 | ||
63 | unsigned int Count = 1; | |
64 | for (; Opts != 0; Opts = Opts->Next, Count++) | |
65 | { | |
66 | if (Opts->Value.empty() == true) | |
67 | continue; | |
68 | ||
69 | if (system(Opts->Value.c_str()) != 0) | |
70 | _exit(100+Count); | |
71 | } | |
72 | _exit(0); | |
73 | } | |
74 | ||
75 | // Wait for the child | |
76 | int Status = 0; | |
77 | while (waitpid(Child,&Status,0) != Child) | |
78 | { | |
79 | if (errno == EINTR) | |
80 | continue; | |
81 | return _error->Errno("waitpid","Couldn't wait for subprocess"); | |
82 | } | |
83 | ||
84 | // Restore sig int/quit | |
85 | signal(SIGQUIT,SIG_DFL); | |
86 | signal(SIGINT,SIG_DFL); | |
87 | ||
88 | // Check for an error code. | |
89 | if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0) | |
90 | { | |
91 | unsigned int Count = WEXITSTATUS(Status); | |
92 | if (Count > 100) | |
93 | { | |
94 | Count -= 100; | |
95 | for (; Opts != 0 && Count != 1; Opts = Opts->Next, Count--); | |
96 | _error->Error("Problem executing scripts %s '%s'",Cnf,Opts->Value.c_str()); | |
97 | } | |
98 | ||
99 | return _error->Error("Sub-process returned an error code"); | |
100 | } | |
101 | ||
102 | return true; | |
103 | } | |
104 | /*}}}*/ | |
105 | ||
578bfd0a AL |
106 | // CopyFile - Buffered copy of a file /*{{{*/ |
107 | // --------------------------------------------------------------------- | |
108 | /* The caller is expected to set things so that failure causes erasure */ | |
8b89e57f | 109 | bool CopyFile(FileFd &From,FileFd &To) |
578bfd0a AL |
110 | { |
111 | if (From.IsOpen() == false || To.IsOpen() == false) | |
112 | return false; | |
113 | ||
114 | // Buffered copy between fds | |
b2e465d6 | 115 | SPtrArray<unsigned char> Buf = new unsigned char[64000]; |
b0db36b1 AL |
116 | unsigned long Size = From.Size(); |
117 | while (Size != 0) | |
578bfd0a | 118 | { |
b0db36b1 AL |
119 | unsigned long ToRead = Size; |
120 | if (Size > 64000) | |
121 | ToRead = 64000; | |
122 | ||
4a6d5862 | 123 | if (From.Read(Buf,ToRead) == false || |
b0db36b1 | 124 | To.Write(Buf,ToRead) == false) |
578bfd0a | 125 | return false; |
b0db36b1 AL |
126 | |
127 | Size -= ToRead; | |
578bfd0a AL |
128 | } |
129 | ||
578bfd0a AL |
130 | return true; |
131 | } | |
132 | /*}}}*/ | |
133 | // GetLock - Gets a lock file /*{{{*/ | |
134 | // --------------------------------------------------------------------- | |
135 | /* This will create an empty file of the given name and lock it. Once this | |
136 | is done all other calls to GetLock in any other process will fail with | |
137 | -1. The return result is the fd of the file, the call should call | |
138 | close at some time. */ | |
139 | int GetLock(string File,bool Errors) | |
140 | { | |
f659b39a OS |
141 | // GetLock() is used in aptitude on directories with public-write access |
142 | // Use O_NOFOLLOW here to prevent symlink traversal attacks | |
143 | int FD = open(File.c_str(),O_RDWR | O_CREAT | O_NOFOLLOW,0640); | |
578bfd0a AL |
144 | if (FD < 0) |
145 | { | |
b2e465d6 AL |
146 | // Read only .. cant have locking problems there. |
147 | if (errno == EROFS) | |
148 | { | |
149 | _error->Warning(_("Not using locking for read only lock file %s"),File.c_str()); | |
150 | return dup(0); // Need something for the caller to close | |
151 | } | |
152 | ||
578bfd0a | 153 | if (Errors == true) |
b2e465d6 AL |
154 | _error->Errno("open",_("Could not open lock file %s"),File.c_str()); |
155 | ||
156 | // Feh.. We do this to distinguish the lock vs open case.. | |
157 | errno = EPERM; | |
578bfd0a AL |
158 | return -1; |
159 | } | |
b2e465d6 AL |
160 | SetCloseExec(FD,true); |
161 | ||
578bfd0a AL |
162 | // Aquire a write lock |
163 | struct flock fl; | |
c71bc556 AL |
164 | fl.l_type = F_WRLCK; |
165 | fl.l_whence = SEEK_SET; | |
166 | fl.l_start = 0; | |
167 | fl.l_len = 0; | |
578bfd0a AL |
168 | if (fcntl(FD,F_SETLK,&fl) == -1) |
169 | { | |
d89df07a AL |
170 | if (errno == ENOLCK) |
171 | { | |
b2e465d6 AL |
172 | _error->Warning(_("Not using locking for nfs mounted lock file %s"),File.c_str()); |
173 | return dup(0); // Need something for the caller to close | |
d89df07a | 174 | } |
578bfd0a | 175 | if (Errors == true) |
b2e465d6 AL |
176 | _error->Errno("open",_("Could not get lock %s"),File.c_str()); |
177 | ||
178 | int Tmp = errno; | |
578bfd0a | 179 | close(FD); |
b2e465d6 | 180 | errno = Tmp; |
578bfd0a AL |
181 | return -1; |
182 | } | |
183 | ||
184 | return FD; | |
185 | } | |
186 | /*}}}*/ | |
187 | // FileExists - Check if a file exists /*{{{*/ | |
188 | // --------------------------------------------------------------------- | |
189 | /* */ | |
190 | bool FileExists(string File) | |
191 | { | |
192 | struct stat Buf; | |
193 | if (stat(File.c_str(),&Buf) != 0) | |
194 | return false; | |
195 | return true; | |
196 | } | |
197 | /*}}}*/ | |
198 | // SafeGetCWD - This is a safer getcwd that returns a dynamic string /*{{{*/ | |
199 | // --------------------------------------------------------------------- | |
200 | /* We return / on failure. */ | |
201 | string SafeGetCWD() | |
202 | { | |
203 | // Stash the current dir. | |
204 | char S[300]; | |
205 | S[0] = 0; | |
7f25bdff | 206 | if (getcwd(S,sizeof(S)-2) == 0) |
578bfd0a | 207 | return "/"; |
7f25bdff AL |
208 | unsigned int Len = strlen(S); |
209 | S[Len] = '/'; | |
210 | S[Len+1] = 0; | |
578bfd0a AL |
211 | return S; |
212 | } | |
213 | /*}}}*/ | |
8ce4327b AL |
214 | // flNotDir - Strip the directory from the filename /*{{{*/ |
215 | // --------------------------------------------------------------------- | |
216 | /* */ | |
217 | string flNotDir(string File) | |
218 | { | |
219 | string::size_type Res = File.rfind('/'); | |
220 | if (Res == string::npos) | |
221 | return File; | |
222 | Res++; | |
223 | return string(File,Res,Res - File.length()); | |
224 | } | |
225 | /*}}}*/ | |
d38b7b3d AL |
226 | // flNotFile - Strip the file from the directory name /*{{{*/ |
227 | // --------------------------------------------------------------------- | |
171c45bc | 228 | /* Result ends in a / */ |
d38b7b3d AL |
229 | string flNotFile(string File) |
230 | { | |
231 | string::size_type Res = File.rfind('/'); | |
232 | if (Res == string::npos) | |
171c45bc | 233 | return "./"; |
d38b7b3d AL |
234 | Res++; |
235 | return string(File,0,Res); | |
236 | } | |
237 | /*}}}*/ | |
b2e465d6 AL |
238 | // flExtension - Return the extension for the file /*{{{*/ |
239 | // --------------------------------------------------------------------- | |
240 | /* */ | |
241 | string flExtension(string File) | |
242 | { | |
243 | string::size_type Res = File.rfind('.'); | |
244 | if (Res == string::npos) | |
245 | return File; | |
246 | Res++; | |
247 | return string(File,Res,Res - File.length()); | |
248 | } | |
249 | /*}}}*/ | |
421c8d10 AL |
250 | // flNoLink - If file is a symlink then deref it /*{{{*/ |
251 | // --------------------------------------------------------------------- | |
252 | /* If the name is not a link then the returned path is the input. */ | |
253 | string flNoLink(string File) | |
254 | { | |
255 | struct stat St; | |
256 | if (lstat(File.c_str(),&St) != 0 || S_ISLNK(St.st_mode) == 0) | |
257 | return File; | |
258 | if (stat(File.c_str(),&St) != 0) | |
259 | return File; | |
260 | ||
261 | /* Loop resolving the link. There is no need to limit the number of | |
262 | loops because the stat call above ensures that the symlink is not | |
263 | circular */ | |
264 | char Buffer[1024]; | |
265 | string NFile = File; | |
266 | while (1) | |
267 | { | |
268 | // Read the link | |
269 | int Res; | |
270 | if ((Res = readlink(NFile.c_str(),Buffer,sizeof(Buffer))) <= 0 || | |
271 | (unsigned)Res >= sizeof(Buffer)) | |
272 | return File; | |
273 | ||
274 | // Append or replace the previous path | |
275 | Buffer[Res] = 0; | |
276 | if (Buffer[0] == '/') | |
277 | NFile = Buffer; | |
278 | else | |
279 | NFile = flNotFile(NFile) + Buffer; | |
280 | ||
281 | // See if we are done | |
282 | if (lstat(NFile.c_str(),&St) != 0) | |
283 | return File; | |
284 | if (S_ISLNK(St.st_mode) == 0) | |
285 | return NFile; | |
286 | } | |
287 | } | |
288 | /*}}}*/ | |
b2e465d6 AL |
289 | // flCombine - Combine a file and a directory /*{{{*/ |
290 | // --------------------------------------------------------------------- | |
291 | /* If the file is an absolute path then it is just returned, otherwise | |
292 | the directory is pre-pended to it. */ | |
293 | string flCombine(string Dir,string File) | |
294 | { | |
295 | if (File.empty() == true) | |
296 | return string(); | |
297 | ||
298 | if (File[0] == '/' || Dir.empty() == true) | |
299 | return File; | |
300 | if (File.length() >= 2 && File[0] == '.' && File[1] == '/') | |
301 | return File; | |
302 | if (Dir[Dir.length()-1] == '/') | |
303 | return Dir + File; | |
304 | return Dir + '/' + File; | |
305 | } | |
306 | /*}}}*/ | |
3b5421b4 AL |
307 | // SetCloseExec - Set the close on exec flag /*{{{*/ |
308 | // --------------------------------------------------------------------- | |
309 | /* */ | |
310 | void SetCloseExec(int Fd,bool Close) | |
311 | { | |
312 | if (fcntl(Fd,F_SETFD,(Close == false)?0:FD_CLOEXEC) != 0) | |
313 | { | |
314 | cerr << "FATAL -> Could not set close on exec " << strerror(errno) << endl; | |
315 | exit(100); | |
316 | } | |
317 | } | |
318 | /*}}}*/ | |
319 | // SetNonBlock - Set the nonblocking flag /*{{{*/ | |
320 | // --------------------------------------------------------------------- | |
321 | /* */ | |
322 | void SetNonBlock(int Fd,bool Block) | |
323 | { | |
0a8a80e5 AL |
324 | int Flags = fcntl(Fd,F_GETFL) & (~O_NONBLOCK); |
325 | if (fcntl(Fd,F_SETFL,Flags | ((Block == false)?0:O_NONBLOCK)) != 0) | |
3b5421b4 AL |
326 | { |
327 | cerr << "FATAL -> Could not set non-blocking flag " << strerror(errno) << endl; | |
328 | exit(100); | |
329 | } | |
330 | } | |
331 | /*}}}*/ | |
332 | // WaitFd - Wait for a FD to become readable /*{{{*/ | |
333 | // --------------------------------------------------------------------- | |
b2e465d6 | 334 | /* This waits for a FD to become readable using select. It is useful for |
6d5dd02a AL |
335 | applications making use of non-blocking sockets. The timeout is |
336 | in seconds. */ | |
1084d58a | 337 | bool WaitFd(int Fd,bool write,unsigned long timeout) |
3b5421b4 AL |
338 | { |
339 | fd_set Set; | |
cc2313b7 | 340 | struct timeval tv; |
3b5421b4 AL |
341 | FD_ZERO(&Set); |
342 | FD_SET(Fd,&Set); | |
6d5dd02a AL |
343 | tv.tv_sec = timeout; |
344 | tv.tv_usec = 0; | |
1084d58a | 345 | if (write == true) |
b0db36b1 AL |
346 | { |
347 | int Res; | |
348 | do | |
349 | { | |
350 | Res = select(Fd+1,0,&Set,0,(timeout != 0?&tv:0)); | |
351 | } | |
352 | while (Res < 0 && errno == EINTR); | |
353 | ||
354 | if (Res <= 0) | |
355 | return false; | |
1084d58a AL |
356 | } |
357 | else | |
358 | { | |
b0db36b1 AL |
359 | int Res; |
360 | do | |
361 | { | |
362 | Res = select(Fd+1,&Set,0,0,(timeout != 0?&tv:0)); | |
363 | } | |
364 | while (Res < 0 && errno == EINTR); | |
365 | ||
366 | if (Res <= 0) | |
367 | return false; | |
cc2313b7 | 368 | } |
1084d58a | 369 | |
3b5421b4 AL |
370 | return true; |
371 | } | |
372 | /*}}}*/ | |
54676e1a AL |
373 | // ExecFork - Magical fork that sanitizes the context before execing /*{{{*/ |
374 | // --------------------------------------------------------------------- | |
375 | /* This is used if you want to cleanse the environment for the forked | |
376 | child, it fixes up the important signals and nukes all of the fds, | |
377 | otherwise acts like normal fork. */ | |
75ef8f14 | 378 | pid_t ExecFork() |
54676e1a AL |
379 | { |
380 | // Fork off the process | |
381 | pid_t Process = fork(); | |
382 | if (Process < 0) | |
383 | { | |
384 | cerr << "FATAL -> Failed to fork." << endl; | |
385 | exit(100); | |
386 | } | |
387 | ||
388 | // Spawn the subprocess | |
389 | if (Process == 0) | |
390 | { | |
391 | // Setup the signals | |
392 | signal(SIGPIPE,SIG_DFL); | |
393 | signal(SIGQUIT,SIG_DFL); | |
394 | signal(SIGINT,SIG_DFL); | |
395 | signal(SIGWINCH,SIG_DFL); | |
396 | signal(SIGCONT,SIG_DFL); | |
397 | signal(SIGTSTP,SIG_DFL); | |
75ef8f14 MV |
398 | |
399 | set<int> KeepFDs; | |
400 | Configuration::Item const *Opts = _config->Tree("APT::Keep-Fds"); | |
401 | if (Opts != 0 && Opts->Child != 0) | |
402 | { | |
403 | Opts = Opts->Child; | |
404 | for (; Opts != 0; Opts = Opts->Next) | |
405 | { | |
406 | if (Opts->Value.empty() == true) | |
407 | continue; | |
408 | int fd = atoi(Opts->Value.c_str()); | |
409 | KeepFDs.insert(fd); | |
410 | } | |
411 | } | |
412 | ||
54676e1a AL |
413 | // Close all of our FDs - just in case |
414 | for (int K = 3; K != 40; K++) | |
75ef8f14 MV |
415 | { |
416 | if(KeepFDs.find(K) == KeepFDs.end()) | |
007dc9e0 | 417 | fcntl(K,F_SETFD,FD_CLOEXEC); |
75ef8f14 | 418 | } |
54676e1a AL |
419 | } |
420 | ||
421 | return Process; | |
422 | } | |
423 | /*}}}*/ | |
ddc1d8d0 AL |
424 | // ExecWait - Fancy waitpid /*{{{*/ |
425 | // --------------------------------------------------------------------- | |
2c9a72d1 | 426 | /* Waits for the given sub process. If Reap is set then no errors are |
ddc1d8d0 AL |
427 | generated. Otherwise a failed subprocess will generate a proper descriptive |
428 | message */ | |
3826564e | 429 | bool ExecWait(pid_t Pid,const char *Name,bool Reap) |
ddc1d8d0 AL |
430 | { |
431 | if (Pid <= 1) | |
432 | return true; | |
433 | ||
434 | // Wait and collect the error code | |
435 | int Status; | |
436 | while (waitpid(Pid,&Status,0) != Pid) | |
437 | { | |
438 | if (errno == EINTR) | |
439 | continue; | |
440 | ||
441 | if (Reap == true) | |
442 | return false; | |
443 | ||
db0db9fe | 444 | return _error->Error(_("Waited for %s but it wasn't there"),Name); |
ddc1d8d0 AL |
445 | } |
446 | ||
447 | ||
448 | // Check for an error code. | |
449 | if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0) | |
450 | { | |
451 | if (Reap == true) | |
452 | return false; | |
ab7f4d7c | 453 | if (WIFSIGNALED(Status) != 0) |
40e7fe0e | 454 | { |
ab7f4d7c MV |
455 | if( WTERMSIG(Status) == SIGSEGV) |
456 | return _error->Error(_("Sub-process %s received a segmentation fault."),Name); | |
457 | else | |
458 | return _error->Error(_("Sub-process %s received signal %u."),Name, WTERMSIG(Status)); | |
40e7fe0e | 459 | } |
ddc1d8d0 AL |
460 | |
461 | if (WIFEXITED(Status) != 0) | |
b2e465d6 | 462 | return _error->Error(_("Sub-process %s returned an error code (%u)"),Name,WEXITSTATUS(Status)); |
ddc1d8d0 | 463 | |
b2e465d6 | 464 | return _error->Error(_("Sub-process %s exited unexpectedly"),Name); |
ddc1d8d0 AL |
465 | } |
466 | ||
467 | return true; | |
468 | } | |
469 | /*}}}*/ | |
578bfd0a | 470 | |
13d87e2e | 471 | // FileFd::Open - Open a file /*{{{*/ |
578bfd0a AL |
472 | // --------------------------------------------------------------------- |
473 | /* The most commonly used open mode combinations are given with Mode */ | |
13d87e2e | 474 | bool FileFd::Open(string FileName,OpenMode Mode, unsigned long Perms) |
578bfd0a | 475 | { |
13d87e2e | 476 | Close(); |
1164783d | 477 | Flags = AutoClose; |
578bfd0a AL |
478 | switch (Mode) |
479 | { | |
480 | case ReadOnly: | |
481 | iFd = open(FileName.c_str(),O_RDONLY); | |
482 | break; | |
483 | ||
484 | case WriteEmpty: | |
50b513a1 AL |
485 | { |
486 | struct stat Buf; | |
459681d3 | 487 | if (lstat(FileName.c_str(),&Buf) == 0 && S_ISLNK(Buf.st_mode)) |
50b513a1 AL |
488 | unlink(FileName.c_str()); |
489 | iFd = open(FileName.c_str(),O_RDWR | O_CREAT | O_TRUNC,Perms); | |
490 | break; | |
491 | } | |
578bfd0a AL |
492 | |
493 | case WriteExists: | |
494 | iFd = open(FileName.c_str(),O_RDWR); | |
495 | break; | |
0a8e3465 AL |
496 | |
497 | case WriteAny: | |
498 | iFd = open(FileName.c_str(),O_RDWR | O_CREAT,Perms); | |
d38b7b3d | 499 | break; |
f08fcf34 AL |
500 | |
501 | case WriteTemp: | |
4decd43c AL |
502 | unlink(FileName.c_str()); |
503 | iFd = open(FileName.c_str(),O_RDWR | O_CREAT | O_EXCL,Perms); | |
f08fcf34 | 504 | break; |
578bfd0a AL |
505 | } |
506 | ||
507 | if (iFd < 0) | |
b2e465d6 | 508 | return _error->Errno("open",_("Could not open file %s"),FileName.c_str()); |
13d87e2e AL |
509 | |
510 | this->FileName = FileName; | |
511 | SetCloseExec(iFd,true); | |
512 | return true; | |
578bfd0a AL |
513 | } |
514 | /*}}}*/ | |
8e06abb2 | 515 | // FileFd::~File - Closes the file /*{{{*/ |
578bfd0a AL |
516 | // --------------------------------------------------------------------- |
517 | /* If the proper modes are selected then we close the Fd and possibly | |
518 | unlink the file on error. */ | |
8e06abb2 | 519 | FileFd::~FileFd() |
578bfd0a AL |
520 | { |
521 | Close(); | |
522 | } | |
523 | /*}}}*/ | |
8e06abb2 | 524 | // FileFd::Read - Read a bit of the file /*{{{*/ |
578bfd0a | 525 | // --------------------------------------------------------------------- |
b0db36b1 AL |
526 | /* We are carefull to handle interruption by a signal while reading |
527 | gracefully. */ | |
f604cf55 | 528 | bool FileFd::Read(void *To,unsigned long Size,unsigned long *Actual) |
578bfd0a | 529 | { |
b0db36b1 AL |
530 | int Res; |
531 | errno = 0; | |
f604cf55 AL |
532 | if (Actual != 0) |
533 | *Actual = 0; | |
534 | ||
b0db36b1 | 535 | do |
578bfd0a | 536 | { |
b0db36b1 AL |
537 | Res = read(iFd,To,Size); |
538 | if (Res < 0 && errno == EINTR) | |
539 | continue; | |
540 | if (Res < 0) | |
541 | { | |
542 | Flags |= Fail; | |
b2e465d6 | 543 | return _error->Errno("read",_("Read error")); |
b0db36b1 | 544 | } |
578bfd0a | 545 | |
b0db36b1 AL |
546 | To = (char *)To + Res; |
547 | Size -= Res; | |
f604cf55 AL |
548 | if (Actual != 0) |
549 | *Actual += Res; | |
b0db36b1 AL |
550 | } |
551 | while (Res > 0 && Size > 0); | |
552 | ||
553 | if (Size == 0) | |
554 | return true; | |
555 | ||
ddc1d8d0 | 556 | // Eof handling |
f604cf55 | 557 | if (Actual != 0) |
ddc1d8d0 AL |
558 | { |
559 | Flags |= HitEof; | |
560 | return true; | |
561 | } | |
562 | ||
b0db36b1 | 563 | Flags |= Fail; |
b2e465d6 | 564 | return _error->Error(_("read, still have %lu to read but none left"),Size); |
578bfd0a AL |
565 | } |
566 | /*}}}*/ | |
8e06abb2 | 567 | // FileFd::Write - Write to the file /*{{{*/ |
578bfd0a AL |
568 | // --------------------------------------------------------------------- |
569 | /* */ | |
a05599f1 | 570 | bool FileFd::Write(const void *From,unsigned long Size) |
578bfd0a | 571 | { |
b0db36b1 AL |
572 | int Res; |
573 | errno = 0; | |
574 | do | |
578bfd0a | 575 | { |
b0db36b1 AL |
576 | Res = write(iFd,From,Size); |
577 | if (Res < 0 && errno == EINTR) | |
578 | continue; | |
579 | if (Res < 0) | |
580 | { | |
581 | Flags |= Fail; | |
b2e465d6 | 582 | return _error->Errno("write",_("Write error")); |
b0db36b1 AL |
583 | } |
584 | ||
585 | From = (char *)From + Res; | |
586 | Size -= Res; | |
578bfd0a | 587 | } |
b0db36b1 | 588 | while (Res > 0 && Size > 0); |
578bfd0a | 589 | |
b0db36b1 AL |
590 | if (Size == 0) |
591 | return true; | |
592 | ||
593 | Flags |= Fail; | |
b2e465d6 | 594 | return _error->Error(_("write, still have %lu to write but couldn't"),Size); |
578bfd0a AL |
595 | } |
596 | /*}}}*/ | |
8e06abb2 | 597 | // FileFd::Seek - Seek in the file /*{{{*/ |
578bfd0a AL |
598 | // --------------------------------------------------------------------- |
599 | /* */ | |
8e06abb2 | 600 | bool FileFd::Seek(unsigned long To) |
578bfd0a AL |
601 | { |
602 | if (lseek(iFd,To,SEEK_SET) != (signed)To) | |
603 | { | |
604 | Flags |= Fail; | |
b2e465d6 | 605 | return _error->Error("Unable to seek to %lu",To); |
578bfd0a AL |
606 | } |
607 | ||
727f18af AL |
608 | return true; |
609 | } | |
610 | /*}}}*/ | |
611 | // FileFd::Skip - Seek in the file /*{{{*/ | |
612 | // --------------------------------------------------------------------- | |
613 | /* */ | |
614 | bool FileFd::Skip(unsigned long Over) | |
615 | { | |
616 | if (lseek(iFd,Over,SEEK_CUR) < 0) | |
617 | { | |
618 | Flags |= Fail; | |
b2e465d6 | 619 | return _error->Error("Unable to seek ahead %lu",Over); |
727f18af AL |
620 | } |
621 | ||
6d5dd02a AL |
622 | return true; |
623 | } | |
624 | /*}}}*/ | |
625 | // FileFd::Truncate - Truncate the file /*{{{*/ | |
626 | // --------------------------------------------------------------------- | |
627 | /* */ | |
628 | bool FileFd::Truncate(unsigned long To) | |
629 | { | |
630 | if (ftruncate(iFd,To) != 0) | |
631 | { | |
632 | Flags |= Fail; | |
b2e465d6 | 633 | return _error->Error("Unable to truncate to %lu",To); |
6d5dd02a AL |
634 | } |
635 | ||
578bfd0a AL |
636 | return true; |
637 | } | |
638 | /*}}}*/ | |
7f25bdff AL |
639 | // FileFd::Tell - Current seek position /*{{{*/ |
640 | // --------------------------------------------------------------------- | |
641 | /* */ | |
642 | unsigned long FileFd::Tell() | |
643 | { | |
644 | off_t Res = lseek(iFd,0,SEEK_CUR); | |
645 | if (Res == (off_t)-1) | |
646 | _error->Errno("lseek","Failed to determine the current file position"); | |
647 | return Res; | |
648 | } | |
649 | /*}}}*/ | |
8e06abb2 | 650 | // FileFd::Size - Return the size of the file /*{{{*/ |
578bfd0a AL |
651 | // --------------------------------------------------------------------- |
652 | /* */ | |
8e06abb2 | 653 | unsigned long FileFd::Size() |
578bfd0a AL |
654 | { |
655 | struct stat Buf; | |
656 | if (fstat(iFd,&Buf) != 0) | |
657 | return _error->Errno("fstat","Unable to determine the file size"); | |
658 | return Buf.st_size; | |
659 | } | |
660 | /*}}}*/ | |
8e06abb2 | 661 | // FileFd::Close - Close the file if the close flag is set /*{{{*/ |
578bfd0a AL |
662 | // --------------------------------------------------------------------- |
663 | /* */ | |
8e06abb2 | 664 | bool FileFd::Close() |
578bfd0a AL |
665 | { |
666 | bool Res = true; | |
667 | if ((Flags & AutoClose) == AutoClose) | |
1164783d | 668 | if (iFd >= 0 && close(iFd) != 0) |
b2e465d6 | 669 | Res &= _error->Errno("close",_("Problem closing the file")); |
1164783d AL |
670 | iFd = -1; |
671 | ||
578bfd0a AL |
672 | if ((Flags & Fail) == Fail && (Flags & DelOnFail) == DelOnFail && |
673 | FileName.empty() == false) | |
674 | if (unlink(FileName.c_str()) != 0) | |
b2e465d6 | 675 | Res &= _error->WarningE("unlnk",_("Problem unlinking the file")); |
578bfd0a AL |
676 | return Res; |
677 | } | |
678 | /*}}}*/ | |
b2e465d6 AL |
679 | // FileFd::Sync - Sync the file /*{{{*/ |
680 | // --------------------------------------------------------------------- | |
681 | /* */ | |
682 | bool FileFd::Sync() | |
683 | { | |
684 | #ifdef _POSIX_SYNCHRONIZED_IO | |
685 | if (fsync(iFd) != 0) | |
686 | return _error->Errno("sync",_("Problem syncing the file")); | |
687 | #endif | |
688 | return true; | |
689 | } | |
690 | /*}}}*/ |