]>
git.saurik.com Git - apt.git/blob - methods/copy.cc
e515b2def10e1eed04abd89709915703470ec684
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/acquire-method.h>
17 #include <apt-pkg/error.h>
18 #include <apt-pkg/hashes.h>
19 #include <apt-pkg/configuration.h>
20 #include "aptmethod.h"
29 class CopyMethod
: public aptMethod
31 virtual bool Fetch(FetchItem
*Itm
) APT_OVERRIDE
;
35 CopyMethod() : aptMethod("copy", "1.0",SingleInstance
| SendConfig
) {};
38 // CopyMethod::Fetch - Fetch a file /*{{{*/
39 // ---------------------------------------------------------------------
41 bool CopyMethod::Fetch(FetchItem
*Itm
)
43 // this ensures that relative paths work in copy
44 std::string
const File
= Itm
->Uri
.substr(Itm
->Uri
.find(':')+1);
46 // Stat the file and send a start message
48 if (stat(File
.c_str(),&Buf
) != 0)
49 return _error
->Errno("stat",_("Failed to stat"));
51 // Forumulate a result and send a start message
53 Res
.Size
= Buf
.st_size
;
54 Res
.Filename
= Itm
->DestFile
;
55 Res
.LastModified
= Buf
.st_mtime
;
59 // just calc the hashes if the source and destination are identical
60 if (File
== Itm
->DestFile
|| Itm
->DestFile
== "/dev/null")
62 CalculateHashes(Itm
, Res
);
67 // See if the file exists
68 FileFd
From(File
,FileFd::ReadOnly
);
69 FileFd
To(Itm
->DestFile
,FileFd::WriteAtomic
);
73 if (CopyFile(From
,To
) == false)
82 // Transfer the modification times
83 struct timeval times
[2];
84 times
[0].tv_sec
= Buf
.st_atime
;
85 times
[1].tv_sec
= Buf
.st_mtime
;
86 times
[0].tv_usec
= times
[1].tv_usec
= 0;
87 if (utimes(Res
.Filename
.c_str(), times
) != 0)
88 return _error
->Errno("utimes",_("Failed to set modification time"));
90 CalculateHashes(Itm
, Res
);
98 setlocale(LC_ALL
, "");