-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.pm
375 lines (279 loc) · 8.58 KB
/
Utils.pm
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
#!/usr/bin/perl
# $Id$
#
# WEB INTERFACE and Controll application for an headless squeezelite
# installation.
#
# Best used with Squeezelite-R2
# (https://github.com/marcoc1712/squeezelite/releases)
#
# Copyright 2016 Marco Curti, marcoc1712 at gmail dot com.
# Please visit www.marcoc1712.it
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License,
# version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
################################################################################
package Utils;
use strict;
use warnings;
use utf8;
use File::Copy qw(move copy);
use File::Path;
use File::Basename;
use Time::HiRes qw(time usleep);
use POSIX qw(strftime);
use constant ISWINDOWS => ( $^O =~ /^m?s?win/i ) ? 1 : 0;
use constant ISMAC => ( $^O =~ /darwin/i ) ? 1 : 0;
use constant ISLINUX => ( $^O =~ /linux/i ) ? 1 : 0;
sub new{
my $class = shift;
my $status = shift;
my $self = bless {
_status => $status,
}, $class;
return $self;
}
sub getStatus{
my $self = shift;
return $self->{_status};
}
################################################################################
# public
sub trim{
my $self = shift;
my ($val) = shift;
if (defined $val) {
$val =~ s/^\s+//; # strip white space from the beginning
$val =~ s/\s+$//; # strip white space from the end
if (! utf8::is_utf8($val)) {
utf8::upgrade($val);
}
}
return $val;
}
sub executeCommand{
my $self= shift;
my $command=shift;
#some hacking on quoting and escaping for differents Os...
$command= _finalizeCommand($command);
my @ret= `$command`;
my $err=$?;
return ($err, @ret);
}
sub mkDir{
my $self= shift;
my $dir = shift;
if (! $dir){
$self->getStatus()->record( " mkdir "."".", 0755",7, "undefined or empty dirname",'');
return undef;
}
if (! -d $dir){
mkpath $dir, 0755;
if (! -d $dir){
$self->getStatus()->record( " mkdir ".$dir.", 0755",7, "can't create directory",'');
return undef;
}
$self->getStatus()->record(" mkdir ".$dir.", 0755",1, 'created','');
return 1;
} else{
$self->getStatus()->record(" mkdir ".$dir.", 0755",1, 'already exists','');
return 1;
}
}
sub rmTree{
my $self = shift;
my $dir = shift;
if (!$dir){
$self->getStatus()->record( "",7, "undefined or empty directory name",'');
return undef;
}
if (! -e $dir){
$self->getStatus()->record( "",1, "directory: ".$dir." does not exists",'');
return 1;
}
my $err=undef;
my @answ=();
rmtree( $dir, {error => \my $msg} );
if (@$msg) {
$err="Error deleting tree starting at: $dir";
for my $diag (@$msg) {
my ($file, $message) = %$diag;
if ($file eq '') {
push @answ, "general error: $message";
} else {
push @answ, "problem unlinking $file: $message";
}
}
}
if ($err){
$self->getStatus()->record( "rmtree",7, $err,(join "\n", @answ));
return undef;
}
$self->getStatus()->record( "",1, "directory: ".$dir." removed",'');
return 1;
}
sub saveBU{
my $self = shift;
my $oldPath = shift;
my $newPath = shift;
if (! -e $oldPath){
$self->getStatus()->record( "",5, "file: ".$oldPath." does not exists.",'');
return 1;
}
if (-e $newPath){
$self->getStatus()->record( "",5, "file: ".$newPath." already exist, keeped",'');
return $self->removeFile($oldPath);
}
if (!$self->mkDir(File::Basename::dirname($newPath))){return undef;}
return $self->copyFile($oldPath, $newPath);
}
sub saveBUAndRemove{
my $self = shift;
my $oldPath = shift;
my $newPath = shift;
if (! -e $oldPath){
$self->getStatus()->record( "",1, "file: ".$oldPath." does not exists.",'');
return 1;
}
if (-e $newPath){
$self->getStatus()->record( "",3, "file: ".$newPath." already exist, keeped",'');
return $self->removeFile($oldPath);
}
if (!$self->mkDir(File::Basename::dirname($newPath))){return undef;}
return $self->moveFile($oldPath, $newPath);
}
#commons between copy and move-
sub _ckcopy{
my $self = shift;
my $oldPath = shift;
my $newPath = shift;
my $replace = shift || 0;
if (! $oldPath ){
$self->getStatus()->record( "",7, "undefined or empty old filepath",'');
return undef;
}
if (! $newPath){
$self->getStatus()->record( "",7, "undefined or empty new filepath",'');
return undef;
}
if (! -e $oldPath) {
$self->getStatus()->record( "",7, "file: ".$oldPath." does not exists",'');
return undef;
}
if (-e $newPath){
if (!$replace){
$self->getStatus()->record( "",7, "file: ".$newPath." already exists, not replaced",-e $newPath);
return undef;
}
if (!_remove($newPath)){
$self->getStatus()->record( "",7, "file: ".$newPath." already exists, could not remove",'');
return undef;
}
$self->getStatus()->record( "",1, "file: ".$newPath." removed",'');
}
return 1;
}
sub copyFile{
my $self = shift;
my $oldPath = shift;
my $newPath = shift;
my $replace = shift || 0;
if (!$self->_ckcopy($oldPath,$newPath,$replace)){return undef;}
copy $oldPath, $newPath;
if (! -e $newPath){
$self->getStatus()->record( "",7, "can't copy ".$oldPath." to ".$newPath,$!);
return undef;
}
$self->getStatus()->record( "",1, "file: ".$oldPath." copied to ".$newPath,'');
return 1;
}
sub moveFile{
my $self = shift;
my $oldPath = shift;
my $newPath = shift;
my $replace = shift || 0;
if (!$self->_ckcopy($oldPath,$newPath,$replace)){return undef;}
move $oldPath, $newPath;
if (-e $oldPath && !-e $newPath){
$self->getStatus()->record( "",7, "can't move ".$oldPath." to ".$newPath,$!);
return undef;
}
$self->getStatus()->record( "",1, "file: ".$oldPath." moved to ".$newPath,'');
return 1;
}
sub createFile{
my $self = shift;
my $path = shift;
my $line = shift;
if (!$path){
$self->getStatus()->record( "",7, "undefined or empty filepath",'');
return undef;
}
if (-e $path){
$self->getStatus()->record( "",7, "file: ".$path." already exists",'');
return undef;
}
open my $fileHandle, ">>", $path or return undef;
if ($line){
print $fileHandle $line;
}
close $fileHandle;
return 1;
}
sub removeFile{
my $self = shift;
my $path = shift;
if (!$path){
$self->getStatus()->record( "",7, "undefined or empty filepath",'');
return undef;
}
if (! -e $path){
$self->getStatus()->record( "",1, "file: ".$path." does not exists",'');
return 1;
}
unlink $path;
if (-e $path){
$self->getStatus()->record( "",7, "can't remove ".$path,'');
return undef;
}
$self->getStatus()->record( "",1, "file: ".$path." removed",'');
return 1;
}
sub getTimeString {
my $self = shift;
my $time = shift || time;
return POSIX::strftime('%Y%m%d%H%M%S', localtime($time));
}
sub getNiceTimeString {
my $self = shift;
my $time = shift || time;
return POSIX::strftime('%Y/%m/%d %H:%M:%S', localtime($time));
}
sub getTimestamp{
my $self = shift;
usleep(1000);
return time;
}
#################################################################################
# privates
sub _finalizeCommand{
my $command=shift;
if (!defined $command || $command eq "") {return ""}
if (ISWINDOWS){
# command could not start with ", should move it after the volume ID.
if (substr($command,0,1) eq '"'){
my $str= substr($command,2,length($command)-2);
my $vol= substr($command,1,1);
$command=$vol.'"'.$str;
}
}
return $command;
}
1;