]> git.saurik.com Git - apt.git/blame - methods/copy.cc
Reorderd error handling
[apt.git] / methods / copy.cc
CommitLineData
561ab0db
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
91cb4c6b 3// $Id: copy.cc,v 1.6 1999/01/20 04:36:43 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
91cb4c6b
AL
38 // Stat the file and send a start message
39 struct stat Buf;
40 if (stat(File.c_str(),&Buf) != 0)
41 return _error->Errno("stat","Failed to stat");
42
43 // Forumulate a result and send a start message
44 FetchResult Res;
45 Res.Size = Buf.st_size;
46 Res.Filename = Itm->DestFile;
47 Res.LastModified = Buf.st_mtime;
48 Res.IMSHit = false;
49 URIStart(Res);
50
93bf083d
AL
51 // See if the file exists
52 FileFd From(File,FileFd::ReadOnly);
be4401bf 53 FileFd To(Itm->DestFile,FileFd::WriteEmpty);
93bf083d
AL
54 To.EraseOnFailure();
55 if (_error->PendingError() == true)
91cb4c6b
AL
56 {
57 To.OpFail();
93bf083d 58 return false;
91cb4c6b 59 }
561ab0db 60
93bf083d
AL
61 // Copy the file
62 if (CopyFile(From,To) == false)
91cb4c6b
AL
63 {
64 To.OpFail();
93bf083d 65 return false;
91cb4c6b 66 }
561ab0db 67
93bf083d
AL
68 From.Close();
69 To.Close();
561ab0db 70
93bf083d 71 // Transfer the modification times
93bf083d
AL
72 struct utimbuf TimeBuf;
73 TimeBuf.actime = Buf.st_atime;
74 TimeBuf.modtime = Buf.st_mtime;
be4401bf 75 if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
93bf083d
AL
76 {
77 To.OpFail();
78 return _error->Errno("utime","Failed to set modification time");
79 }
80
93bf083d
AL
81 URIDone(Res);
82 return true;
83}
84 /*}}}*/
85
86int main()
87{
88 CopyMethod Mth;
89 return Mth.Run();
561ab0db 90}