]> git.saurik.com Git - apt.git/blob - apt-inst/dirstream.cc
Resync
[apt.git] / apt-inst / dirstream.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: dirstream.cc,v 1.3 2003/02/10 00:36:12 doogie Exp $
4 /* ######################################################################
5
6 Directory Stream
7
8 This class provides a simple basic extractor that can be used for
9 a number of purposes.
10
11 ##################################################################### */
12 /*}}}*/
13 // Include Files /*{{{*/
14 #ifdef __GNUG__
15 #pragma implementation "apt-pkg/dirstream.h"
16 #endif
17
18 #include <apti18n.h>
19 #include <apt-pkg/dirstream.h>
20 #include <apt-pkg/error.h>
21
22 #include <fcntl.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <errno.h>
26 #include <utime.h>
27 #include <unistd.h>
28 /*}}}*/
29
30 // DirStream::DoItem - Process an item /*{{{*/
31 // ---------------------------------------------------------------------
32 /* This is a very simple extractor, it does not deal with things like
33 overwriting directories with files and so on. */
34 bool pkgDirStream::DoItem(Item &Itm,int &Fd)
35 {
36 switch (Itm.Type)
37 {
38 case Item::File:
39 {
40 /* Open the output file, NDELAY is used to prevent this from
41 blowing up on device special files.. */
42 int iFd = open(Itm.Name,O_NDELAY|O_WRONLY|O_CREAT|O_TRUNC|O_APPEND,
43 Itm.Mode);
44 if (iFd < 0)
45 return _error->Errno("open",_("Failed write file %s"),
46 Itm.Name);
47
48 // fchmod deals with umask and fchown sets the ownership
49 if (fchmod(iFd,Itm.Mode) != 0)
50 return _error->Errno("fchmod",_("Failed write file %s"),
51 Itm.Name);
52 if (fchown(iFd,Itm.UID,Itm.GID) != 0 && errno != EPERM)
53 return _error->Errno("fchown",_("Failed write file %s"),
54 Itm.Name);
55 Fd = iFd;
56 return true;
57 }
58
59 case Item::HardLink:
60 case Item::SymbolicLink:
61 case Item::CharDevice:
62 case Item::BlockDevice:
63 case Item::Directory:
64 case Item::FIFO:
65 break;
66 }
67
68 return true;
69 }
70 /*}}}*/
71 // DirStream::FinishedFile - Finished processing a file /*{{{*/
72 // ---------------------------------------------------------------------
73 /* */
74 bool pkgDirStream::FinishedFile(Item &Itm,int Fd)
75 {
76 if (Fd < 0)
77 return true;
78
79 if (close(Fd) != 0)
80 return _error->Errno("close",_("Failed to close file %s"),Itm.Name);
81
82 /* Set the modification times. The only way it can fail is if someone
83 has futzed with our file, which is intolerable :> */
84 struct utimbuf Time;
85 Time.actime = Itm.MTime;
86 Time.modtime = Itm.MTime;
87 if (utime(Itm.Name,&Time) != 0)
88 _error->Errno("utime",_("Failed to close file %s"),Itm.Name);
89
90 return true;
91 }
92 /*}}}*/
93 // DirStream::Fail - Failed processing a file /*{{{*/
94 // ---------------------------------------------------------------------
95 /* */
96 bool pkgDirStream::Fail(Item &Itm,int Fd)
97 {
98 if (Fd < 0)
99 return true;
100
101 close(Fd);
102 return false;
103 }
104 /*}}}*/