| 1 | package File; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base qw(Object); | ||
| 5 | use Array; | ||
| 6 | use Hashtable; | ||
| 7 | use Time; | ||
| 8 | use File::Find; | ||
| 9 | use File::Copy::Recursive; | ||
| 10 | use YAML::XS (); | ||
| 11 | use JSON::XS (); | ||
| 12 | |||
| 13 | sub new { | ||
| 14 | my $pkg = shift; | ||
| 15 | return bless {}, $pkg; | ||
| 16 | } | ||
| 17 | |||
| 18 | =head2 from_json/yaml | ||
| 19 | return Hashtable or Array | ||
| 20 | my $object = from_yaml/json($file); | ||
| 21 | =cut | ||
| 22 | |||
| 23 | sub from_yaml { | ||
| 24 | my ( $filepath ) = @_; | ||
| 25 | my $ref = YAML::XS::LoadFile($filepath); | ||
| 26 | return | ||
| 27 | ref($ref) eq 'HASH' | ||
| 28 | ? NewBie::Gift::Hashtable->new($ref) | ||
| 29 | : NewBie::Gift::Array->new($ref); | ||
| 30 | } | ||
| 31 | |||
| 32 | sub from_json { | ||
| 33 | my ( $filepath ) = @_; | ||
| 34 | my $data = read_file($filepath); | ||
| 35 | my $ref = JSON::XS::decode_json($data); | ||
| 36 | return | ||
| 37 | ref($ref) eq 'HASH' | ||
| 38 | ? NewBie::Gift::Hashtable->new($ref) | ||
| 39 | : NewBie::Gift::Array->new($ref); | ||
| 40 | } | ||
| 41 | |||
| 42 | =head2 read_file | ||
| 43 | my $filepath = '/home/chenryn/test.conf'; | ||
| 44 | my $content = read_file($filepath); | ||
| 45 | say $content; | ||
| 46 | |||
| 47 | read_file($filepath, sub { | ||
| 48 | my ($content) = @_; | ||
| 49 | say $content->get(1); | ||
| 50 | }); | ||
| 51 | =cut | ||
| 52 | |||
| 53 | sub read_file { | ||
| 54 | my ( $filepath, $cb ) = @_; | ||
| 55 | open my $fh, '<', $filepath; | ||
| 56 | my $content = new Array; | ||
| 57 | while (<$fh>) { | ||
| 58 | chomp; | ||
| 59 | $content->push($_); | ||
| 60 | } | ||
| 61 | if ( defined $cb ) { | ||
| 62 | $cb->($content); | ||
| 63 | } | ||
| 64 | else { | ||
| 65 | return join( "\n", @$content ); | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | =head2 read_dir | ||
| 70 | read_dir('/root')->each(sub { | ||
| 71 | my $file = shift; | ||
| 72 | read_file( $file, sub { | ||
| 73 | ... | ||
| 74 | }) | ||
| 75 | }); | ||
| 76 | read_dir(qw(/root /tmp), sub{ | ||
| 77 | my ($dir, $file) = @_; | ||
| 78 | ... | ||
| 79 | }); | ||
| 80 | =cut | ||
| 81 | |||
| 82 | sub read_dir { | ||
| 83 | if ( ref( $_[-1] ) eq 'CODE' ) { | ||
| 84 | my $cb = pop @_; | ||
| 85 | my @dirpath = @_; | ||
| 86 | File::Find::find( | ||
| 87 | sub { | ||
| 88 | $cb->( $File::Find::dir, $_ ); | ||
| 89 | }, | ||
| 90 | @dirpath | ||
| 91 | ); | ||
| 92 | } | ||
| 93 | else { | ||
| 94 | my $dirpath = shift; | ||
| 95 | return Array->new( glob("$dirpath/*") ); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | sub mkdir_p { File::Copy::Recursive::pathmk(@_) } | ||
| 100 | |||
| 101 | sub rm_r { File::Copy::Recursive::pathrm(@_) } | ||
| 102 | |||
| 103 | sub cp_r { File::Copy::Recursive::rcopy(@_) } | ||
| 104 | |||
| 105 | =head2 | ||
| 106 | 0 dev device number of filesystem | ||
| 107 | 1 ino inode number | ||
| 108 | 2 mode file mode (type and permissions) | ||
| 109 | 3 nlink number of (hard) links to the file | ||
| 110 | 4 uid numeric user ID of file's owner | ||
| 111 | 5 gid numeric group ID of file's owner | ||
| 112 | 6 rdev the device identifier (special files only) | ||
| 113 | 7 size total size of file, in bytes | ||
| 114 | 8 atime last access time in seconds since the epoch | ||
| 115 | 9 mtime last modify time in seconds since the epoch | ||
| 116 | 10 ctime inode change time in seconds since the epoch (*) | ||
| 117 | 11 blksize preferred block size for file system I/O | ||
| 118 | 12 blocks actual number of blocks allocated | ||
| 119 | =cut | ||
| 120 | |||
| 121 | sub fstat { | ||
| 122 | my ( $file ) = @_; | ||
| 123 | my $ret = Array->new( stat($file) ); | ||
| 124 | return Hashtable->new( | ||
| 125 | dev => $ret->get(0), | ||
| 126 | inode => $ret->get(1), | ||
| 127 | mode => sprintf( "%04o", $ret->get(2) & 07777 ), | ||
| 128 | nlink => $ret->get(3), | ||
| 129 | uid => $ret->get(4), | ||
| 130 | gid => $ret->get(5), | ||
| 131 | rdev => $ret->get(6), | ||
| 132 | size => $ret->get(7), | ||
| 133 | atime => Time->new->from_epoch( $ret->get(8) ), | ||
| 134 | mtime => Time->new->from_epoch( $ret->get(9) ), | ||
| 135 | ctime => Time->new->from_epoch( $ret->get(10) ), | ||
| 136 | blksize => $ret->get(11), | ||
| 137 | blocks => $ret->get(12), | ||
| 138 | ); | ||
| 139 | } | ||
| 140 | |||
| 141 | 1; |