-
Notifications
You must be signed in to change notification settings - Fork 432
/
arbiter.cpp
4939 lines (3977 loc) · 126 KB
/
arbiter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/// Arbiter amalgamated source (https://github.com/connormanning/arbiter).
/// It is intended to be used with #include "arbiter.hpp"
// //////////////////////////////////////////////////////////////////////
// Beginning of content of file: LICENSE
// //////////////////////////////////////////////////////////////////////
/*
The MIT License (MIT)
Copyright (c) 2015 Connor Manning
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// //////////////////////////////////////////////////////////////////////
// End of content of file: LICENSE
// //////////////////////////////////////////////////////////////////////
#include "arbiter.hpp"
#ifndef ARBITER_IS_AMALGAMATION
#error "Compile with -I PATH_TO_ARBITER_DIRECTORY"
#endif
// //////////////////////////////////////////////////////////////////////
// Beginning of content of file: arbiter/arbiter.cpp
// //////////////////////////////////////////////////////////////////////
#ifndef ARBITER_IS_AMALGAMATION
#include <arbiter/arbiter.hpp>
#include <arbiter/driver.hpp>
#include <arbiter/util/sha256.hpp>
#include <arbiter/util/json.hpp>
#include <arbiter/util/transforms.hpp>
#include <arbiter/util/util.hpp>
#endif
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <sstream>
#ifdef ARBITER_CUSTOM_NAMESPACE
namespace ARBITER_CUSTOM_NAMESPACE
{
#endif
namespace arbiter
{
namespace
{
const std::string delimiter("://");
#ifdef ARBITER_CURL
const std::size_t concurrentHttpReqs(32);
const std::size_t httpRetryCount(8);
#endif
json getConfig(const std::string& s)
{
json in(s.size() ? json::parse(s) : json::object());
json config;
std::string path("~/.arbiter/config.json");
if (auto p = env("ARBITER_CONFIG_FILE")) path = *p;
else if (auto p = env("ARBITER_CONFIG_PATH")) path = *p;
if (auto data = drivers::Fs().tryGet(path)) config = json::parse(*data);
if (in.is_null()) in = json::object();
if (config.is_null()) config = json::object();
return merge(in, config);
}
inline bool iequals(const std::string& s, const std::string& s2)
{
if (s.length() != s2.length())
return false;
for (size_t i = 0; i < s.length(); ++i)
if (std::toupper(s[i]) != std::toupper(s2[i]))
return false;
return true;
}
inline bool findEntry(const StringMap& map, const std::string& key, std::string& val)
{
for (auto& p : map)
if (iequals(p.first, key))
{
val = p.second;
return true;
}
return false;
}
}
Arbiter::Arbiter() : Arbiter(json().dump()) { }
Arbiter::Arbiter(const std::string s)
: m_drivers()
#ifdef ARBITER_CURL
, m_pool(
new http::Pool(
concurrentHttpReqs,
httpRetryCount,
getConfig(s).dump()))
#endif
{
using namespace drivers;
const json c(getConfig(s));
if (auto d = Fs::create())
{
m_drivers[d->type()] = std::move(d);
}
if (auto d = Test::create())
{
m_drivers[d->type()] = std::move(d);
}
#ifdef ARBITER_CURL
if (auto d = Http::create(*m_pool))
{
m_drivers[d->type()] = std::move(d);
}
if (auto d = Https::create(*m_pool))
{
m_drivers[d->type()] = std::move(d);
}
{
auto dlist(S3::create(*m_pool, c.value("s3", json()).dump()));
for (auto& d : dlist) m_drivers[d->type()] = std::move(d);
}
// Credential-based drivers should probably all do something similar to the
// S3 driver to support multiple profiles.
if (auto d = Dropbox::create(*m_pool, c.value("dropbox", json()).dump()))
{
m_drivers[d->type()] = std::move(d);
}
#ifdef ARBITER_OPENSSL
if (auto d = Google::create(*m_pool, c.value("gs", json()).dump()))
{
m_drivers[d->type()] = std::move(d);
}
#endif
#endif
}
bool Arbiter::hasDriver(const std::string path) const
{
return m_drivers.count(getProtocol(path));
}
void Arbiter::addDriver(const std::string type, std::unique_ptr<Driver> driver)
{
if (!driver) throw ArbiterError("Cannot add empty driver for " + type);
m_drivers[type] = std::move(driver);
}
std::string Arbiter::get(const std::string path) const
{
return getDriver(path).get(stripProtocol(path));
}
std::vector<char> Arbiter::getBinary(const std::string path) const
{
return getDriver(path).getBinary(stripProtocol(path));
}
std::unique_ptr<std::string> Arbiter::tryGet(std::string path) const
{
return getDriver(path).tryGet(stripProtocol(path));
}
std::unique_ptr<std::vector<char>> Arbiter::tryGetBinary(std::string path) const
{
return getDriver(path).tryGetBinary(stripProtocol(path));
}
std::size_t Arbiter::getSize(const std::string path) const
{
return getDriver(path).getSize(stripProtocol(path));
}
std::unique_ptr<std::size_t> Arbiter::tryGetSize(const std::string path) const
{
return getDriver(path).tryGetSize(stripProtocol(path));
}
void Arbiter::put(const std::string path, const std::string& data) const
{
return getDriver(path).put(stripProtocol(path), data);
}
void Arbiter::put(const std::string path, const std::vector<char>& data) const
{
return getDriver(path).put(stripProtocol(path), data);
}
std::string Arbiter::get(
const std::string path,
const http::Headers headers,
const http::Query query) const
{
return getHttpDriver(path).get(stripProtocol(path), headers, query);
}
std::unique_ptr<std::string> Arbiter::tryGet(
const std::string path,
const http::Headers headers,
const http::Query query) const
{
return getHttpDriver(path).tryGet(stripProtocol(path), headers, query);
}
std::vector<char> Arbiter::getBinary(
const std::string path,
const http::Headers headers,
const http::Query query) const
{
return getHttpDriver(path).getBinary(stripProtocol(path), headers, query);
}
std::unique_ptr<std::vector<char>> Arbiter::tryGetBinary(
const std::string path,
const http::Headers headers,
const http::Query query) const
{
return getHttpDriver(path).tryGetBinary(stripProtocol(path), headers, query);
}
void Arbiter::put(
const std::string path,
const std::string& data,
const http::Headers headers,
const http::Query query) const
{
return getHttpDriver(path).put(stripProtocol(path), data, headers, query);
}
void Arbiter::put(
const std::string path,
const std::vector<char>& data,
const http::Headers headers,
const http::Query query) const
{
return getHttpDriver(path).put(stripProtocol(path), data, headers, query);
}
void Arbiter::copy(
const std::string src,
const std::string dst,
const bool verbose) const
{
if (src.empty()) throw ArbiterError("Cannot copy from empty source");
if (dst.empty()) throw ArbiterError("Cannot copy to empty destination");
// Globify the source path if it's a directory. In this case, the source
// already ends with a slash.
const std::string srcToResolve(src + (isDirectory(src) ? "**" : ""));
if (srcToResolve.back() != '*')
{
// The source is a single file.
copyFile(src, dst, verbose);
}
else
{
// We'll need this to mirror the directory structure in the output.
// All resolved paths will contain this common prefix, so we can
// determine any nested paths from recursive resolutions by stripping
// that common portion.
const Endpoint& srcEndpoint(getEndpoint(stripPostfixing(src)));
const std::string commonPrefix(srcEndpoint.prefixedRoot());
const Endpoint dstEndpoint(getEndpoint(dst));
if (srcEndpoint.prefixedRoot() == dstEndpoint.prefixedRoot())
{
throw ArbiterError("Cannot copy directory to itself");
}
int i(0);
const auto paths(resolve(srcToResolve, verbose));
for (const auto& path : paths)
{
const std::string subpath(path.substr(commonPrefix.size()));
if (verbose)
{
std::cout <<
++i << " / " << paths.size() << ": " <<
path << " -> " << dstEndpoint.prefixedFullPath(subpath) <<
std::endl;
}
if (dstEndpoint.isLocal())
{
mkdirp(getDirname(dstEndpoint.fullPath(subpath)));
}
dstEndpoint.put(subpath, getBinary(path));
}
}
}
void Arbiter::copyFile(
const std::string file,
std::string dst,
const bool verbose) const
{
if (dst.empty()) throw ArbiterError("Cannot copy to empty destination");
const Endpoint dstEndpoint(getEndpoint(dst));
if (isDirectory(dst))
{
// If the destination is a directory, maintain the basename of the
// source file.
dst += getBasename(file);
}
if (verbose) std::cout << file << " -> " << dst << std::endl;
if (dstEndpoint.isLocal()) mkdirp(getDirname(dst));
if (getEndpoint(file).type() == dstEndpoint.type())
{
// If this copy is within the same driver domain, defer to the
// hopefully specialized copy method.
getDriver(file).copy(stripProtocol(file), stripProtocol(dst));
}
else
{
// Otherwise do a GET/PUT for the copy.
put(dst, getBinary(file));
}
}
bool Arbiter::isRemote(const std::string path) const
{
return getDriver(path).isRemote();
}
bool Arbiter::isLocal(const std::string path) const
{
return !isRemote(path);
}
bool Arbiter::exists(const std::string path) const
{
return tryGetSize(path).get() != nullptr;
}
bool Arbiter::isHttpDerived(const std::string path) const
{
return tryGetHttpDriver(path) != nullptr;
}
std::vector<std::string> Arbiter::resolve(
const std::string path,
const bool verbose) const
{
return getDriver(path).resolve(stripProtocol(path), verbose);
}
Endpoint Arbiter::getEndpoint(const std::string root) const
{
return Endpoint(getDriver(root), stripProtocol(root));
}
const Driver& Arbiter::getDriver(const std::string path) const
{
const auto type(getProtocol(path));
if (!m_drivers.count(type))
{
throw ArbiterError("No driver for " + path);
}
return *m_drivers.at(type);
}
const drivers::Http* Arbiter::tryGetHttpDriver(const std::string path) const
{
return dynamic_cast<const drivers::Http*>(&getDriver(path));
}
const drivers::Http& Arbiter::getHttpDriver(const std::string path) const
{
if (auto d = tryGetHttpDriver(path)) return *d;
else throw ArbiterError("Cannot get driver for " + path + " as HTTP");
}
LocalHandle Arbiter::getLocalHandle(
const std::string path,
const Endpoint& tempEndpoint) const
{
const Endpoint fromEndpoint(getEndpoint(getDirname(path)));
return fromEndpoint.getLocalHandle(getBasename(path));
}
LocalHandle Arbiter::getLocalHandle(
const std::string path,
std::string tempPath) const
{
if (tempPath.empty()) tempPath = getTempPath();
return getLocalHandle(path, getEndpoint(tempPath));
}
LocalHandle Arbiter::getLocalHandle(
const std::string path,
http::Headers headers,
http::Query query) const
{
const Endpoint fromEndpoint(getEndpoint(getDirname(path)));
return fromEndpoint.getLocalHandle(getBasename(path), headers, query);
}
} // namespace arbiter
#ifdef ARBITER_CUSTOM_NAMESPACE
}
#endif
// //////////////////////////////////////////////////////////////////////
// End of content of file: arbiter/arbiter.cpp
// //////////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////
// Beginning of content of file: arbiter/driver.cpp
// //////////////////////////////////////////////////////////////////////
#ifndef ARBITER_IS_AMALGAMATION
#include <arbiter/driver.hpp>
#include <arbiter/arbiter.hpp>
#endif
#ifdef ARBITER_CUSTOM_NAMESPACE
namespace ARBITER_CUSTOM_NAMESPACE
{
#endif
namespace arbiter
{
std::string Driver::get(const std::string path) const
{
const std::vector<char> data(getBinary(path));
return std::string(data.begin(), data.end());
}
std::unique_ptr<std::string> Driver::tryGet(const std::string path) const
{
std::unique_ptr<std::string> result;
std::unique_ptr<std::vector<char>> data(tryGetBinary(path));
if (data) result.reset(new std::string(data->begin(), data->end()));
return result;
}
std::vector<char> Driver::getBinary(std::string path) const
{
std::vector<char> data;
if (!get(path, data)) throw ArbiterError("Could not read file " + path);
return data;
}
std::unique_ptr<std::vector<char>> Driver::tryGetBinary(std::string path) const
{
std::unique_ptr<std::vector<char>> data(new std::vector<char>());
if (!get(path, *data)) data.reset();
return data;
}
std::size_t Driver::getSize(const std::string path) const
{
if (auto size = tryGetSize(path)) return *size;
else throw ArbiterError("Could not get size of " + path);
}
void Driver::put(std::string path, const std::string& data) const
{
put(path, std::vector<char>(data.begin(), data.end()));
}
void Driver::copy(std::string src, std::string dst) const
{
put(dst, getBinary(src));
}
std::vector<std::string> Driver::resolve(
std::string path,
const bool verbose) const
{
std::vector<std::string> results;
if (path.size() > 1 && path.back() == '*')
{
if (verbose)
{
std::cout << "Resolving [" << type() << "]: " << path << " ..." <<
std::flush;
}
results = glob(path, verbose);
if (verbose)
{
std::cout << "\n\tResolved to " << results.size() <<
" paths." << std::endl;
}
}
else
{
if (isRemote()) path = type() + "://" + path;
else path = expandTilde(path);
results.push_back(path);
}
return results;
}
std::vector<std::string> Driver::glob(std::string path, bool verbose) const
{
throw ArbiterError("Cannot glob driver for: " + path);
}
} // namespace arbiter
#ifdef ARBITER_CUSTOM_NAMESPACE
}
#endif
// //////////////////////////////////////////////////////////////////////
// End of content of file: arbiter/driver.cpp
// //////////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////
// Beginning of content of file: arbiter/endpoint.cpp
// //////////////////////////////////////////////////////////////////////
#include <algorithm>
#include <fstream>
#ifndef ARBITER_IS_AMALGAMATION
#include <arbiter/endpoint.hpp>
#include <arbiter/arbiter.hpp>
#include <arbiter/driver.hpp>
#include <arbiter/drivers/fs.hpp>
#include <arbiter/util/sha256.hpp>
#include <arbiter/util/transforms.hpp>
#include <arbiter/util/util.hpp>
#endif
#ifdef ARBITER_CUSTOM_NAMESPACE
namespace ARBITER_CUSTOM_NAMESPACE
{
#endif
namespace arbiter
{
namespace
{
constexpr std::size_t mb = 1024 * 1024;
const std::size_t chunkSize = 10 * mb;
const auto streamFlags(
std::ofstream::binary |
std::ofstream::out |
std::ofstream::app);
std::string postfixSlash(std::string path)
{
if (!path.empty() && path.back() != '/') path.push_back('/');
return path;
}
}
Endpoint::Endpoint(const Driver& driver, const std::string root)
: m_driver(&driver)
, m_root(expandTilde(postfixSlash(root)))
{ }
std::string Endpoint::root() const
{
return m_root;
}
std::string Endpoint::prefixedRoot() const
{
return softPrefix() + root();
}
std::string Endpoint::type() const
{
return m_driver->type();
}
bool Endpoint::isRemote() const
{
return m_driver->isRemote();
}
bool Endpoint::isLocal() const
{
return !isRemote();
}
bool Endpoint::isHttpDerived() const
{
return tryGetHttpDriver() != nullptr;
}
LocalHandle Endpoint::getLocalHandle(
const std::string subpath,
http::Headers headers,
http::Query query) const
{
if (isRemote())
{
const std::string tmp(getTempPath());
const auto ext(getExtension(subpath));
const std::string basename(
std::to_string(randomNumber()) +
(ext.size() ? "." + ext : ""));
const std::string local(tmp + basename);
if (isHttpDerived())
{
if (auto fileSize = tryGetSize(subpath))
{
std::ofstream stream(local, streamFlags);
if (!stream.good())
{
throw ArbiterError("Unable to create local handle");
}
for (std::size_t pos(0); pos < *fileSize; pos += chunkSize)
{
const std::size_t end =
(std::min)(pos + chunkSize, *fileSize);
const std::string range("bytes=" +
std::to_string(pos) + "-" +
std::to_string(end - 1));
headers["Range"] = range;
const auto data(getBinary(subpath, headers, query));
stream.write(data.data(), data.size());
if (!stream.good())
{
throw ArbiterError("Unable to write local handle");
}
}
}
else
{
drivers::Fs fs;
fs.put(local, getBinary(subpath, headers, query));
}
}
else
{
drivers::Fs fs;
fs.put(local, getBinary(subpath));
}
return LocalHandle(local, true);
}
else
{
return LocalHandle(expandTilde(fullPath(subpath)), false);
}
}
std::string Endpoint::get(const std::string subpath) const
{
return m_driver->get(fullPath(subpath));
}
std::unique_ptr<std::string> Endpoint::tryGet(const std::string subpath)
const
{
return m_driver->tryGet(fullPath(subpath));
}
std::vector<char> Endpoint::getBinary(const std::string subpath) const
{
return m_driver->getBinary(fullPath(subpath));
}
std::unique_ptr<std::vector<char>> Endpoint::tryGetBinary(
const std::string subpath) const
{
return m_driver->tryGetBinary(fullPath(subpath));
}
std::size_t Endpoint::getSize(const std::string subpath) const
{
return m_driver->getSize(fullPath(subpath));
}
std::unique_ptr<std::size_t> Endpoint::tryGetSize(
const std::string subpath) const
{
return m_driver->tryGetSize(fullPath(subpath));
}
void Endpoint::put(const std::string subpath, const std::string& data) const
{
m_driver->put(fullPath(subpath), data);
}
void Endpoint::put(
const std::string subpath,
const std::vector<char>& data) const
{
m_driver->put(fullPath(subpath), data);
}
std::string Endpoint::get(
const std::string subpath,
const http::Headers headers,
const http::Query query) const
{
return getHttpDriver().get(fullPath(subpath), headers, query);
}
std::unique_ptr<std::string> Endpoint::tryGet(
const std::string subpath,
const http::Headers headers,
const http::Query query) const
{
return getHttpDriver().tryGet(fullPath(subpath), headers, query);
}
std::vector<char> Endpoint::getBinary(
const std::string subpath,
const http::Headers headers,
const http::Query query) const
{
return getHttpDriver().getBinary(fullPath(subpath), headers, query);
}
std::unique_ptr<std::vector<char>> Endpoint::tryGetBinary(
const std::string subpath,
const http::Headers headers,
const http::Query query) const
{
return getHttpDriver().tryGetBinary(fullPath(subpath), headers, query);
}
void Endpoint::put(
const std::string path,
const std::string& data,
const http::Headers headers,
const http::Query query) const
{
getHttpDriver().put(path, data, headers, query);
}
void Endpoint::put(
const std::string path,
const std::vector<char>& data,
const http::Headers headers,
const http::Query query) const
{
getHttpDriver().put(path, data, headers, query);
}
http::Response Endpoint::httpGet(
std::string path,
http::Headers headers,
http::Query query,
const std::size_t reserve) const
{
return getHttpDriver().internalGet(fullPath(path), headers, query, reserve);
}
http::Response Endpoint::httpPut(
std::string path,
const std::vector<char>& data,
http::Headers headers,
http::Query query) const
{
return getHttpDriver().internalPut(fullPath(path), data, headers, query);
}
http::Response Endpoint::httpHead(
std::string path,
http::Headers headers,
http::Query query) const
{
return getHttpDriver().internalHead(fullPath(path), headers, query);
}
http::Response Endpoint::httpPost(
std::string path,
const std::vector<char>& data,
http::Headers headers,
http::Query query) const
{
return getHttpDriver().internalPost(fullPath(path), data, headers, query);
}
std::string Endpoint::fullPath(const std::string& subpath) const
{
return m_root + subpath;
}
std::string Endpoint::prefixedFullPath(const std::string& subpath) const
{
return softPrefix() + fullPath(subpath);
}
Endpoint Endpoint::getSubEndpoint(std::string subpath) const
{
return Endpoint(*m_driver, m_root + subpath);
}
std::string Endpoint::softPrefix() const
{
return isRemote() ? type() + "://" : "";
}
const drivers::Http* Endpoint::tryGetHttpDriver() const
{
return dynamic_cast<const drivers::Http*>(m_driver);
}
const drivers::Http& Endpoint::getHttpDriver() const
{
if (auto d = tryGetHttpDriver()) return *d;
else throw ArbiterError("Cannot get driver of type " + type() + " as HTTP");
}
} // namespace arbiter
#ifdef ARBITER_CUSTOM_NAMESPACE
}
#endif
// //////////////////////////////////////////////////////////////////////
// End of content of file: arbiter/endpoint.cpp
// //////////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////
// Beginning of content of file: arbiter/drivers/fs.cpp
// //////////////////////////////////////////////////////////////////////
#ifndef ARBITER_IS_AMALGAMATION
#include <arbiter/arbiter.hpp>
#include <arbiter/drivers/fs.hpp>
#include <arbiter/util/util.hpp>
#endif
#ifndef ARBITER_WINDOWS
#include <glob.h>
#include <sys/stat.h>
#else
#define UNICODE
#include <Shlwapi.h>
#include <iterator>
#include <locale>
#include <codecvt>
#include <windows.h>
#include <direct.h>
#endif
#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <ios>
#include <istream>
#ifdef ARBITER_CUSTOM_NAMESPACE
namespace ARBITER_CUSTOM_NAMESPACE
{
#endif
namespace arbiter
{
namespace
{
// Binary output, overwriting any existing file with a conflicting name.
const std::ios_base::openmode binaryTruncMode(
std::ofstream::binary |
std::ofstream::out |
std::ofstream::trunc);
std::string getHome()
{
std::string s;
#ifndef ARBITER_WINDOWS
if (auto home = env("HOME")) s = *home;
#else
if (auto userProfile = env("USERPROFILE"))
{
s = *userProfile;
}
else
{
auto homeDrive(env("HOMEDRIVE"));
auto homePath(env("HOMEPATH"));
if (homeDrive && homePath) s = *homeDrive + *homePath;
}
#endif
return s;
}
}
namespace drivers
{
std::unique_ptr<Fs> Fs::create()
{
return std::unique_ptr<Fs>(new Fs());
}
std::unique_ptr<std::size_t> Fs::tryGetSize(std::string path) const
{
std::unique_ptr<std::size_t> size;
path = expandTilde(path);
std::ifstream stream(path, std::ios::in | std::ios::binary);
if (stream.good())
{
stream.seekg(0, std::ios::end);
size.reset(new std::size_t(stream.tellg()));
}
return size;
}
bool Fs::get(std::string path, std::vector<char>& data) const
{
bool good(false);