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