New http method
[ntk/apt.git] / methods / copy.cc
CommitLineData
561ab0db
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
be4401bf 3// $Id: copy.cc,v 1.5 1998/11/01 05:27:40 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>
93bf083d 13#include <apt-pkg/acquire-method.h>
561ab0db
AL
14#include <apt-pkg/error.h>
15
16#include <sys/stat.h>
92173b19 17#include <utime.h>
561ab0db 18#include <unistd.h>
561ab0db
AL
19 /*}}}*/
20
93bf083d
AL
21class CopyMethod : public pkgAcqMethod
22{
be4401bf 23 virtual bool Fetch(FetchItem *Itm);
93bf083d
AL
24
25 public:
26
27 CopyMethod() : pkgAcqMethod("1.0",SingleInstance) {};
28};
29
30// CopyMethod::Fetch - Fetch a file /*{{{*/
561ab0db
AL
31// ---------------------------------------------------------------------
32/* */
be4401bf 33bool CopyMethod::Fetch(FetchItem *Itm)
561ab0db 34{
be4401bf 35 URI Get = Itm->Uri;
93bf083d
AL
36 string File = Get.Path;
37
38 // See if the file exists
39 FileFd From(File,FileFd::ReadOnly);
be4401bf 40 FileFd To(Itm->DestFile,FileFd::WriteEmpty);
93bf083d
AL
41 To.EraseOnFailure();
42 if (_error->PendingError() == true)
43 return false;
561ab0db 44
93bf083d
AL
45 // Copy the file
46 if (CopyFile(From,To) == false)
47 return false;
561ab0db 48
93bf083d
AL
49 From.Close();
50 To.Close();
561ab0db 51
93bf083d
AL
52 // Transfer the modification times
53 struct stat Buf;
54 if (stat(File.c_str(),&Buf) != 0)
561ab0db 55 {
93bf083d
AL
56 To.OpFail();
57 return _error->Errno("stat","Failed to stat");
561ab0db
AL
58 }
59
93bf083d
AL
60 struct utimbuf TimeBuf;
61 TimeBuf.actime = Buf.st_atime;
62 TimeBuf.modtime = Buf.st_mtime;
be4401bf 63 if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
93bf083d
AL
64 {
65 To.OpFail();
66 return _error->Errno("utime","Failed to set modification time");
67 }
68
69 // Forumulate a result
70 FetchResult Res;
71 Res.Size = Buf.st_size;
be4401bf 72 Res.Filename = Itm->DestFile;
93bf083d
AL
73 Res.LastModified = Buf.st_mtime;
74 Res.IMSHit = false;
75
76 URIDone(Res);
77 return true;
78}
79 /*}}}*/
80
81int main()
82{
83 CopyMethod Mth;
84 return Mth.Run();
561ab0db 85}