libzypp: zypp-curl/parser/metadatahelper.cc Source File

libzypp 17.38.6

metadatahelper.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
9#include "metadatahelper.h"
10
11#include <zypp-core/base/IOTools.h>
12#include <memory>
13
14namespace zypp::media {
15
16 MetaDataType looks_like_meta_data(const std::vector<char> &data)
17 {
18 if ( data.empty() )
19 return MetaDataType::None;
20
21 const char *p = data.data();
22 while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')
23 p++;
24
25 // If we have a zsync file, it has to start with zsync:
26 if ( !strncasecmp( p, "zsync:", 6 ) ) {
27 return MetaDataType::Zsync;
28 }
29
30 if (!strncasecmp(p, "<?xml", 5))
31 {
32 while (*p && *p != '>')
33 p++;
34 if (*p == '>')
35 p++;
36 while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')
37 p++;
38 }
39 bool ret = !strncasecmp( p, "<metalink", 9 ) ? true : false;
40 if ( ret )
41 return MetaDataType::MetaLink;
42
43 return MetaDataType::None;
44 }
45
46 MetaDataType looks_like_meta_file(const Pathname &file)
47 {
48 std::unique_ptr<FILE, int(*)(FILE *)> fd( fopen( file.c_str(), "r" ), &fclose );
49 return looks_like_meta_file( fd.get() );
50 }
51
52 MetaDataType looks_like_meta_file(FILE *file)
53 {
54 if ( !file )
55 return MetaDataType::None;
56 return looks_like_meta_data( io::peek_data_fd( file, 0, minMetalinkProbeSize ) );
57 }
58
59}
IOTools.h
zypp::filesystem::Pathname
Pathname.
Definition Pathname.h:48
zypp::filesystem::Pathname::c_str
const char * c_str() const
String representation.
Definition Pathname.h:113
metadatahelper.h
zypp::io::peek_data_fd
std::vector< char > peek_data_fd(FILE *fd, off_t offset, size_t count)
Definition IOTools.cc:171
zypp::media
Definition MediaNetworkRequestExecutor.h:22
zypp::media::looks_like_meta_data
MetaDataType looks_like_meta_data(const std::vector< char > &data)
Definition metadatahelper.cc:16
zypp::media::MetaDataType
MetaDataType
Definition metadatahelper.h:24
zypp::media::MetaDataType::None
@ None
Definition metadatahelper.h:25
zypp::media::MetaDataType::MetaLink
@ MetaLink
Definition metadatahelper.h:27
zypp::media::MetaDataType::Zsync
@ Zsync
Definition metadatahelper.h:26
zypp::media::minMetalinkProbeSize
constexpr auto minMetalinkProbeSize
Definition metadatahelper.h:30
zypp::media::looks_like_meta_file
MetaDataType looks_like_meta_file(const Pathname &file)
Definition metadatahelper.cc:46