]>
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/acquire-method.h>
17 #include <apt-pkg/error.h>
18 #include <apt-pkg/hashes.h>
19 #include <apt-pkg/configuration.h>
28 class CopyMethod
: public pkgAcqMethod
30 virtual bool Fetch(FetchItem
*Itm
);
31 void CalculateHashes(FetchResult
&Res
);
35 CopyMethod() : pkgAcqMethod("1.0",SingleInstance
| SendConfig
) {};
38 void CopyMethod::CalculateHashes(FetchResult
&Res
)
40 // For gzip indexes we need to look inside the gzip for the hash
41 // We can not use the extension here as its not used in partial
43 FileFd::OpenMode OpenMode
= FileFd::ReadOnly
;
44 if (_config
->FindB("Acquire::GzipIndexes", false) == true)
45 OpenMode
= FileFd::ReadOnlyGzip
;
48 FileFd
Fd(Res
.Filename
, OpenMode
);
53 // CopyMethod::Fetch - Fetch a file /*{{{*/
54 // ---------------------------------------------------------------------
56 bool CopyMethod::Fetch(FetchItem
*Itm
)
58 // this ensures that relative paths work in copy
59 std::string File
= Itm
->Uri
.substr(Itm
->Uri
.find(':')+1);
61 // Stat the file and send a start message
63 if (stat(File
.c_str(),&Buf
) != 0)
64 return _error
->Errno("stat",_("Failed to stat"));
66 // Forumulate a result and send a start message
68 Res
.Size
= Buf
.st_size
;
69 Res
.Filename
= Itm
->DestFile
;
70 Res
.LastModified
= Buf
.st_mtime
;
74 // just calc the hashes if the source and destination are identical
75 if (File
== Itm
->DestFile
)
82 // See if the file exists
83 FileFd
From(File
,FileFd::ReadOnly
);
84 FileFd
To(Itm
->DestFile
,FileFd::WriteAtomic
);
86 if (_error
->PendingError() == true)
93 if (CopyFile(From
,To
) == false)
102 // Transfer the modification times
103 struct timeval times
[2];
104 times
[0].tv_sec
= Buf
.st_atime
;
105 times
[1].tv_sec
= Buf
.st_mtime
;
106 times
[0].tv_usec
= times
[1].tv_usec
= 0;
107 if (utimes(Res
.Filename
.c_str(), times
) != 0)
108 return _error
->Errno("utimes",_("Failed to set modification time"));
110 CalculateHashes(Res
);
119 setlocale(LC_ALL
, "");