Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | ##---------------------------------------------------------------------------## |
2 | ## File: | |
3 | ## $Id: mhfile.pl,v 2.6 2002/05/14 00:04:40 ehood Exp $ | |
4 | ## Author: | |
5 | ## Earl Hood mhonarc@mhonarc.org | |
6 | ## Description: | |
7 | ## File routines for MHonArc | |
8 | ##---------------------------------------------------------------------------## | |
9 | ## MHonArc -- Internet mail-to-HTML converter | |
10 | ## Copyright (C) 1997-1999 Earl Hood, mhonarc@mhonarc.org | |
11 | ## | |
12 | ## This program is free software; you can redistribute it and/or modify | |
13 | ## it under the terms of the GNU General Public License as published by | |
14 | ## the Free Software Foundation; either version 2 of the License, or | |
15 | ## (at your option) any later version. | |
16 | ## | |
17 | ## This program is distributed in the hope that it will be useful, | |
18 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ## GNU General Public License for more details. | |
21 | ## | |
22 | ## You should have received a copy of the GNU General Public License | |
23 | ## along with this program; if not, write to the Free Software | |
24 | ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
25 | ## 02111-1307, USA | |
26 | ##---------------------------------------------------------------------------## | |
27 | ||
28 | package mhonarc; | |
29 | ||
30 | use Symbol; | |
31 | ||
32 | ##---------------------------------------------------------------------------## | |
33 | ||
34 | sub file_open { | |
35 | my($file) = shift; | |
36 | my($handle) = gensym; | |
37 | my($gz) = $file =~ /\.gz$/i; | |
38 | ||
39 | if ($gz) { | |
40 | return $handle if open($handle, "$GzipExe -cd $file |"); | |
41 | die qq/ERROR: Failed to exec "$GzipExe -cd $file |": $!\n/; | |
42 | } | |
43 | return $handle if open($handle, $file); | |
44 | if (-e "$file.gz") { | |
45 | return $handle if open($handle, "$GzipExe -cd $file.gz |"); | |
46 | die qq/ERROR: Failed to exec "$GzipExe -cd $file.gz |": $!\n/; | |
47 | } | |
48 | die qq/ERROR: Failed to open "$file": $!\n/; | |
49 | } | |
50 | ||
51 | sub file_create { | |
52 | my($file) = shift; | |
53 | my($gz) = shift; | |
54 | my($handle) = gensym; | |
55 | ||
56 | if ($gz) { | |
57 | $file .= ".gz" unless $file =~ /\.gz$/; | |
58 | return $handle if open($handle, "| $GzipExe > $file"); | |
59 | die qq{ERROR: Failed to exec "| $GzipExe > $file": $!\n}; | |
60 | } | |
61 | return $handle if open($handle, "> $file"); | |
62 | die qq{ERROR: Failed to create "$file": $!\n}; | |
63 | } | |
64 | ||
65 | sub file_exists { | |
66 | (-e $_[0]) || (-e "$_[0].gz"); | |
67 | } | |
68 | ||
69 | sub file_copy { | |
70 | my($src, $dst) = ($_[0], $_[1]); | |
71 | my($gz) = $src =~ /\.gz$/i; | |
72 | ||
73 | if ($gz || (-e "$src.gz")) { | |
74 | $src .= ".gz" unless $gz; | |
75 | $dst .= ".gz" unless $dst =~ /\.gz$/i; | |
76 | } | |
77 | &cp($src, $dst); | |
78 | } | |
79 | ||
80 | sub file_rename { | |
81 | my($src, $dst) = ($_[0], $_[1]); | |
82 | my($gz) = $src =~ /\.gz$/i; | |
83 | ||
84 | if ($gz || (-e "$src.gz")) { | |
85 | $src .= ".gz" unless $gz; | |
86 | $dst .= ".gz" unless $dst =~ /\.gz$/i; | |
87 | } | |
88 | if (!rename($src, $dst)) { | |
89 | die qq/ERROR: Unable to rename "$src" to "$dst": $!\n/; | |
90 | } | |
91 | } | |
92 | ||
93 | sub file_remove { | |
94 | my($file) = shift; | |
95 | ||
96 | unlink($file); | |
97 | unlink("$file.gz"); | |
98 | } | |
99 | ||
100 | sub file_utime { | |
101 | my($atime) = shift; | |
102 | my($mtime) = shift; | |
103 | foreach (@_) { | |
104 | utime($atime, $mtime, $_, "$_.gz"); | |
105 | } | |
106 | } | |
107 | ||
108 | ##---------------------------------------------------------------------------## | |
109 | ||
110 | sub dir_remove { | |
111 | my($file) = shift; | |
112 | ||
113 | if (-d $file) { | |
114 | local(*DIR); | |
115 | local($_); | |
116 | if (!opendir(DIR, $file)) { | |
117 | warn qq{Warning: Unable to open "$file"\n}; | |
118 | return 0; | |
119 | } | |
120 | my @files = grep(!/^(\.|\..)$/i, readdir(DIR)); | |
121 | closedir(DIR); | |
122 | foreach (@files) { | |
123 | &dir_remove($file . $mhonarc::DIRSEP . $_); | |
124 | } | |
125 | if (!rmdir($file)) { | |
126 | warn qq{Warning: Unable to remove "$file": $!\n}; | |
127 | return 0; | |
128 | } | |
129 | ||
130 | } else { | |
131 | if (!unlink($file)) { | |
132 | warn qq{Warning: Unable to delete "$file": $!\n}; | |
133 | return 0; | |
134 | } | |
135 | } | |
136 | 1; | |
137 | } | |
138 | ||
139 | ##---------------------------------------------------------------------------## | |
140 | 1; |