]>
git.saurik.com Git - apt.git/blob - methods/copy.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: copy.cc,v 1.7.2.1 2004/01/16 18:58:50 mdz Exp $
4 /* ######################################################################
6 Copy URI - This method takes a uri like a file: uri and copies it
7 to the destination file.
9 ##################################################################### */
11 // Include Files /*{{{*/
14 #include <apt-pkg/fileutl.h>
15 #include <apt-pkg/strutl.h>
16 #include <apt-pkg/error.h>
17 #include <apt-pkg/hashes.h>
18 #include <apt-pkg/configuration.h>
19 #include "aptmethod.h"
28 class CopyMethod
: public aptMethod
30 virtual bool Fetch(FetchItem
*Itm
) APT_OVERRIDE
;
34 CopyMethod() : aptMethod("copy", "1.0",SingleInstance
| SendConfig
) {};
37 // CopyMethod::Fetch - Fetch a file /*{{{*/
38 // ---------------------------------------------------------------------
40 bool CopyMethod::Fetch(FetchItem
*Itm
)
42 // this ensures that relative paths work in copy
43 std::string
const File
= Itm
->Uri
.substr(Itm
->Uri
.find(':')+1);
45 // Stat the file and send a start message
47 if (stat(File
.c_str(),&Buf
) != 0)
48 return _error
->Errno("stat",_("Failed to stat"));
50 // Forumulate a result and send a start message
52 Res
.Size
= Buf
.st_size
;
53 Res
.Filename
= Itm
->DestFile
;
54 Res
.LastModified
= Buf
.st_mtime
;
58 // just calc the hashes if the source and destination are identical
59 if (File
== Itm
->DestFile
|| Itm
->DestFile
== "/dev/null")
61 CalculateHashes(Itm
, Res
);
66 // See if the file exists
67 FileFd
From(File
,FileFd::ReadOnly
);
68 FileFd
To(Itm
->DestFile
,FileFd::WriteAtomic
);
72 if (CopyFile(From
,To
) == false)
81 // Transfer the modification times
82 struct timeval times
[2];
83 times
[0].tv_sec
= Buf
.st_atime
;
84 times
[1].tv_sec
= Buf
.st_mtime
;
85 times
[0].tv_usec
= times
[1].tv_usec
= 0;
86 if (utimes(Res
.Filename
.c_str(), times
) != 0)
87 return _error
->Errno("utimes",_("Failed to set modification time"));
89 CalculateHashes(Itm
, Res
);
97 return CopyMethod().Run();