]>
git.saurik.com Git - apt.git/blob - methods/file.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: file.cc,v 1.9.2.1 2004/01/16 18:58:50 mdz Exp $
4 /* ######################################################################
6 File URI method for APT
8 This simply checks that the file specified exists, if so the relevant
9 information is returned. If a .gz filename is specified then the file
10 name with .gz removed will also be checked and information about it
11 will be returned in Alt-*
13 ##################################################################### */
15 // Include Files /*{{{*/
18 #include <apt-pkg/acquire-method.h>
19 #include <apt-pkg/aptconfiguration.h>
20 #include <apt-pkg/error.h>
21 #include <apt-pkg/hashes.h>
22 #include <apt-pkg/fileutl.h>
23 #include <apt-pkg/strutl.h>
31 class FileMethod
: public pkgAcqMethod
33 virtual bool Fetch(FetchItem
*Itm
) APT_OVERRIDE
;
34 virtual bool Configuration(std::string Message
) APT_OVERRIDE
;
38 FileMethod() : pkgAcqMethod("1.0",SingleInstance
| SendConfig
| LocalOnly
) {};
40 bool FileMethod::Configuration(std::string Message
)
42 if (pkgAcqMethod::Configuration(Message
) == false)
50 // FileMethod::Fetch - Fetch a file /*{{{*/
51 // ---------------------------------------------------------------------
53 bool FileMethod::Fetch(FetchItem
*Itm
)
56 std::string File
= Get
.Path
;
58 if (Get
.Host
.empty() == false)
59 return _error
->Error(_("Invalid URI, local URIS must not start with //"));
62 // deal with destination files which might linger around
63 if (lstat(Itm
->DestFile
.c_str(), &Buf
) == 0)
65 if ((Buf
.st_mode
& S_IFREG
) != 0)
67 if (Itm
->LastModified
== Buf
.st_mtime
&& Itm
->LastModified
!= 0)
69 HashStringList
const hsl
= Itm
->ExpectedHashes
;
70 if (Itm
->ExpectedHashes
.VerifyFile(File
))
72 Res
.Filename
= Itm
->DestFile
;
78 if (Res
.IMSHit
!= true)
79 unlink(Itm
->DestFile
.c_str());
81 // See if the file exists
82 if (stat(File
.c_str(),&Buf
) == 0)
84 Res
.Size
= Buf
.st_size
;
86 Res
.LastModified
= Buf
.st_mtime
;
88 if (Itm
->LastModified
== Buf
.st_mtime
&& Itm
->LastModified
!= 0)
90 unsigned long long const filesize
= Itm
->ExpectedHashes
.FileSize();
91 if (filesize
!= 0 && filesize
== Res
.Size
)
95 Hashes
Hash(Itm
->ExpectedHashes
);
96 FileFd
Fd(File
, FileFd::ReadOnly
);
100 if (Res
.IMSHit
== false)
103 // See if the uncompressed file exists and reuse it
105 AltRes
.Filename
.clear();
106 std::vector
<std::string
> extensions
= APT::Configuration::getCompressorExtensions();
107 for (std::vector
<std::string
>::const_iterator ext
= extensions
.begin(); ext
!= extensions
.end(); ++ext
)
109 if (APT::String::Endswith(File
, *ext
) == true)
111 std::string
const unfile
= File
.substr(0, File
.length() - ext
->length() - 1);
112 if (stat(unfile
.c_str(),&Buf
) == 0)
114 AltRes
.Size
= Buf
.st_size
;
115 AltRes
.Filename
= unfile
;
116 AltRes
.LastModified
= Buf
.st_mtime
;
117 AltRes
.IMSHit
= false;
118 if (Itm
->LastModified
== Buf
.st_mtime
&& Itm
->LastModified
!= 0)
119 AltRes
.IMSHit
= true;
122 // no break here as we could have situations similar to '.gz' vs '.tar.gz' here
126 if (AltRes
.Filename
.empty() == false)
127 URIDone(Res
,&AltRes
);
128 else if (Res
.Filename
.empty() == false)
131 return _error
->Error(_("File not found"));
139 setlocale(LC_ALL
, "");