Fixed pthread test
[ntk/apt.git] / methods / copy.cc
CommitLineData
561ab0db
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
8b89e57f 3// $Id: copy.cc,v 1.3 1998/10/26 07:11:52 jgg Exp $
561ab0db
AL
4/* ######################################################################
5
6 Copy URI - This method takes a uri like a file: uri and copies it
92173b19 7 to the destination file.
561ab0db
AL
8
9 ##################################################################### */
10 /*}}}*/
11// Include Files /*{{{*/
12#include <apt-pkg/fileutl.h>
13#include <strutl.h>
14#include <apt-pkg/error.h>
15
16#include <sys/stat.h>
92173b19 17#include <utime.h>
561ab0db
AL
18#include <unistd.h>
19#include <stdio.h>
20 /*}}}*/
21
22// Fail - Generate a failure message /*{{{*/
23// ---------------------------------------------------------------------
24/* */
25void Fail(string URI)
26{
27 string Err = "Undetermined Error";
28 if (_error->empty() == false)
29 _error->PopMessage(Err);
30
31 printf("400 URI Failure\n"
32 "URI: %s\n"
33 "Message: %s\n\n",URI.c_str(),Err.c_str());
34 _error->Discard();
35}
36 /*}}}*/
37
38int main()
39{
40 setlinebuf(stdout);
41 SetNonBlock(STDIN_FILENO,true);
42
43 printf("100 Capabilities\n"
44 "Version: 1.0\n"
45 "Pipeline: true\n\n");
46
47 vector<string> Messages;
48 while (1)
49 {
50 if (WaitFd(STDIN_FILENO) == false ||
51 ReadMessages(STDIN_FILENO,Messages) == false)
52 return 0;
53
54 while (Messages.empty() == false)
55 {
56 string Message = Messages.front();
57 Messages.erase(Messages.begin());
58
59 // Fetch the message number
60 char *End;
61 int Number = strtol(Message.c_str(),&End,10);
62 if (End == Message.c_str())
63 {
64 cerr << "Malformed message!" << endl;
65 return 100;
66 }
67
68 // We only understand 600 URI Fetch messages
69 if (Number != 600)
70 continue;
71
72 // Grab the URI bit
73 string URI = LookupTag(Message,"URI");
74 string Target = LookupTag(Message,"Filename");
75
76 // Grab the filename
77 string::size_type Pos = URI.find(':');
78 if (Pos == string::npos)
79 {
80 _error->Error("Invalid message");
81 Fail(URI);
82 continue;
83 }
84 string File = string(URI,Pos+1);
85
86 // Start the reply message
87 string Result = "201 URI Done";
88 Result += "\nURI: " + URI;
89 Result += "\nFileName: " + Target;
90
91 // See if the file exists
92 FileFd From(File,FileFd::ReadOnly);
93 FileFd To(Target,FileFd::WriteEmpty);
94 To.EraseOnFailure();
95 if (_error->PendingError() == true)
96 {
97 Fail(URI);
98 continue;
99 }
100
101 // Copy the file
102 if (CopyFile(From,To) == false)
103 {
104 Fail(URI);
105 continue;
106 }
92173b19
AL
107
108 From.Close();
109 To.Close();
561ab0db 110
92173b19
AL
111 // Transfer the modification times
112 struct stat Buf;
113 if (stat(File.c_str(),&Buf) != 0)
114 {
115 _error->Errno("stat","Failed to stat");
116 Fail(URI);
117 continue;
118 }
119 struct utimbuf TimeBuf;
120 TimeBuf.actime = Buf.st_atime;
121 TimeBuf.modtime = Buf.st_mtime;
122 if (utime(Target.c_str(),&TimeBuf) != 0)
123 {
8b89e57f 124 To.OpFail();
92173b19
AL
125 _error->Errno("utime","Failed to set modification time");
126 Fail(URI);
127 continue;
128 }
129
561ab0db
AL
130 // Send the message
131 Result += "\n\n";
132 if (write(STDOUT_FILENO,Result.begin(),Result.length()) !=
133 (signed)Result.length())
134 return 100;
135 }
136 }
137
138 return 0;
139}