]> git.saurik.com Git - apt.git/blob - apt-inst/dirstream.cc
cherrypick from my apt/experimental branch
[apt.git] / apt-inst / dirstream.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: dirstream.cc,v 1.3.2.1 2004/01/16 18:58:50 mdz 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 #include <apt-pkg/dirstream.h>
15 #include <apt-pkg/error.h>
16
17 #include <fcntl.h>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <errno.h>
21 #include <utime.h>
22 #include <unistd.h>
23 #include <apti18n.h>
24 /*}}}*/
25
26 // DirStream::DoItem - Process an item /*{{{*/
27 // ---------------------------------------------------------------------
28 /* This is a very simple extractor, it does not deal with things like
29 overwriting directories with files and so on. */
30 bool pkgDirStream::DoItem(Item &Itm,int &Fd)
31 {
32 switch (Itm.Type)
33 {
34 case Item::File:
35 {
36 /* Open the output file, NDELAY is used to prevent this from
37 blowing up on device special files.. */
38 int iFd = open(Itm.Name,O_NDELAY|O_WRONLY|O_CREAT|O_TRUNC|O_APPEND,
39 Itm.Mode);
40 if (iFd < 0)
41 return _error->Errno("open",_("Failed to write file %s"),
42 Itm.Name);
43
44 // fchmod deals with umask and fchown sets the ownership
45 if (fchmod(iFd,Itm.Mode) != 0)
46 {
47 _error->Errno("fchmod",_("Failed to write file %s"), Itm.Name);
48 close(iFd);
49 return false;
50 }
51 if (fchown(iFd,Itm.UID,Itm.GID) != 0 && errno != EPERM)
52 {
53 return _error->Errno("fchown",_("Failed to write file %s"), Itm.Name);
54 close(iFd);
55 return false;
56 }
57 Fd = iFd;
58 return true;
59 }
60
61 case Item::HardLink:
62 case Item::SymbolicLink:
63 case Item::CharDevice:
64 case Item::BlockDevice:
65 case Item::Directory:
66 {
67 struct stat Buf;
68 // check if the dir is already there, if so return true
69 if (stat(Itm.Name,&Buf) == 0)
70 {
71 if(S_ISDIR(Buf.st_mode))
72 return true;
73 // something else is there already, return false
74 return false;
75 }
76 // nothing here, create the dir
77 if(mkdir(Itm.Name,Itm.Mode) < 0)
78 return false;
79 return true;
80 break;
81 }
82 case Item::FIFO:
83 break;
84 }
85
86 return true;
87 }
88 /*}}}*/
89 // DirStream::FinishedFile - Finished processing a file /*{{{*/
90 // ---------------------------------------------------------------------
91 /* */
92 bool pkgDirStream::FinishedFile(Item &Itm,int Fd)
93 {
94 if (Fd < 0)
95 return true;
96
97 if (close(Fd) != 0)
98 return _error->Errno("close",_("Failed to close file %s"),Itm.Name);
99
100 /* Set the modification times. The only way it can fail is if someone
101 has futzed with our file, which is intolerable :> */
102 struct utimbuf Time;
103 Time.actime = Itm.MTime;
104 Time.modtime = Itm.MTime;
105 if (utime(Itm.Name,&Time) != 0)
106 _error->Errno("utime",_("Failed to close file %s"),Itm.Name);
107
108 return true;
109 }
110 /*}}}*/
111 // DirStream::Fail - Failed processing a file /*{{{*/
112 // ---------------------------------------------------------------------
113 /* */
114 bool pkgDirStream::Fail(Item &Itm,int Fd)
115 {
116 if (Fd < 0)
117 return true;
118
119 close(Fd);
120 return false;
121 }
122 /*}}}*/