/
PathInfo.cc
1159 lines (1032 loc) · 31.6 KB
/
PathInfo.cc
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
/*---------------------------------------------------------------------\
| ____ _ __ __ ___ |
| |__ / \ / / . \ . \ |
| / / \ V /| _/ _/ |
| / /__ | | | | | | |
| /_____||_| |_| |_| |
| |
\---------------------------------------------------------------------*/
/** \file zypp/PathInfo.cc
*
*/
#include <sys/types.h> // for ::minor, ::major macros
#include <utime.h> // for ::utime
#include <sys/statvfs.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#include "zypp/base/LogTools.h"
#include "zypp/base/String.h"
#include "zypp/base/IOStream.h"
#include "zypp/base/StrMatcher.h"
#include "zypp/base/Errno.h"
#include "zypp/AutoDispose.h"
#include "zypp/ExternalProgram.h"
#include "zypp/PathInfo.h"
#include "zypp/Digest.h"
#include "zypp/TmpPath.h"
using std::endl;
using std::string;
///////////////////////////////////////////////////////////////////
namespace zypp
{ /////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
namespace filesystem
{ /////////////////////////////////////////////////////////////////
/******************************************************************
**
** FUNCTION NAME : operator<<
** FUNCTION TYPE : std::ostream &
*/
std::ostream & operator<<( std::ostream & str, FileType obj )
{
switch ( obj ) {
#define EMUMOUT(T) case T: return str << #T; break
EMUMOUT( FT_NOT_AVAIL );
EMUMOUT( FT_NOT_EXIST );
EMUMOUT( FT_FILE );
EMUMOUT( FT_DIR );
EMUMOUT( FT_CHARDEV );
EMUMOUT( FT_BLOCKDEV );
EMUMOUT( FT_FIFO );
EMUMOUT( FT_LINK );
EMUMOUT( FT_SOCKET );
#undef EMUMOUT
}
return str;
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : StatMode::fileType
// METHOD TYPE : FileType
//
FileType StatMode::fileType() const
{
if ( isFile() )
return FT_FILE;
if ( isDir() )
return FT_DIR;
if ( isLink() )
return FT_LINK;
if ( isChr() )
return FT_CHARDEV;
if ( isBlk() )
return FT_BLOCKDEV;
if ( isFifo() )
return FT_FIFO;
if ( isSock() )
return FT_SOCKET ;
return FT_NOT_AVAIL;
}
/******************************************************************
**
** FUNCTION NAME : operator<<
** FUNCTION TYPE : std::ostream &
*/
std::ostream & operator<<( std::ostream & str, const StatMode & obj )
{
iostr::IosFmtFlagsSaver autoResoreState( str );
char t = '?';
if ( obj.isFile() )
t = '-';
else if ( obj.isDir() )
t = 'd';
else if ( obj.isLink() )
t = 'l';
else if ( obj.isChr() )
t = 'c';
else if ( obj.isBlk() )
t = 'b';
else if ( obj.isFifo() )
t = 'p';
else if ( obj.isSock() )
t = 's';
str << t << " " << std::setfill( '0' ) << std::setw( 4 ) << std::oct << obj.perm();
return str;
}
///////////////////////////////////////////////////////////////////
//
// Class : PathInfo
//
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : PathInfo::PathInfo
// METHOD TYPE : Constructor
//
PathInfo::PathInfo()
: mode_e( STAT )
, error_i( -1 )
{}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : PathInfo::PathInfo
// METHOD TYPE : Constructor
//
PathInfo::PathInfo( const Pathname & path, Mode initial )
: path_t( path )
, mode_e( initial )
, error_i( -1 )
{
operator()();
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : PathInfo::PathInfo
// METHOD TYPE : Constructor
//
PathInfo::PathInfo( const std::string & path, Mode initial )
: path_t( path )
, mode_e( initial )
, error_i( -1 )
{
operator()();
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : PathInfo::PathInfo
// METHOD TYPE : Constructor
//
PathInfo::PathInfo( const char * path, Mode initial )
: path_t( path )
, mode_e( initial )
, error_i( -1 )
{
operator()();
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : PathInfo::~PathInfo
// METHOD TYPE : Destructor
//
PathInfo::~PathInfo()
{
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : PathInfo::operator()
// METHOD TYPE : bool
//
bool PathInfo::operator()()
{
if ( path_t.empty() ) {
error_i = -1;
} else {
switch ( mode_e ) {
case STAT:
error_i = ::stat( path_t.asString().c_str(), &statbuf_C );
break;
case LSTAT:
error_i = ::lstat( path_t.asString().c_str(), &statbuf_C );
break;
}
if ( error_i == -1 )
error_i = errno;
}
return !error_i;
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : PathInfo::fileType
// METHOD TYPE : File_type
//
FileType PathInfo::fileType() const
{
if ( isExist() )
return asStatMode().fileType();
return FT_NOT_EXIST;
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : PathInfo::userMay
// METHOD TYPE : mode_t
//
mode_t PathInfo::userMay() const
{
if ( !isExist() )
return 0;
if ( owner() == geteuid() ) {
return( uperm()/0100 );
} else if ( group() == getegid() ) {
return( gperm()/010 );
}
return operm();
}
/******************************************************************
**
** FUNCTION NAME : PathInfo::devMajor
** FUNCTION TYPE : unsigned int
*/
unsigned int PathInfo::devMajor() const
{
return isBlk() || isChr() ? ::major(statbuf_C.st_rdev) : 0;
}
/******************************************************************
**
** FUNCTION NAME : PathInfo::devMinor
** FUNCTION TYPE : unsigned int
*/
unsigned int PathInfo::devMinor() const
{
return isBlk() || isChr() ? ::minor(statbuf_C.st_rdev) : 0;
}
unsigned int PathInfo::major() const
{ INT << "Cleanup the code: This method is deprecated" << endl; return devMajor(); }
unsigned int PathInfo::minor() const
{ INT << "Cleanup the code: This method is deprecated" << endl; return devMinor(); }
/******************************************************************
**
** FUNCTION NAME : operator<<
** FUNCTION TYPE : std::ostream &
*/
std::ostream & operator<<( std::ostream & str, const PathInfo & obj )
{
iostr::IosFmtFlagsSaver autoResoreState( str );
str << obj.asString() << "{";
if ( !obj.isExist() ) {
str << Errno( obj.error() );
} else {
str << obj.asStatMode() << " " << std::dec << obj.owner() << "/" << obj.group();
if ( obj.isFile() )
str << " size " << obj.size();
}
return str << "}";
}
///////////////////////////////////////////////////////////////////
//
// filesystem utilities
//
///////////////////////////////////////////////////////////////////
/******************************************************************
**
** FUNCTION NAME : _Log_Result
** FUNCTION TYPE : int
**
** DESCRIPTION : Helper function to log return values.
*/
#define _Log_Result MIL << endl, __Log_Result
inline int __Log_Result( const int res, const char * rclass = 0 /*errno*/ )
{
if ( res )
{
if ( rclass )
WAR << " FAILED: " << rclass << " " << res << endl;
else
WAR << " FAILED: " << str::strerror( res ) << endl;
}
return res;
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : PathInfo::mkdir
// METHOD TYPE : int
//
int mkdir( const Pathname & path, unsigned mode )
{
MIL << "mkdir " << path << ' ' << str::octstring( mode );
if ( ::mkdir( path.asString().c_str(), mode ) == -1 ) {
return _Log_Result( errno );
}
return _Log_Result( 0 );
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : assert_dir()
// METHOD TYPE : int
//
int assert_dir( const Pathname & path, unsigned mode )
{
if ( path.empty() )
return ENOENT;
{ // Handle existing paths in advance.
PathInfo pi( path );
if ( pi.isDir() )
return 0;
if ( pi.isExist() )
return EEXIST;
}
string spath = path.asString()+"/";
string::size_type lastpos = ( path.relative() ? 2 : 1 ); // skip leasding './' or '/'
string::size_type pos = string::npos;
int ret = 0;
while ( (pos = spath.find('/',lastpos)) != string::npos )
{
string dir( spath.substr(0,pos) );
ret = ::mkdir( dir.c_str(), mode );
if ( ret == -1 )
{
if ( errno == EEXIST ) // ignore errors about already existing paths
ret = 0;
else
{
ret = errno;
WAR << " FAILED: mkdir " << dir << ' ' << str::octstring( mode ) << " errno " << ret << endl;
}
}
else
{
MIL << "mkdir " << dir << ' ' << str::octstring( mode ) << endl;
}
lastpos = pos+1;
}
return ret;
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : rmdir
// METHOD TYPE : int
//
int rmdir( const Pathname & path )
{
MIL << "rmdir " << path;
if ( ::rmdir( path.asString().c_str() ) == -1 ) {
return _Log_Result( errno );
}
return _Log_Result( 0 );
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : recursive_rmdir
// METHOD TYPE : int
//
static int recursive_rmdir_1( const Pathname & dir )
{
DIR * dp;
struct dirent * d;
if ( ! (dp = opendir( dir.c_str() )) )
return _Log_Result( errno );
while ( (d = readdir(dp)) )
{
std::string direntry = d->d_name;
if ( direntry == "." || direntry == ".." )
continue;
Pathname new_path( dir / d->d_name );
struct stat st;
if ( ! lstat( new_path.c_str(), &st ) )
{
if ( S_ISDIR( st.st_mode ) )
recursive_rmdir_1( new_path );
else
::unlink( new_path.c_str() );
}
}
closedir( dp );
if ( ::rmdir( dir.c_str() ) < 0 )
return errno;
return 0;
}
///////////////////////////////////////////////////////////////////
int recursive_rmdir( const Pathname & path )
{
MIL << "recursive_rmdir " << path << ' ';
PathInfo p( path );
if ( !p.isExist() ) {
return _Log_Result( 0 );
}
if ( !p.isDir() ) {
return _Log_Result( ENOTDIR );
}
return _Log_Result( recursive_rmdir_1( path ) );
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : clean_dir
// METHOD TYPE : int
//
int clean_dir( const Pathname & path )
{
MIL << "clean_dir " << path << ' ';
PathInfo p( path );
if ( !p.isExist() ) {
return _Log_Result( 0 );
}
if ( !p.isDir() ) {
return _Log_Result( ENOTDIR );
}
string cmd( str::form( "cd '%s' && rm -rf --preserve-root -- *", path.asString().c_str() ) );
ExternalProgram prog( cmd, ExternalProgram::Stderr_To_Stdout );
for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
MIL << " " << output;
}
int ret = prog.close();
return _Log_Result( ret, "returned" );
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : copy_dir
// METHOD TYPE : int
//
int copy_dir( const Pathname & srcpath, const Pathname & destpath )
{
MIL << "copy_dir " << srcpath << " -> " << destpath << ' ';
PathInfo sp( srcpath );
if ( !sp.isDir() ) {
return _Log_Result( ENOTDIR );
}
PathInfo dp( destpath );
if ( !dp.isDir() ) {
return _Log_Result( ENOTDIR );
}
PathInfo tp( destpath + srcpath.basename() );
if ( tp.isExist() ) {
return _Log_Result( EEXIST );
}
const char *const argv[] = {
"/bin/cp",
"-dR",
"--",
srcpath.asString().c_str(),
destpath.asString().c_str(),
NULL
};
ExternalProgram prog( argv, ExternalProgram::Stderr_To_Stdout );
for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
MIL << " " << output;
}
int ret = prog.close();
return _Log_Result( ret, "returned" );
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : copy_dir_content
// METHOD TYPE : int
//
int copy_dir_content(const Pathname & srcpath, const Pathname & destpath)
{
MIL << "copy_dir " << srcpath << " -> " << destpath << ' ';
PathInfo sp( srcpath );
if ( !sp.isDir() ) {
return _Log_Result( ENOTDIR );
}
PathInfo dp( destpath );
if ( !dp.isDir() ) {
return _Log_Result( ENOTDIR );
}
if ( srcpath == destpath ) {
return _Log_Result( EEXIST );
}
std::string src( srcpath.asString());
src += "/.";
const char *const argv[] = {
"/bin/cp",
"-dR",
"--",
src.c_str(),
destpath.asString().c_str(),
NULL
};
ExternalProgram prog( argv, ExternalProgram::Stderr_To_Stdout );
for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
MIL << " " << output;
}
int ret = prog.close();
return _Log_Result( ret, "returned" );
}
///////////////////////////////////////////////////////////////////////
// dirForEach
///////////////////////////////////////////////////////////////////////
const StrMatcher & matchNoDots()
{
static StrMatcher noDots( "[^.]*", Match::GLOB );
return noDots;
}
int dirForEach( const Pathname & dir_r, function<bool(const Pathname &, const char *const)> fnc_r )
{
if ( ! fnc_r )
return 0;
AutoDispose<DIR *> dir( ::opendir( dir_r.c_str() ),
[]( DIR * dir_r ) { if ( dir_r ) ::closedir( dir_r ); } );
MIL << "readdir " << dir_r << ' ';
if ( ! dir )
return _Log_Result( errno );
MIL << endl; // close line before callbacks are invoked.
int ret = 0;
for ( struct dirent * entry = ::readdir( dir ); entry; entry = ::readdir( dir ) )
{
if ( entry->d_name[0] == '.' && ( entry->d_name[1] == '\0' || ( entry->d_name[1] == '.' && entry->d_name[2] == '\0' ) ) )
continue; // omitt . and ..
if ( ! fnc_r( dir_r, entry->d_name ) )
{
ret = -1;
break;
}
}
return ret;
}
int dirForEach( const Pathname & dir_r, const StrMatcher & matcher_r, function<bool( const Pathname &, const char *const)> fnc_r )
{
if ( ! fnc_r )
return 0;
bool nodots = ( &matcher_r == &matchNoDots() );
return dirForEach( dir_r,
[&]( const Pathname & dir_r, const char *const name_r )->bool
{
if ( ( nodots && name_r[0] == '.' ) || ! matcher_r( name_r ) )
return true;
return fnc_r( dir_r, name_r );
} );
}
///////////////////////////////////////////////////////////////////
// readdir
///////////////////////////////////////////////////////////////////
int readdir( std::list<std::string> & retlist_r, const Pathname & path_r, bool dots_r )
{
retlist_r.clear();
return dirForEach( path_r,
[&]( const Pathname & dir_r, const char *const name_r )->bool
{
if ( dots_r || name_r[0] != '.' )
retlist_r.push_back( name_r );
return true;
} );
}
int readdir( std::list<Pathname> & retlist_r, const Pathname & path_r, bool dots_r )
{
retlist_r.clear();
return dirForEach( path_r,
[&]( const Pathname & dir_r, const char *const name_r )->bool
{
if ( dots_r || name_r[0] != '.' )
retlist_r.push_back( dir_r/name_r );
return true;
} );
}
bool DirEntry::operator==( const DirEntry &rhs ) const
{
// if one of the types is not known, use the name only
if ( type == FT_NOT_AVAIL || rhs.type == FT_NOT_AVAIL )
return ( name == rhs.name );
return ((name == rhs.name ) && (type == rhs.type));
}
int readdir( DirContent & retlist_r, const Pathname & path_r, bool dots_r, PathInfo::Mode statmode_r )
{
retlist_r.clear();
return dirForEach( path_r,
[&]( const Pathname & dir_r, const char *const name_r )->bool
{
if ( dots_r || name_r[0] != '.' )
retlist_r.push_back( DirEntry( name_r, PathInfo( dir_r/name_r, statmode_r ).fileType() ) );
return true;
} );
}
std::ostream & operator<<( std::ostream & str, const DirContent & obj )
{ return dumpRange( str, obj.begin(), obj.end() ); }
///////////////////////////////////////////////////////////////////
// is_empty_dir
///////////////////////////////////////////////////////////////////
int is_empty_dir( const Pathname & path_r )
{
return dirForEach( path_r,
[&]( const Pathname & dir_r, const char *const name_r )->bool
{ return false; } );
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : unlink
// METHOD TYPE : int
//
int unlink( const Pathname & path )
{
MIL << "unlink " << path;
if ( ::unlink( path.asString().c_str() ) == -1 ) {
return _Log_Result( errno );
}
return _Log_Result( 0 );
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : rename
// METHOD TYPE : int
//
int rename( const Pathname & oldpath, const Pathname & newpath )
{
MIL << "rename " << oldpath << " -> " << newpath;
if ( ::rename( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 ) {
return _Log_Result( errno );
}
return _Log_Result( 0 );
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : exchange
// METHOD TYPE : int
//
int exchange( const Pathname & lpath, const Pathname & rpath )
{
MIL << "exchange " << lpath << " <-> " << rpath;
if ( lpath.empty() || rpath.empty() )
return _Log_Result( EINVAL );
PathInfo linfo( lpath );
PathInfo rinfo( rpath );
if ( ! linfo.isExist() )
{
if ( ! rinfo.isExist() )
return _Log_Result( 0 ); // both don't exist.
// just rename rpath -> lpath
int ret = assert_dir( lpath.dirname() );
if ( ret != 0 )
return _Log_Result( ret );
if ( ::rename( rpath.c_str(), lpath.c_str() ) == -1 ) {
return _Log_Result( errno );
}
return _Log_Result( 0 );
}
// HERE: lpath exists:
if ( ! rinfo.isExist() )
{
// just rename lpath -> rpath
int ret = assert_dir( rpath.dirname() );
if ( ret != 0 )
return _Log_Result( ret );
if ( ::rename( lpath.c_str(), rpath.c_str() ) == -1 ) {
return _Log_Result( errno );
}
return _Log_Result( 0 );
}
// HERE: both exist
TmpFile tmpfile( TmpFile::makeSibling( rpath ) );
if ( ! tmpfile )
return _Log_Result( errno );
Pathname tmp( tmpfile.path() );
::unlink( tmp.c_str() );
if ( ::rename( lpath.c_str(), tmp.c_str() ) == -1 ) {
return _Log_Result( errno );
}
if ( ::rename( rpath.c_str(), lpath.c_str() ) == -1 ) {
::rename( tmp.c_str(), lpath.c_str() );
return _Log_Result( errno );
}
if ( ::rename( tmp.c_str(), rpath.c_str() ) == -1 ) {
::rename( lpath.c_str(), rpath.c_str() );
::rename( tmp.c_str(), lpath.c_str() );
return _Log_Result( errno );
}
return _Log_Result( 0 );
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : copy
// METHOD TYPE : int
//
int copy( const Pathname & file, const Pathname & dest )
{
MIL << "copy " << file << " -> " << dest << ' ';
PathInfo sp( file );
if ( !sp.isFile() ) {
return _Log_Result( EINVAL );
}
PathInfo dp( dest );
if ( dp.isDir() ) {
return _Log_Result( EISDIR );
}
const char *const argv[] = {
"/bin/cp",
"--remove-destination",
"--",
file.asString().c_str(),
dest.asString().c_str(),
NULL
};
ExternalProgram prog( argv, ExternalProgram::Stderr_To_Stdout );
for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
MIL << " " << output;
}
int ret = prog.close();
return _Log_Result( ret, "returned" );
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : symlink
// METHOD TYPE : int
//
int symlink( const Pathname & oldpath, const Pathname & newpath )
{
MIL << "symlink " << newpath << " -> " << oldpath;
if ( ::symlink( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 ) {
return _Log_Result( errno );
}
return _Log_Result( 0 );
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : hardlink
// METHOD TYPE : int
//
int hardlink( const Pathname & oldpath, const Pathname & newpath )
{
MIL << "hardlink " << newpath << " -> " << oldpath;
if ( ::link( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 ) {
return _Log_Result( errno );
}
return _Log_Result( 0 );
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : hardlink
// METHOD TYPE : int
//
int hardlinkCopy( const Pathname & oldpath, const Pathname & newpath )
{
MIL << "hardlinkCopy " << oldpath << " -> " << newpath;
PathInfo pi( oldpath, PathInfo::LSTAT );
if ( pi.isLink() )
{
// dont hardlink symlinks!
return copy( oldpath, newpath );
}
pi.lstat( newpath );
if ( pi.isExist() )
{
int res = unlink( newpath );
if ( res != 0 )
return _Log_Result( res );
}
// Here: no symlink, no newpath
if ( ::link( oldpath.asString().c_str(), newpath.asString().c_str() ) == -1 )
{
switch ( errno )
{
case EPERM: // /proc/sys/fs/protected_hardlink in proc(5)
case EXDEV: // oldpath and newpath are not on the same mounted file system
return copy( oldpath, newpath );
break;
}
return _Log_Result( errno );
}
return _Log_Result( 0 );
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : readlink
// METHOD TYPE : int
//
int readlink( const Pathname & symlink_r, Pathname & target_r )
{
static const ssize_t bufsiz = 2047;
static char buf[bufsiz+1];
ssize_t ret = ::readlink( symlink_r.c_str(), buf, bufsiz );
if ( ret == -1 )
{
target_r = Pathname();
MIL << "readlink " << symlink_r;
return _Log_Result( errno );
}
buf[ret] = '\0';
target_r = buf;
return 0;
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : expandlink
// METHOD TYPE : Pathname
//
Pathname expandlink( const Pathname & path_r )
{
static const unsigned int level_limit = 256;
static unsigned int count;
Pathname path(path_r);
PathInfo info(path_r, PathInfo::LSTAT);
for (count = level_limit; info.isLink() && count; count--)
{
DBG << "following symlink " << path;
path = path.dirname() / readlink(path);
DBG << "->" << path << std::endl;
info = PathInfo(path, PathInfo::LSTAT);
}
// expand limit reached
if (count == 0)
{
ERR << "Expand level limit reached. Probably a cyclic symbolic link." << endl;
return Pathname();
}
// symlink
else if (count < level_limit)
{
// check for a broken link
if (PathInfo(path).isExist())
return path;
// broken link, return an empty path
else
{
ERR << path << " is broken (expanded from " << path_r << ")" << endl;
return Pathname();
}
}
// not a symlink, return the original pathname
DBG << "not a symlink" << endl;
return path;
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : copy_file2dir
// METHOD TYPE : int
//
int copy_file2dir( const Pathname & file, const Pathname & dest )
{
MIL << "copy_file2dir " << file << " -> " << dest << ' ';
PathInfo sp( file );
if ( !sp.isFile() ) {
return _Log_Result( EINVAL );
}
PathInfo dp( dest );
if ( !dp.isDir() ) {
return _Log_Result( ENOTDIR );
}
const char *const argv[] = {
"/bin/cp",
"--",
file.asString().c_str(),
dest.asString().c_str(),
NULL
};
ExternalProgram prog( argv, ExternalProgram::Stderr_To_Stdout );
for ( string output( prog.receiveLine() ); output.length(); output = prog.receiveLine() ) {
MIL << " " << output;
}
int ret = prog.close();
return _Log_Result( ret, "returned" );
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : md5sum
// METHOD TYPE : std::string
//
std::string md5sum( const Pathname & file )
{
if ( ! PathInfo( file ).isFile() ) {
return string();
}
std::ifstream istr( file.asString().c_str() );
if ( ! istr ) {
return string();
}
return Digest::digest( "MD5", istr );
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : sha1sum
// METHOD TYPE : std::string
//
std::string sha1sum( const Pathname & file )
{
return checksum(file, "SHA1");
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : checksum
// METHOD TYPE : std::string
//
std::string checksum( const Pathname & file, const std::string &algorithm )
{
if ( ! PathInfo( file ).isFile() ) {
return string();
}
std::ifstream istr( file.asString().c_str() );
if ( ! istr ) {
return string();
}
return Digest::digest( algorithm, istr );
}