2 # This program is free software; you can redistribute it and/or modify
3 # it under the terms of the GNU General Public License as published by
4 # the Free Software Foundation, version 2 of the License
6 This file was transferred by kio_fish, a network client part of the
7 KDE project. You may safely delete it, it will be transferred again
8 when needed. It's only purpose is to make kio_fish access faster and
15 #open(DEBUG,">/tmp/kio_fish.debug.$$.log");
16 # save code in initial directory if just transferred
18 unlink('.fishsrv.pl');
19 sysopen(FH
,'.fishsrv.pl',O_WRONLY
|O_CREAT
|O_EXCL
);
22 chmod(0444,'.fishsrv.pl');
23 # request new code if it changed (checksum mismatch)
24 # for automatic upgrades
25 } elsif ($ARGV[0] ne "{CHECKSUM}") {
27 print "### 100 transfer fish server\n";
35 # we are up and running.
38 use POSIX
qw(getcwd dup2 strftime);
39 $SIG{'CHLD'} = 'IGNORE';
41 MAIN
: while (<STDIN
>) {
44 next if !length($_) || substr($_,0,1) ne '#';
48 # We do not advertise "append" capability anymore, as "write" is
49 # as fast in perl mode and more reliable (overlapping writes)
50 print "VER 0.0.3 copy lscount lslinks lsmime exec stat\n### 200\n";
54 print getcwd
(),"\n### 200\n";
57 /^SYMLINK\s+((?:\\.|[^\\])*?)\s+((?:\\.|[^\\])*?)\s*$/ && do {
58 my $ofn = unquote
($1);
60 print (symlink($ofn,$fn)?
"### 200\n":"### 500 $!\n");
63 /^COPY\s+((?:\\.|[^\\])*?)\s+((?:\\.|[^\\])*?)\s*$/ && do {
64 my $ofn = unquote
($1);
66 my ($size) = (stat($ofn))[7];
69 my $dest = readlink($ofn);
71 symlink($dest,$fn) || ($read = 0);
73 sysopen(FH
,$ofn,O_RDONLY
) || do { print "### 500 $!\n"; next; };
74 sysopen(OFH
,$fn,O_WRONLY
|O_CREAT
|O_TRUNC
) || do { close(FH
); print "### 500 $!\n"; next; };
77 while ($size > 32768 && ($read = sysread(FH
,$buffer,32768)) > 0) {
79 if (syswrite(OFH
,$buffer,$read) != $read) {
80 close(FH
); close(OFH
);
86 while ($size > 0 && ($read = sysread(FH
,$buffer,$size)) > 0) {
88 if (syswrite(OFH
,$buffer,$read) != $read) {
89 close(FH
); close(OFH
);
100 print "### 500 $!\n";
104 /^LINK\s+((?:\\.|[^\\])*?)\s+((?:\\.|[^\\])*?)\s*$/ && do {
105 my $ofn = unquote
($1);
106 my $fn = unquote
($2);
107 print (link($ofn,$fn)?
"### 200\n":"### 500 $!\n");
110 /^RENAME\s+((?:\\.|[^\\])*?)\s+((?:\\.|[^\\])*?)\s*$/ && do {
111 my $ofn = unquote
($1);
112 my $fn = unquote
($2);
113 print (rename($ofn,$fn)?
"### 200\n":"### 500 $!\n");
116 /^CHGRP\s+(\d+)\s+((?:\\.|[^\\])*?)\s*$/ && do {
117 my $fn = unquote
($2);
118 print (chown(-1,int($1),$fn)?
"### 200\n":"### 500 $!\n");
121 /^CHOWN\s+(\d+)\s+((?:\\.|[^\\])*?)\s*$/ && do {
122 my $fn = unquote
($2);
123 print (chown(int($1),-1,$fn)?
"### 200\n":"### 500 $!\n");
126 /^CHMOD\s+([0-7]+)\s+((?:\\.|[^\\])*?)\s*$/ && do {
127 my $fn = unquote
($2);
128 print (chmod(oct($1),$fn)?
"### 200\n":"### 500 $!\n");
131 /^DELE\s+((?:\\.|[^\\])*?)\s*$/ && do {
132 my $fn = unquote
($1);
133 print (unlink($fn)?
"### 200\n":"### 500 $!\n");
136 /^RMD\s+((?:\\.|[^\\])*?)\s*$/ && do {
137 my $dn = unquote
($1);
138 print (rmdir($dn)?
"### 200\n":"### 500 $!\n");
141 /^MKD\s+((?:\\.|[^\\])*?)\s*$/ && do {
142 my $dn = unquote
($1);
143 if (mkdir($dn,0777)) {
147 print (chdir($dn)?
"### 501 $err\n":"### 500 $err\n");
151 /^CWD\s+((?:\\.|[^\\])*?)\s*$/ && do {
152 my $dn = unquote
($1);
153 print (chdir($dn)?
"### 200\n":"### 500 $!\n");
156 /^LIST\s+((?:\\.|[^\\])*?)\s*$/ && do {
160 /^STAT\s+((?:\\.|[^\\])*?)\s*$/ && do {
164 /^WRITE\s+(\d+)\s+(\d+)\s+((?:\\.|[^\\])*?)\s*$/ && do {
165 write_loop
($2,$3,O_WRONLY
|O_CREAT
,$1);
168 /^APPEND\s+(\d+)\s+((?:\\.|[^\\])*?)\s*$/ && do {
169 write_loop
($1,$2,O_WRONLY
|O_APPEND
);
172 /^STOR\s+(\d+)\s+((?:\\.|[^\\])*?)\s*$/ && do {
173 write_loop
($1,$2,O_WRONLY
|O_CREAT
|O_TRUNC
);
176 /^RETR\s+((?:\\.|[^\\])*?)\s*$/ && do {
180 /^READ\s+(\d+)\s+(\d+)\s+((?:\\.|[^\\])*?)\s*$/ && do {
184 /^EXEC\s+((?:\\.|[^\\])*?)\s+((?:\\.|[^\\])*?)\s*$/ && do {
185 my $tempfile = unquote
($2);
186 my $command = unquote
($1);
187 $command = $command . ";echo \"###RESULT: \$?\"";
188 print("### 500 $!\n"), next
189 if (!sysopen(FH
,$tempfile,O_CREAT
|O_EXCL
|O_WRONLY
,0600));
191 print("### 500 $!\n"), next
194 open(STDOUT
,'>>&FH');
195 open(STDERR
,'>>&FH');
196 open(STDIN
,'</dev/null'); # not sure here, ms windows anyone?
197 exec('/bin/sh','-c',$command);
198 print STDERR
"Couldn't exec /bin/sh: $!\n";
210 my $dn = unquote
($_[0]);
213 print "### 404 File does not exist\n";
215 } elsif ($_[1] && -d _
) {
216 opendir(DIR
,$dn) || do { print "### 500 $!\n"; return; };
217 @entries = readdir(DIR
);
220 ($dn, @entries) = $dn =~ m{(.*)/(.*)};
221 $dn = '/' if (!length($dn));
223 print scalar(@entries),"\n### 100\n";
225 chdir($dn) || do { print "### 500 $!\n"; return; };
228 my ($mode,$uid,$gid,$size,$mtime) = (lstat)[2,4,5,7,9];
229 print filetype
($mode,$link,$uid,$gid);
231 print strftime
("D%Y %m %d %H %M %S\n",localtime($mtime));
233 print "L$link\n" if defined $link;
242 my $fn = unquote
($_[0]);
243 my ($size) = ($_[1]?
int($_[1]):(stat($fn))[7]);
245 print "### 501 Is directory\n" and return if -d
$fn;
246 sysopen(FH
,$fn,O_RDONLY
) || ($error = $!);
248 sysseek(FH
,int($_[2]),0) || do { close(FH
); $error ||= $!; };
250 print "### 500 $error\n" and return if $error;
257 while ($size > 32768 && ($read = sysread(FH
,$buffer,32768)) > 0) {
258 #print DEBUG "$size left, $read read\n";
262 while ($size > 0 && ($read = sysread(FH
,$buffer,$size)) > 0) {
263 #print DEBUG "$size left, $read read\n";
271 $error ||= $! if $read <= 0;
276 print "### 500 $error\n";
281 my $size = int($_[0]);
282 my $fn = unquote
($_[1]);
283 #print DEBUG "write_loop called $size size, $fn fn, $_[2]\n";
285 sysopen(FH
,$fn,$_[2]) || do { print "### 400 $!\n"; return; };
286 eval { flock(FH
,2); };
288 sysseek(FH
,int($_[3]),0) || do { close(FH
);print "### 400 $!\n"; return; };
294 while ($size > 32768 && ($read = read(STDIN
,$buffer,32768)) > 0) {
295 #print DEBUG "$size left, $read read\n";
297 $error ||= $! if (syswrite(FH
,$buffer,$read) != $read);
299 while ($size > 0 && ($read = read(STDIN
,$buffer,$size)) > 0) {
300 #print DEBUG "$size left, $read read\n";
302 $error ||= $! if (syswrite(FH
,$buffer,$read) != $read);
308 print "### 500 $error\n";
312 sub unquote
{ $_ = shift; s/\\(.)/$1/g; return $_; }
315 my ($mode,$link,$uid,$gid) = @_;
318 -f _
&& do { $result .= '-'; last; };
319 -d _
&& do { $result .= 'd'; last; };
320 defined($link) && do { $result .= 'l'; last; };
321 -c _
&& do { $result .= 'c'; last; };
322 -b _
&& do { $result .= 'b'; last; };
323 -S _
&& do { $result .= 's'; last; };
324 -p _
&& do { $result .= 'p'; last; };
325 $result .= '?'; last;
327 $result .= ($mode & 0400?
'r':'-');
328 $result .= ($mode & 0200?
'w':'-');
329 $result .= ($mode & 0100?
($mode&04000?
's':'x'):($mode&04000?
'S':'-'));
330 $result .= ($mode & 0040?
'r':'-');
331 $result .= ($mode & 0020?
'w':'-');
332 $result .= ($mode & 0010?
($mode&02000?
's':'x'):($mode&02000?
'S':'-'));
333 $result .= ($mode & 0004?
'r':'-');
334 $result .= ($mode & 0002?
'w':'-');
335 $result .= ($mode & 0001?
($mode&01000?
't':'x'):($mode&01000?
'T':'-'));
338 $result .= (getpwuid($uid)||$uid);
340 $result .= (getgrgid($gid)||$gid);
347 return "Minode/directory\n" if -d
$fn;
350 return '' if (!defined $pid);
357 $type =~ s/[,; ].*//;
358 return '' if ($type !~ m/\//);
362 sysopen(NULL
,'/dev/null',O_RDWR
);
363 dup2
(fileno(NULL
),fileno(STDIN
));
364 dup2
(fileno(OUT
),fileno(STDOUT
));
365 dup2
(fileno(NULL
),fileno(STDERR
));
366 exec('/usr/bin/file','-i','-b','-L',$fn);