use utimes instead of utimensat/futimens
[ntk/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>
21#include <sys/types.h>
246bbb61 22#include <sys/time.h>
b2e465d6 23#include <errno.h>
b2e465d6 24#include <unistd.h>
d77559ac 25#include <apti18n.h>
b2e465d6
AL
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. */
32bool 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)
6804503b 43 return _error->Errno("open",_("Failed to write file %s"),
b2e465d6
AL
44 Itm.Name);
45
46 // fchmod deals with umask and fchown sets the ownership
47 if (fchmod(iFd,Itm.Mode) != 0)
6070a346 48 {
6070a346 49 close(iFd);
f685054e 50 return _error->Errno("fchmod",_("Failed to write file %s"), Itm.Name);
6070a346 51 }
b2e465d6 52 if (fchown(iFd,Itm.UID,Itm.GID) != 0 && errno != EPERM)
6070a346 53 {
6070a346 54 close(iFd);
f685054e 55 return _error->Errno("fchown",_("Failed to write file %s"), Itm.Name);
6070a346 56 }
b2e465d6
AL
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:
f2047f6b
MV
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 }
b2e465d6
AL
82 case Item::FIFO:
83 break;
84 }
85
86 return true;
87}
88 /*}}}*/
89// DirStream::FinishedFile - Finished processing a file /*{{{*/
90// ---------------------------------------------------------------------
91/* */
92bool pkgDirStream::FinishedFile(Item &Itm,int Fd)
93{
94 if (Fd < 0)
95 return true;
b2e465d6
AL
96
97 /* Set the modification times. The only way it can fail is if someone
98 has futzed with our file, which is intolerable :> */
246bbb61 99 struct timeval times[2];
9ce3cfc9 100 times[0].tv_sec = times[1].tv_sec = Itm.MTime;
246bbb61
DK
101 times[0].tv_usec = times[1].tv_usec = 0;
102 if (utimes(Itm.Name, times) != 0)
103 _error->Errno("utimes", "Failed to set modification time for %s",Itm.Name);
9ce3cfc9
DK
104
105 if (close(Fd) != 0)
106 return _error->Errno("close",_("Failed to close file %s"),Itm.Name);
107 return true;
b2e465d6
AL
108}
109 /*}}}*/
110// DirStream::Fail - Failed processing a file /*{{{*/
111// ---------------------------------------------------------------------
112/* */
113bool pkgDirStream::Fail(Item &Itm,int Fd)
114{
115 if (Fd < 0)
116 return true;
117
118 close(Fd);
119 return false;
120}
121 /*}}}*/