]> git.saurik.com Git - apt.git/blame - apt-inst/dirstream.cc
* debian/changelog:
[apt.git] / apt-inst / dirstream.cc
CommitLineData
b2e465d6
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
b3d44315 3// $Id: dirstream.cc,v 1.3.2.1 2004/01/16 18:58:50 mdz Exp $
b2e465d6
AL
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 <apt-pkg/dirstream.h>
19#include <apt-pkg/error.h>
20
21#include <fcntl.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24#include <errno.h>
25#include <utime.h>
26#include <unistd.h>
d77559ac 27#include <apti18n.h>
b2e465d6
AL
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. */
34bool 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)
6804503b 45 return _error->Errno("open",_("Failed to write file %s"),
b2e465d6
AL
46 Itm.Name);
47
48 // fchmod deals with umask and fchown sets the ownership
49 if (fchmod(iFd,Itm.Mode) != 0)
6804503b 50 return _error->Errno("fchmod",_("Failed to write file %s"),
b2e465d6
AL
51 Itm.Name);
52 if (fchown(iFd,Itm.UID,Itm.GID) != 0 && errno != EPERM)
6804503b 53 return _error->Errno("fchown",_("Failed to write file %s"),
b2e465d6
AL
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:
f2047f6b
MV
64 {
65 struct stat Buf;
66 // check if the dir is already there, if so return true
67 if (stat(Itm.Name,&Buf) == 0)
68 {
69 if(S_ISDIR(Buf.st_mode))
70 return true;
71 // something else is there already, return false
72 return false;
73 }
74 // nothing here, create the dir
75 if(mkdir(Itm.Name,Itm.Mode) < 0)
76 return false;
77 return true;
78 break;
79 }
b2e465d6
AL
80 case Item::FIFO:
81 break;
82 }
83
84 return true;
85}
86 /*}}}*/
87// DirStream::FinishedFile - Finished processing a file /*{{{*/
88// ---------------------------------------------------------------------
89/* */
90bool pkgDirStream::FinishedFile(Item &Itm,int Fd)
91{
92 if (Fd < 0)
93 return true;
94
95 if (close(Fd) != 0)
05eb7df0 96 return _error->Errno("close",_("Failed to close file %s"),Itm.Name);
b2e465d6
AL
97
98 /* Set the modification times. The only way it can fail is if someone
99 has futzed with our file, which is intolerable :> */
100 struct utimbuf Time;
101 Time.actime = Itm.MTime;
102 Time.modtime = Itm.MTime;
103 if (utime(Itm.Name,&Time) != 0)
05eb7df0 104 _error->Errno("utime",_("Failed to close file %s"),Itm.Name);
b2e465d6
AL
105
106 return true;
107}
108 /*}}}*/
109// DirStream::Fail - Failed processing a file /*{{{*/
110// ---------------------------------------------------------------------
111/* */
112bool pkgDirStream::Fail(Item &Itm,int Fd)
113{
114 if (Fd < 0)
115 return true;
116
117 close(Fd);
118 return false;
119}
120 /*}}}*/