Imported Upstream version 14.45.0
[platform/upstream/libzypp.git] / devel / devel.dmacvicar / zsync.cc
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 extern "C" {
5 #include <zsync.h>
6 }
7 #include "zypp/base/Exception.h"
8 #include "zypp/base/Logger.h"
9 #include "zypp/Pathname.h"
10 #include "zypp/ExternalProgram.cc"
11 //#include 
12
13 using namespace zypp;
14 using namespace std;
15
16 void read_seed_file(struct zsync_state* z, const Pathname &path )
17 {
18   if (zsync_hint_decompress(z) && path.basename().size() > 3 && path.extension() == ".gz" )
19   {
20     FILE* f;
21     {
22       // ugh
23       char* cmd = (char *) malloc(6 + strlen(path.c_str())*2);
24
25       if (!cmd) return;
26
27       const char *fname = path.c_str();
28       strcpy(cmd,"zcat ");
29       {
30         int i,j;
31         for (i=0,j=5; fname[i]; i++)
32         {
33           if (!isalnum(fname[i])) cmd[j++] = '\\';
34             cmd[j++] = fname[i];
35         }
36         cmd[j] = 0;
37       }
38
39       //if (!no_progress) fprintf(stderr,"reading seed %s: ",cmd);
40       MIL << "Reading seed " << cmd << endl;
41       f = popen(cmd,"r");
42       free(cmd);
43     }
44
45     if (!f)
46     {
47       //perror("popen"); fprintf(stderr,"not using seed file %s\n",fname);
48       ZYPP_THROW(Exception("not using seed file"));
49     }
50     else
51     {
52       // 0 no progress
53       zsync_submit_source_file(z, f, 0);
54       if (pclose(f) != 0)
55       {
56         ZYPP_THROW(Exception("pclose"));
57         perror("close");
58       }
59     }
60   }
61   else
62   {
63     FILE* f = fopen(path.c_str(),"r");
64     MIL << "Reading seed " << path << endl;
65     if (!f) {
66       //perror("open"); fprintf(stderr,"not using seed file %s\n",fname);
67       ZYPP_THROW(Exception("open: " + path.asString()));
68     }
69     else
70     {
71       // 0 no progress
72       zsync_submit_source_file(z, f, 0);
73       if (fclose(f) != 0)
74       {
75         perror("close");
76       }
77     }
78   }
79   {
80     long long done,total;
81     zsync_progress(z, &done, &total);
82     MIL << "Read " << path << ". Target " << (100.0f * done)/total << " complete" << endl;
83   }
84 }
85
86 void figure_ranges(struct zsync_state* zs)
87 {
88   //struct zsync_receiver* zr;
89   int num_ranges;
90   // it seems type is 1 for gz, 0 normal
91   off_t *ranges = zsync_needed_byte_ranges(zs, &num_ranges, 0);
92   int i=0;
93
94   MIL << "Need to get " << num_ranges << " ranges" << endl;
95
96   while ( i < 2*num_ranges )
97   {
98     int from = ranges[i];
99     MIL << "From: " << ranges[i] << " To: " << ranges[i+1] << endl;
100     i += 2;
101   }
102
103   free(ranges);
104 }
105
106 int main()
107 {
108   Pathname root("/home/duncan/suse/metadata-diff");
109   struct zsync_state* zs;
110
111   FILE *f = fopen( (root+"/3/packages.zsync").c_str(), "r" );
112
113   if ((zs = zsync_begin(f)) == NULL)
114   {
115     exit(1);
116   }
117   
118   if (fclose(f) != 0)
119   {
120     perror("fclose"); exit(2);
121   }
122
123   read_seed_file( zs, root + "1/packages" );
124   figure_ranges(zs);
125
126   zsync_end(zs);
127   return 0;
128 }