]> git.saurik.com Git - apt.git/blob - methods/copy.cc
94e88e503df3896597caa2dd02958dd056a8cd74
[apt.git] / methods / copy.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: copy.cc,v 1.5 1998/11/01 05:27:40 jgg Exp $
4 /* ######################################################################
5
6 Copy URI - This method takes a uri like a file: uri and copies it
7 to the destination file.
8
9 ##################################################################### */
10 /*}}}*/
11 // Include Files /*{{{*/
12 #include <apt-pkg/fileutl.h>
13 #include <apt-pkg/acquire-method.h>
14 #include <apt-pkg/error.h>
15
16 #include <sys/stat.h>
17 #include <utime.h>
18 #include <unistd.h>
19 /*}}}*/
20
21 class CopyMethod : public pkgAcqMethod
22 {
23 virtual bool Fetch(FetchItem *Itm);
24
25 public:
26
27 CopyMethod() : pkgAcqMethod("1.0",SingleInstance) {};
28 };
29
30 // CopyMethod::Fetch - Fetch a file /*{{{*/
31 // ---------------------------------------------------------------------
32 /* */
33 bool CopyMethod::Fetch(FetchItem *Itm)
34 {
35 URI Get = Itm->Uri;
36 string File = Get.Path;
37
38 // See if the file exists
39 FileFd From(File,FileFd::ReadOnly);
40 FileFd To(Itm->DestFile,FileFd::WriteEmpty);
41 To.EraseOnFailure();
42 if (_error->PendingError() == true)
43 return false;
44
45 // Copy the file
46 if (CopyFile(From,To) == false)
47 return false;
48
49 From.Close();
50 To.Close();
51
52 // Transfer the modification times
53 struct stat Buf;
54 if (stat(File.c_str(),&Buf) != 0)
55 {
56 To.OpFail();
57 return _error->Errno("stat","Failed to stat");
58 }
59
60 struct utimbuf TimeBuf;
61 TimeBuf.actime = Buf.st_atime;
62 TimeBuf.modtime = Buf.st_mtime;
63 if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
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;
72 Res.Filename = Itm->DestFile;
73 Res.LastModified = Buf.st_mtime;
74 Res.IMSHit = false;
75
76 URIDone(Res);
77 return true;
78 }
79 /*}}}*/
80
81 int main()
82 {
83 CopyMethod Mth;
84 return Mth.Run();
85 }