Corrected array handling
[ntk/apt.git] / apt-inst / dirstream.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: dirstream.cc,v 1.2 2001/02/20 07:03:16 jgg 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 #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>
27 /*}}}*/
28
29 // DirStream::DoItem - Process an item /*{{{*/
30 // ---------------------------------------------------------------------
31 /* This is a very simple extractor, it does not deal with things like
32 overwriting directories with files and so on. */
33 bool pkgDirStream::DoItem(Item &Itm,int &Fd)
34 {
35 switch (Itm.Type)
36 {
37 case Item::File:
38 {
39 /* Open the output file, NDELAY is used to prevent this from
40 blowing up on device special files.. */
41 int iFd = open(Itm.Name,O_NDELAY|O_WRONLY|O_CREAT|O_TRUNC|O_APPEND,
42 Itm.Mode);
43 if (iFd < 0)
44 return _error->Errno("open","Failed write file %s",
45 Itm.Name);
46
47 // fchmod deals with umask and fchown sets the ownership
48 if (fchmod(iFd,Itm.Mode) != 0)
49 return _error->Errno("fchmod","Failed write file %s",
50 Itm.Name);
51 if (fchown(iFd,Itm.UID,Itm.GID) != 0 && errno != EPERM)
52 return _error->Errno("fchown","Failed write file %s",
53 Itm.Name);
54 Fd = iFd;
55 return true;
56 }
57
58 case Item::HardLink:
59 case Item::SymbolicLink:
60 case Item::CharDevice:
61 case Item::BlockDevice:
62 case Item::Directory:
63 case Item::FIFO:
64 break;
65 }
66
67 return true;
68 }
69 /*}}}*/
70 // DirStream::FinishedFile - Finished processing a file /*{{{*/
71 // ---------------------------------------------------------------------
72 /* */
73 bool pkgDirStream::FinishedFile(Item &Itm,int Fd)
74 {
75 if (Fd < 0)
76 return true;
77
78 if (close(Fd) != 0)
79 return _error->Errno("close","Failed to close file %s",Itm.Name);
80
81 /* Set the modification times. The only way it can fail is if someone
82 has futzed with our file, which is intolerable :> */
83 struct utimbuf Time;
84 Time.actime = Itm.MTime;
85 Time.modtime = Itm.MTime;
86 if (utime(Itm.Name,&Time) != 0)
87 _error->Errno("utime","Failed to close file %s",Itm.Name);
88
89 return true;
90 }
91 /*}}}*/
92 // DirStream::Fail - Failed processing a file /*{{{*/
93 // ---------------------------------------------------------------------
94 /* */
95 bool pkgDirStream::Fail(Item &Itm,int Fd)
96 {
97 if (Fd < 0)
98 return true;
99
100 close(Fd);
101 return false;
102 }
103 /*}}}*/