]> git.saurik.com Git - apt.git/blame - apt-inst/dirstream.cc
edsp: ask policy engine for the pin of the version directly
[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 /*{{{*/
ea542140
DK
14#include<config.h>
15
b2e465d6
AL
16#include <apt-pkg/dirstream.h>
17#include <apt-pkg/error.h>
18
19#include <fcntl.h>
20#include <sys/stat.h>
246bbb61 21#include <sys/time.h>
b2e465d6 22#include <errno.h>
b2e465d6 23#include <unistd.h>
d77559ac 24#include <apti18n.h>
b2e465d6
AL
25 /*}}}*/
26
27// DirStream::DoItem - Process an item /*{{{*/
28// ---------------------------------------------------------------------
29/* This is a very simple extractor, it does not deal with things like
30 overwriting directories with files and so on. */
31bool pkgDirStream::DoItem(Item &Itm,int &Fd)
32{
33 switch (Itm.Type)
34 {
35 case Item::File:
36 {
37 /* Open the output file, NDELAY is used to prevent this from
38 blowing up on device special files.. */
39 int iFd = open(Itm.Name,O_NDELAY|O_WRONLY|O_CREAT|O_TRUNC|O_APPEND,
40 Itm.Mode);
41 if (iFd < 0)
6804503b 42 return _error->Errno("open",_("Failed to write file %s"),
b2e465d6
AL
43 Itm.Name);
44
45 // fchmod deals with umask and fchown sets the ownership
46 if (fchmod(iFd,Itm.Mode) != 0)
6070a346 47 {
6070a346 48 close(iFd);
f685054e 49 return _error->Errno("fchmod",_("Failed to write file %s"), Itm.Name);
6070a346 50 }
b2e465d6 51 if (fchown(iFd,Itm.UID,Itm.GID) != 0 && errno != EPERM)
6070a346 52 {
6070a346 53 close(iFd);
f685054e 54 return _error->Errno("fchown",_("Failed to write file %s"), Itm.Name);
6070a346 55 }
b2e465d6
AL
56 Fd = iFd;
57 return true;
58 }
59
60 case Item::HardLink:
61 case Item::SymbolicLink:
62 case Item::CharDevice:
63 case Item::BlockDevice:
64 case Item::Directory:
f2047f6b
MV
65 {
66 struct stat Buf;
67 // check if the dir is already there, if so return true
68 if (stat(Itm.Name,&Buf) == 0)
69 {
70 if(S_ISDIR(Buf.st_mode))
71 return true;
72 // something else is there already, return false
73 return false;
74 }
75 // nothing here, create the dir
76 if(mkdir(Itm.Name,Itm.Mode) < 0)
77 return false;
78 return true;
f2047f6b 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;
b2e465d6
AL
94
95 /* Set the modification times. The only way it can fail is if someone
96 has futzed with our file, which is intolerable :> */
246bbb61 97 struct timeval times[2];
9ce3cfc9 98 times[0].tv_sec = times[1].tv_sec = Itm.MTime;
246bbb61
DK
99 times[0].tv_usec = times[1].tv_usec = 0;
100 if (utimes(Itm.Name, times) != 0)
101 _error->Errno("utimes", "Failed to set modification time for %s",Itm.Name);
9ce3cfc9
DK
102
103 if (close(Fd) != 0)
104 return _error->Errno("close",_("Failed to close file %s"),Itm.Name);
105 return true;
b2e465d6
AL
106}
107 /*}}}*/
108// DirStream::Fail - Failed processing a file /*{{{*/
109// ---------------------------------------------------------------------
110/* */
65512241 111bool pkgDirStream::Fail(Item &/*Itm*/, int Fd)
b2e465d6
AL
112{
113 if (Fd < 0)
114 return true;
115
116 close(Fd);
117 return false;
118}
119 /*}}}*/