fish.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. /* Virtual File System: FISH implementation for transfering files over
  2. shell connections.
  3. Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
  4. 2007 Free Software Foundation, Inc.
  5. Written by: 1998 Pavel Machek
  6. Spaces fix: 2000 Michal Svec
  7. Derived from ftpfs.c.
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Library General Public License
  10. as published by the Free Software Foundation; either version 2 of
  11. the License, or (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU Library General Public License for more details.
  16. You should have received a copy of the GNU Library General Public
  17. License along with this program; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  19. /**
  20. * \file
  21. * \brief Source: Virtual File System: FISH implementation for transfering files over
  22. * shell connections
  23. * \author Pavel Machek
  24. * \author Michal Svec
  25. * \date 1998, 2000
  26. *
  27. * Derived from ftpfs.c
  28. * Read README.fish for protocol specification.
  29. *
  30. * Syntax of path is: \verbatim /#sh:user@host[:Cr]/path \endverbatim
  31. * where C means you want compressed connection,
  32. * and r means you want to use rsh
  33. *
  34. * Namespace: fish_vfs_ops exported.
  35. */
  36. /* Define this if your ssh can take -I option */
  37. #include <config.h>
  38. #include <errno.h>
  39. #include <fcntl.h>
  40. #include <pwd.h>
  41. #include <grp.h>
  42. #include <sys/time.h> /* gettimeofday() */
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include "lib/global.h"
  46. #include "lib/fs.h"
  47. #include "lib/tty/tty.h" /* enable/disable interrupt key */
  48. #include "lib/strescape.h"
  49. #include "lib/unixcompat.h"
  50. #include "src/wtools.h" /* message() */
  51. #include "src/main.h" /* print_vfs_message */
  52. #include "utilvfs.h"
  53. #include "xdirentry.h"
  54. #include "vfs.h"
  55. #include "vfs-impl.h"
  56. #include "gc.h" /* vfs_stamp_create */
  57. #include "netutil.h"
  58. #include "fish.h"
  59. int fish_directory_timeout = 900;
  60. #define DO_RESOLVE_SYMLINK 1
  61. #define DO_OPEN 2
  62. #define DO_FREE_RESOURCE 4
  63. #define FISH_FLAG_COMPRESSED 1
  64. #define FISH_FLAG_RSH 2
  65. #define OPT_FLUSH 1
  66. #define OPT_IGNORE_ERROR 2
  67. /*
  68. * Reply codes.
  69. */
  70. #define PRELIM 1 /* positive preliminary */
  71. #define COMPLETE 2 /* positive completion */
  72. #define CONTINUE 3 /* positive intermediate */
  73. #define TRANSIENT 4 /* transient negative completion */
  74. #define ERROR 5 /* permanent negative completion */
  75. /* command wait_flag: */
  76. #define NONE 0x00
  77. #define WAIT_REPLY 0x01
  78. #define WANT_STRING 0x02
  79. static char reply_str[80];
  80. static struct vfs_class vfs_fish_ops;
  81. static int
  82. fish_command (struct vfs_class *me, struct vfs_s_super *super,
  83. int wait_reply, const char *fmt, ...) __attribute__ ((format (__printf__, 4, 5)));
  84. static int
  85. fish_decode_reply (char *s, int was_garbage)
  86. {
  87. int code;
  88. if (!sscanf (s, "%d", &code))
  89. {
  90. code = 500;
  91. return 5;
  92. }
  93. if (code < 100)
  94. return was_garbage ? ERROR : (!code ? COMPLETE : PRELIM);
  95. return code / 100;
  96. }
  97. /* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */
  98. static int
  99. fish_get_reply (struct vfs_class *me, int sock, char *string_buf, int string_len)
  100. {
  101. char answer[1024];
  102. int was_garbage = 0;
  103. for (;;)
  104. {
  105. if (!vfs_s_get_line (me, sock, answer, sizeof (answer), '\n'))
  106. {
  107. if (string_buf)
  108. *string_buf = 0;
  109. return 4;
  110. }
  111. if (strncmp (answer, "### ", 4))
  112. {
  113. was_garbage = 1;
  114. if (string_buf)
  115. g_strlcpy (string_buf, answer, string_len);
  116. }
  117. else
  118. return fish_decode_reply (answer + 4, was_garbage);
  119. }
  120. }
  121. #define SUP super->u.fish
  122. static int
  123. fish_command (struct vfs_class *me, struct vfs_s_super *super, int wait_reply, const char *fmt, ...)
  124. {
  125. va_list ap;
  126. char *str;
  127. int status;
  128. FILE *logfile = MEDATA->logfile;
  129. va_start (ap, fmt);
  130. str = g_strdup_vprintf (fmt, ap);
  131. va_end (ap);
  132. if (logfile)
  133. {
  134. size_t ret;
  135. ret = fwrite (str, strlen (str), 1, logfile);
  136. ret = fflush (logfile);
  137. }
  138. tty_enable_interrupt_key ();
  139. status = write (SUP.sockw, str, strlen (str));
  140. g_free (str);
  141. tty_disable_interrupt_key ();
  142. if (status < 0)
  143. return TRANSIENT;
  144. if (wait_reply)
  145. return fish_get_reply (me, SUP.sockr,
  146. (wait_reply & WANT_STRING) ? reply_str :
  147. NULL, sizeof (reply_str) - 1);
  148. return COMPLETE;
  149. }
  150. static void
  151. fish_free_archive (struct vfs_class *me, struct vfs_s_super *super)
  152. {
  153. if ((SUP.sockw != -1) || (SUP.sockr != -1))
  154. {
  155. print_vfs_message (_("fish: Disconnecting from %s"), super->name ? super->name : "???");
  156. fish_command (me, super, NONE, "#BYE\nexit\n");
  157. close (SUP.sockw);
  158. close (SUP.sockr);
  159. SUP.sockw = SUP.sockr = -1;
  160. }
  161. g_free (SUP.host);
  162. g_free (SUP.user);
  163. g_free (SUP.cwdir);
  164. g_free (SUP.password);
  165. }
  166. static void
  167. fish_pipeopen (struct vfs_s_super *super, const char *path, const char *argv[])
  168. {
  169. int fileset1[2], fileset2[2];
  170. int res;
  171. if ((pipe (fileset1) < 0) || (pipe (fileset2) < 0))
  172. vfs_die ("Cannot pipe(): %m.");
  173. if ((res = fork ()))
  174. {
  175. if (res < 0)
  176. vfs_die ("Cannot fork(): %m.");
  177. /* We are the parent */
  178. close (fileset1[0]);
  179. SUP.sockw = fileset1[1];
  180. close (fileset2[1]);
  181. SUP.sockr = fileset2[0];
  182. }
  183. else
  184. {
  185. res = dup2 (fileset1[0], 0);
  186. close (fileset1[0]);
  187. close (fileset1[1]);
  188. res = dup2 (fileset2[1], 1);
  189. close (2);
  190. /* stderr to /dev/null */
  191. res = open ("/dev/null", O_WRONLY);
  192. close (fileset2[0]);
  193. close (fileset2[1]);
  194. execvp (path, const_cast (char **, argv));
  195. _exit (3);
  196. }
  197. }
  198. /* The returned directory should always contain a trailing slash */
  199. static char *
  200. fish_getcwd (struct vfs_class *me, struct vfs_s_super *super)
  201. {
  202. if (fish_command (me, super, WANT_STRING, "#PWD\npwd; echo '### 200'\n") == COMPLETE)
  203. return g_strconcat (reply_str, "/", (char *) NULL);
  204. ERRNOR (EIO, NULL);
  205. }
  206. static int
  207. fish_open_archive_int (struct vfs_class *me, struct vfs_s_super *super)
  208. {
  209. {
  210. char gbuf[10];
  211. const char *argv[10]; /* All of 10 is used now */
  212. const char *xsh = (SUP.flags == FISH_FLAG_RSH ? "rsh" : "ssh");
  213. int i = 0;
  214. argv[i++] = xsh;
  215. if (SUP.flags == FISH_FLAG_COMPRESSED)
  216. argv[i++] = "-C";
  217. if (SUP.flags > FISH_FLAG_RSH)
  218. {
  219. argv[i++] = "-p";
  220. g_snprintf (gbuf, sizeof (gbuf), "%d", SUP.flags);
  221. argv[i++] = gbuf;
  222. }
  223. /*
  224. * Add the user name to the ssh command line only if it was explicitly
  225. * set in vfs URL. rsh/ssh will get current user by default
  226. * plus we can set convenient overrides in ~/.ssh/config (explicit -l
  227. * option breaks it for some)
  228. */
  229. if (SUP.user)
  230. {
  231. argv[i++] = "-l";
  232. argv[i++] = SUP.user;
  233. }
  234. else
  235. {
  236. /* The rest of the code assumes it to be a valid username */
  237. SUP.user = vfs_get_local_username ();
  238. }
  239. argv[i++] = SUP.host;
  240. argv[i++] = "echo FISH:; /bin/sh";
  241. argv[i++] = NULL;
  242. fish_pipeopen (super, xsh, argv);
  243. }
  244. {
  245. char answer[2048];
  246. print_vfs_message (_("fish: Waiting for initial line..."));
  247. if (!vfs_s_get_line (me, SUP.sockr, answer, sizeof (answer), ':'))
  248. ERRNOR (E_PROTO, -1);
  249. print_vfs_message ("%s", answer);
  250. if (strstr (answer, "assword"))
  251. {
  252. /* Currently, this does not work. ssh reads passwords from
  253. /dev/tty, not from stdin :-(. */
  254. message (D_ERROR, MSG_ERROR,
  255. _("Sorry, we cannot do password authenticated connections for now."));
  256. ERRNOR (EPERM, -1);
  257. if (!SUP.password)
  258. {
  259. char *p, *op;
  260. p = g_strconcat (_(" fish: Password required for "), SUP.user, " ", (char *) NULL);
  261. op = vfs_get_password (p);
  262. g_free (p);
  263. if (op == NULL)
  264. ERRNOR (EPERM, -1);
  265. SUP.password = op;
  266. }
  267. print_vfs_message (_("fish: Sending password..."));
  268. {
  269. size_t str_len;
  270. str_len = strlen (SUP.password);
  271. if ((write (SUP.sockw, SUP.password, str_len) != (ssize_t) str_len)
  272. || (write (SUP.sockw, "\n", 1) != 1))
  273. {
  274. ERRNOR (EIO, -1);
  275. }
  276. }
  277. }
  278. }
  279. print_vfs_message (_("fish: Sending initial line..."));
  280. /*
  281. * Run `start_fish_server'. If it doesn't exist - no problem,
  282. * we'll talk directly to the shell.
  283. */
  284. if (fish_command
  285. (me, super, WAIT_REPLY,
  286. "#FISH\necho; start_fish_server 2>&1; echo '### 200'\n") != COMPLETE)
  287. ERRNOR (E_PROTO, -1);
  288. print_vfs_message (_("fish: Handshaking version..."));
  289. if (fish_command (me, super, WAIT_REPLY, "#VER 0.0.0\necho '### 000'\n") != COMPLETE)
  290. ERRNOR (E_PROTO, -1);
  291. /* Set up remote locale to C, otherwise dates cannot be recognized */
  292. if (fish_command
  293. (me, super, WAIT_REPLY,
  294. "LANG=C; LC_ALL=C; LC_TIME=C\n"
  295. "export LANG; export LC_ALL; export LC_TIME\n" "echo '### 200'\n") != COMPLETE)
  296. ERRNOR (E_PROTO, -1);
  297. print_vfs_message (_("fish: Setting up current directory..."));
  298. SUP.cwdir = fish_getcwd (me, super);
  299. print_vfs_message (_("fish: Connected, home %s."), SUP.cwdir);
  300. #if 0
  301. super->name = g_strconcat ("/#sh:", SUP.user, "@", SUP.host, "/", (char *) NULL);
  302. #endif
  303. super->name = g_strdup (PATH_SEP_STR);
  304. super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
  305. return 0;
  306. }
  307. static int
  308. fish_open_archive (struct vfs_class *me, struct vfs_s_super *super,
  309. const char *archive_name, char *op)
  310. {
  311. char *host, *user, *password, *p;
  312. int flags;
  313. (void) archive_name;
  314. p = vfs_split_url (strchr (op, ':') + 1, &host, &user, &flags,
  315. &password, 0, URL_NOSLASH | URL_USE_ANONYMOUS);
  316. g_free (p);
  317. SUP.host = host;
  318. SUP.user = user;
  319. SUP.flags = flags;
  320. if (!strncmp (op, "rsh:", 4))
  321. SUP.flags = FISH_FLAG_RSH;
  322. SUP.cwdir = NULL;
  323. if (password)
  324. SUP.password = password;
  325. return fish_open_archive_int (me, super);
  326. }
  327. static int
  328. fish_archive_same (struct vfs_class *me, struct vfs_s_super *super,
  329. const char *archive_name, char *op, void *cookie)
  330. {
  331. char *host, *user;
  332. int flags;
  333. int result;
  334. (void) me;
  335. (void) archive_name;
  336. (void) cookie;
  337. op = vfs_split_url (strchr (op, ':') + 1, &host, &user, &flags, 0, 0,
  338. URL_NOSLASH | URL_USE_ANONYMOUS);
  339. g_free (op);
  340. if (user == NULL)
  341. user = vfs_get_local_username ();
  342. result = ((strcmp (host, SUP.host) == 0)
  343. && (strcmp (user, SUP.user) == 0) && (flags == SUP.flags));
  344. g_free (host);
  345. g_free (user);
  346. return result;
  347. }
  348. static int
  349. fish_dir_load (struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path)
  350. {
  351. struct vfs_s_super *super = dir->super;
  352. char buffer[8192];
  353. struct vfs_s_entry *ent = NULL;
  354. FILE *logfile;
  355. char *quoted_path;
  356. int reply_code;
  357. gchar *shell_commands;
  358. #if 0
  359. /*
  360. * Simple FISH debug interface :]
  361. */
  362. if (!(MEDATA->logfile))
  363. {
  364. MEDATA->logfile = fopen ("/tmp/mc-FISH.sh", "w");
  365. }
  366. #endif /* 0 */
  367. logfile = MEDATA->logfile;
  368. print_vfs_message (_("fish: Reading directory %s..."), remote_path);
  369. gettimeofday (&dir->timestamp, NULL);
  370. dir->timestamp.tv_sec += fish_directory_timeout;
  371. quoted_path = strutils_shell_escape (remote_path);
  372. /* *INDENT-OFF* */
  373. shell_commands = g_strconcat (
  374. "#LIST /%s\n"
  375. "if `perl -v > /dev/null 2>&1` ; then\n"
  376. "perl -e '\n"
  377. "use strict;\n"
  378. "use POSIX;\n"
  379. "use Fcntl;\n"
  380. "use POSIX \":fcntl_h\"; #S_ISLNK was here until 5.6\n"
  381. "import Fcntl \":mode\" unless defined &S_ISLNK; #and is now here\n"
  382. "my $dirname = $ARGV[0];\n"
  383. "if (opendir ( DIR, $dirname )) {\n"
  384. "while( (my $filename = readdir(DIR))){\n"
  385. "my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = lstat(\"$dirname/$filename\");\n"
  386. "my $mloctime= strftime(\"%%m-%%d-%%Y %%H:%%M\", localtime $mtime);\n"
  387. ,
  388. "my $strutils_shell_escape_regex = s/([;<>\\*\\|`&\\$!#\\(\\)\\[\\]\\{\\}:'\\''\"\\ \\\\])/\\\\$1/g;\n"
  389. "my $e_filename = $filename;\n"
  390. "$e_filename =~ $strutils_shell_escape_regex;\n"
  391. "if (S_ISLNK($mode) ) {\n"
  392. "my $linkname = readlink (\"$dirname/$filename\");\n"
  393. "$linkname =~ $strutils_shell_escape_regex;\n"
  394. "\n"
  395. "printf(\"R%%o %%o $uid.$gid\\n" "S$size\\n"
  396. "d$mloctime\\n"
  397. ":\\\"$e_filename\\\" -> \\\"$linkname\\\"\\n"
  398. "\\n\", S_IMODE($mode), S_IFMT($mode));\n"
  399. "} else {\n"
  400. "printf(\"R%%o %%o $uid.$gid\\n"
  401. "S$size\\n"
  402. "d$mloctime\\n"
  403. ":\\\"$e_filename\\\"\\n"
  404. "\\n\", S_IMODE($mode), S_IFMT($mode));\n"
  405. "}}\n"
  406. ,
  407. "printf(\"### 200\\n\");\n"
  408. "closedir(DIR);\n"
  409. "} else {\n"
  410. "printf(\"### 500\\n\");\n"
  411. "}\n"
  412. "exit 0\n"
  413. "' /%s ||\n" /* ARGV[0] - path to browse */
  414. " echo '### 500'\n" /* do not hang if perl is to eval it */
  415. "elif `ls -1 /%s >/dev/null 2>&1` ; then\n"
  416. "if `ls -Q /%s >/dev/null 2>&1`; then\n"
  417. "LSOPT=\"-Qlan\";\n"
  418. "ADD=0;\n"
  419. "else\n"
  420. "LSOPT=\"-lan\";\n"
  421. "ADD=1;\n"
  422. "fi\n"
  423. "ls $LSOPT /%s 2>/dev/null | grep '^[^cbt]' | (\n"
  424. "while read p l u g s m d y n n2 n3; do\n"
  425. "if test \"$m\" = \"0\" ; then \n"
  426. "s=$d; m=$y; d=$n y=$n2; n=$n3\n"
  427. "else\n"
  428. "n=$n\" \"$n2\" \"$n3\n"
  429. "fi\n"
  430. "if [ $ADD = 0 ]; then\n"
  431. "echo \"P$p $u.$g\nS$s\nd$m $d $y\n:$n\n\"\n"
  432. "elif `sed --version >/dev/null 2>&1` ; then\n"
  433. "file=`echo $n | sed -e 's#^\\(.*\\) -> \\(.*\\)#\\1\" -> \"\\2#'`\n",
  434. "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$file\"\n\"\n"
  435. "else\n"
  436. "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$n\"\n\"\n"
  437. "fi\n"
  438. "done )\n"
  439. "ls $LSOPT /%s 2>/dev/null | grep '^[cb]' | (\n"
  440. "while read p l u g a i m d y n n2 n3; do\n"
  441. "if test \"$a\" = \"0\" ; then \n"
  442. "a=$m; i=$d; m=$y; d=$n y=$n2; n=$n3\n"
  443. "else\n"
  444. "n=$n\" \"$n2\" \"$n3\n"
  445. "fi\n"
  446. "if [ $ADD = 0 ]; then\n"
  447. "echo \"P$p $u.$g\nE$a$i\nd$m $d $y\n:$n\n\"\n"
  448. "elif `sed --version >/dev/null 2>&1` ; then\n"
  449. "file=`echo $n | sed -e 's#^\\(.*\\) -> \\(.*\\)#\\1\" -> \"\\2#'`\n"
  450. "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$file\"\n\"\n"
  451. "else\n"
  452. "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$n\"\n\"\n"
  453. "fi\n"
  454. "done)\n"
  455. "echo '### 200'\n"
  456. "else\n"
  457. "echo '### 500'\n"
  458. "fi\n"
  459. ,
  460. (char *) NULL);
  461. /* *INDENT-ON* */
  462. fish_command (me, super, NONE, shell_commands,
  463. quoted_path, quoted_path, quoted_path, quoted_path, quoted_path, quoted_path);
  464. g_free (shell_commands);
  465. g_free (quoted_path);
  466. ent = vfs_s_generate_entry (me, NULL, dir, 0);
  467. while (1)
  468. {
  469. int res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), SUP.sockr);
  470. if ((!res) || (res == EINTR))
  471. {
  472. vfs_s_free_entry (me, ent);
  473. me->verrno = ECONNRESET;
  474. goto error;
  475. }
  476. if (logfile)
  477. {
  478. fputs (buffer, logfile);
  479. fputs ("\n", logfile);
  480. fflush (logfile);
  481. }
  482. if (!strncmp (buffer, "### ", 4))
  483. break;
  484. if ((!buffer[0]))
  485. {
  486. if (ent->name)
  487. {
  488. vfs_s_insert_entry (me, dir, ent);
  489. ent = vfs_s_generate_entry (me, NULL, dir, 0);
  490. }
  491. continue;
  492. }
  493. #define ST ent->ino->st
  494. switch (buffer[0])
  495. {
  496. case ':':
  497. {
  498. char *temp;
  499. char *data_start = buffer + 1;
  500. char *filename = data_start;
  501. char *linkname = data_start;
  502. char *filename_bound = filename + strlen (filename);
  503. char *linkname_bound = filename_bound;
  504. if (!strcmp (data_start, "\".\"") || !strcmp (data_start, "\"..\""))
  505. break; /* We'll do "." and ".." ourselves */
  506. if (S_ISLNK (ST.st_mode))
  507. {
  508. /* we expect: "escaped-name" -> "escaped-name"
  509. // -> cannot occur in filenames,
  510. // because it will be escaped to -\> */
  511. if (*filename == '"')
  512. ++filename;
  513. linkname = strstr (filename, "\" -> \"");
  514. if (!linkname)
  515. {
  516. /* broken client, or smth goes wrong */
  517. linkname = filename_bound;
  518. if (filename_bound > filename && *(filename_bound - 1) == '"')
  519. --filename_bound; /* skip trailing " */
  520. }
  521. else
  522. {
  523. filename_bound = linkname;
  524. linkname += 6; /* strlen ("\" -> \"") */
  525. if (*(linkname_bound - 1) == '"')
  526. --linkname_bound; /* skip trailing " */
  527. }
  528. ent->name = str_dup_range (filename, filename_bound);
  529. temp = ent->name;
  530. ent->name = strutils_shell_unescape (ent->name);
  531. g_free (temp);
  532. ent->ino->linkname = str_dup_range (linkname, linkname_bound);
  533. temp = ent->ino->linkname;
  534. ent->ino->linkname = strutils_shell_unescape (ent->ino->linkname);
  535. g_free (temp);
  536. }
  537. else
  538. {
  539. /* we expect: "escaped-name" */
  540. if (filename_bound - filename > 2)
  541. {
  542. /*
  543. there is at least 2 "
  544. and we skip them
  545. */
  546. if (*filename == '"')
  547. ++filename;
  548. if (*(filename_bound - 1) == '"')
  549. --filename_bound;
  550. }
  551. ent->name = str_dup_range (filename, filename_bound);
  552. temp = ent->name;
  553. ent->name = strutils_shell_unescape (ent->name);
  554. g_free (temp);
  555. }
  556. break;
  557. }
  558. case 'S':
  559. #ifdef HAVE_ATOLL
  560. ST.st_size = (off_t) atoll (buffer + 1);
  561. #else
  562. ST.st_size = (off_t) atof (buffer + 1);
  563. #endif
  564. break;
  565. case 'P':
  566. {
  567. size_t skipped;
  568. vfs_parse_filemode (buffer + 1, &skipped, &ST.st_mode);
  569. break;
  570. }
  571. case 'R':
  572. {
  573. /*
  574. raw filemode:
  575. we expect: Roctal-filemode octal-filetype uid.gid
  576. */
  577. size_t skipped;
  578. vfs_parse_raw_filemode (buffer + 1, &skipped, &ST.st_mode);
  579. break;
  580. }
  581. case 'd':
  582. {
  583. vfs_split_text (buffer + 1);
  584. if (!vfs_parse_filedate (0, &ST.st_ctime))
  585. break;
  586. ST.st_atime = ST.st_mtime = ST.st_ctime;
  587. }
  588. break;
  589. case 'D':
  590. {
  591. struct tm tim;
  592. if (sscanf (buffer + 1, "%d %d %d %d %d %d", &tim.tm_year, &tim.tm_mon,
  593. &tim.tm_mday, &tim.tm_hour, &tim.tm_min, &tim.tm_sec) != 6)
  594. break;
  595. ST.st_atime = ST.st_mtime = ST.st_ctime = mktime (&tim);
  596. }
  597. break;
  598. case 'E':
  599. {
  600. int maj, min;
  601. if (sscanf (buffer + 1, "%d,%d", &maj, &min) != 2)
  602. break;
  603. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  604. ST.st_rdev = makedev (maj, min);
  605. #endif
  606. }
  607. }
  608. }
  609. vfs_s_free_entry (me, ent);
  610. reply_code = fish_decode_reply (buffer + 4, 0);
  611. if (reply_code == COMPLETE)
  612. {
  613. g_free (SUP.cwdir);
  614. SUP.cwdir = g_strdup (remote_path);
  615. print_vfs_message (_("%s: done."), me->name);
  616. return 0;
  617. }
  618. else if (reply_code == ERROR)
  619. {
  620. me->verrno = EACCES;
  621. }
  622. else
  623. {
  624. me->verrno = E_REMOTE;
  625. }
  626. error:
  627. print_vfs_message (_("%s: failure"), me->name);
  628. return -1;
  629. }
  630. static int
  631. fish_file_store (struct vfs_class *me, struct vfs_s_fh *fh, char *name, char *localname)
  632. {
  633. struct vfs_s_super *super = FH_SUPER;
  634. int n, total;
  635. char buffer[8192];
  636. struct stat s;
  637. int was_error = 0;
  638. int h;
  639. char *quoted_name;
  640. h = open (localname, O_RDONLY);
  641. if (h == -1)
  642. ERRNOR (EIO, -1);
  643. if (fstat (h, &s) < 0)
  644. {
  645. close (h);
  646. ERRNOR (EIO, -1);
  647. }
  648. /* First, try this as stor:
  649. *
  650. * ( head -c number ) | ( cat > file; cat >/dev/null )
  651. *
  652. * If `head' is not present on the remote system, `dd' will be used.
  653. * Unfortunately, we cannot trust most non-GNU `head' implementations
  654. * even if `-c' options is supported. Therefore, we separate GNU head
  655. * (and other modern heads?) using `-q' and `-' . This causes another
  656. * implementations to fail (because of "incorrect options").
  657. *
  658. * Fallback is:
  659. *
  660. * rest=<number>
  661. * while [ $rest -gt 0 ]
  662. * do
  663. * cnt=`expr \( $rest + 255 \) / 256`
  664. * n=`dd bs=256 count=$cnt | tee -a <target_file> | wc -c`
  665. * rest=`expr $rest - $n`
  666. * done
  667. *
  668. * `dd' was not designed for full filling of input buffers,
  669. * and does not report exact number of bytes (not blocks).
  670. * Therefore a more complex shell script is needed.
  671. *
  672. * On some systems non-GNU head writes "Usage:" error report to stdout
  673. * instead of stderr. It makes impossible the use of "head || dd"
  674. * algorithm for file appending case, therefore just "dd" is used for it.
  675. */
  676. quoted_name = strutils_shell_escape (name);
  677. print_vfs_message (_("fish: store %s: sending command..."), quoted_name);
  678. /* FIXME: File size is limited to ULONG_MAX */
  679. if (!fh->u.fish.append)
  680. {
  681. /* *INDENT-OFF* */
  682. n = fish_command (me, super, WAIT_REPLY,
  683. "#STOR %lu /%s\n"
  684. "echo '### 001'\n"
  685. "file=/%s\n"
  686. "res=`exec 3>&1\n"
  687. "(\n"
  688. "head -c %lu -q - || echo DD >&3\n"
  689. ") 2>/dev/null | (\n"
  690. "cat > $file\n"
  691. "cat > /dev/null\n"
  692. ")`; [ \"$res\" = DD ] && {\n"
  693. "> \"$file\"\n"
  694. "rest=%lu\n"
  695. "while [ $rest -gt 0 ]\n"
  696. "do\n"
  697. " cnt=`expr \\( $rest + 255 \\) / 256`\n"
  698. " n=`dd bs=256 count=$cnt | tee -a \"$file\" | wc -c`\n"
  699. " rest=`expr $rest - $n`\n"
  700. "done\n"
  701. "}; echo '### 200'\n",
  702. (unsigned long) s.st_size, quoted_name,
  703. quoted_name, (unsigned long) s.st_size, (unsigned long) s.st_size);
  704. /* *INDENT-ON* */
  705. }
  706. else
  707. {
  708. /* *INDENT-OFF* */
  709. n = fish_command (me, super, WAIT_REPLY,
  710. "#STOR %lu /%s\n"
  711. "echo '### 001'\n"
  712. "{\n"
  713. "file=/%s\n"
  714. "rest=%lu\n"
  715. "while [ $rest -gt 0 ]\n"
  716. "do\n"
  717. " cnt=`expr \\( $rest + 255 \\) / 256`\n"
  718. " n=`dd bs=256 count=$cnt | tee -a $file | wc -c`\n"
  719. " rest=`expr $rest - $n`\n"
  720. "done\n"
  721. "}; echo '### 200'\n",
  722. (unsigned long) s.st_size, quoted_name,
  723. quoted_name, (unsigned long) s.st_size);
  724. /* *INDENT-ON* */
  725. }
  726. if (n != PRELIM)
  727. {
  728. close (h);
  729. ERRNOR (E_REMOTE, -1);
  730. }
  731. total = 0;
  732. while (1)
  733. {
  734. int t;
  735. while ((n = read (h, buffer, sizeof (buffer))) < 0)
  736. {
  737. if ((errno == EINTR) && tty_got_interrupt ())
  738. continue;
  739. print_vfs_message (_("fish: Local read failed, sending zeros"));
  740. close (h);
  741. h = open ("/dev/zero", O_RDONLY);
  742. }
  743. if (n == 0)
  744. break;
  745. if ((t = write (SUP.sockw, buffer, n)) != n)
  746. {
  747. if (t == -1)
  748. {
  749. me->verrno = errno;
  750. }
  751. else
  752. {
  753. me->verrno = EIO;
  754. }
  755. goto error_return;
  756. }
  757. tty_disable_interrupt_key ();
  758. total += n;
  759. print_vfs_message (_("fish: storing %s %d (%lu)"),
  760. was_error ? _("zeros") : _("file"), total, (unsigned long) s.st_size);
  761. }
  762. close (h);
  763. g_free (quoted_name);
  764. if ((fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE) || was_error)
  765. ERRNOR (E_REMOTE, -1);
  766. return 0;
  767. error_return:
  768. close (h);
  769. fish_get_reply (me, SUP.sockr, NULL, 0);
  770. g_free (quoted_name);
  771. return -1;
  772. }
  773. static int
  774. fish_linear_start (struct vfs_class *me, struct vfs_s_fh *fh, off_t offset)
  775. {
  776. char *name;
  777. char *quoted_name;
  778. if (offset)
  779. ERRNOR (E_NOTSUPP, 0);
  780. name = vfs_s_fullpath (me, fh->ino);
  781. if (name == NULL)
  782. return 0;
  783. quoted_name = strutils_shell_escape (name);
  784. g_free (name);
  785. fh->u.fish.append = 0;
  786. /*
  787. * Check whether the remote file is readable by using `dd' to copy
  788. * a single byte from the remote file to /dev/null. If `dd' completes
  789. * with exit status of 0 use `cat' to send the file contents to the
  790. * standard output (i.e. over the network).
  791. */
  792. /* *INDENT-OFF* */
  793. offset = fish_command (me, FH_SUPER, WANT_STRING,
  794. "#RETR /%s\n"
  795. "if dd if=/%s of=/dev/null bs=1 count=1 2>/dev/null ;\n"
  796. "then\n"
  797. "ls -ln /%s 2>/dev/null | (\n"
  798. "read p l u g s r\n"
  799. "echo $s\n"
  800. ")\n"
  801. "echo '### 100'\n"
  802. "cat /%s\n"
  803. "echo '### 200'\n"
  804. "else\n"
  805. "echo '### 500'\n"
  806. "fi\n",
  807. quoted_name, quoted_name, quoted_name, quoted_name);
  808. /* *INDENT-ON* */
  809. g_free (quoted_name);
  810. if (offset != PRELIM)
  811. ERRNOR (E_REMOTE, 0);
  812. fh->linear = LS_LINEAR_OPEN;
  813. fh->u.fish.got = 0;
  814. errno = 0;
  815. #if SIZEOF_OFF_T == SIZEOF_LONG
  816. fh->u.fish.total = strtol (reply_str, NULL, 10);
  817. #else
  818. fh->u.fish.total = strtoll (reply_str, NULL, 10);
  819. #endif
  820. if (errno != 0)
  821. ERRNOR (E_REMOTE, 0);
  822. return 1;
  823. }
  824. static void
  825. fish_linear_abort (struct vfs_class *me, struct vfs_s_fh *fh)
  826. {
  827. struct vfs_s_super *super = FH_SUPER;
  828. char buffer[8192];
  829. int n;
  830. print_vfs_message (_("Aborting transfer..."));
  831. do
  832. {
  833. n = MIN (8192, fh->u.fish.total - fh->u.fish.got);
  834. if (n)
  835. {
  836. if ((n = read (SUP.sockr, buffer, n)) < 0)
  837. return;
  838. fh->u.fish.got += n;
  839. }
  840. }
  841. while (n);
  842. if (fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE)
  843. print_vfs_message (_("Error reported after abort."));
  844. else
  845. print_vfs_message (_("Aborted transfer would be successful."));
  846. }
  847. static int
  848. fish_linear_read (struct vfs_class *me, struct vfs_s_fh *fh, void *buf, int len)
  849. {
  850. struct vfs_s_super *super = FH_SUPER;
  851. int n = 0;
  852. len = MIN (fh->u.fish.total - fh->u.fish.got, len);
  853. tty_disable_interrupt_key ();
  854. while (len && ((n = read (SUP.sockr, buf, len)) < 0))
  855. {
  856. if ((errno == EINTR) && !tty_got_interrupt ())
  857. continue;
  858. break;
  859. }
  860. tty_enable_interrupt_key ();
  861. if (n > 0)
  862. fh->u.fish.got += n;
  863. if (n < 0)
  864. fish_linear_abort (me, fh);
  865. if ((!n) && ((fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE)))
  866. ERRNOR (E_REMOTE, -1);
  867. ERRNOR (errno, n);
  868. }
  869. static void
  870. fish_linear_close (struct vfs_class *me, struct vfs_s_fh *fh)
  871. {
  872. if (fh->u.fish.total != fh->u.fish.got)
  873. fish_linear_abort (me, fh);
  874. }
  875. static int
  876. fish_ctl (void *fh, int ctlop, void *arg)
  877. {
  878. (void) arg;
  879. (void) fh;
  880. (void) ctlop;
  881. return 0;
  882. #if 0
  883. switch (ctlop)
  884. {
  885. case VFS_CTL_IS_NOTREADY:
  886. {
  887. int v;
  888. if (!FH->linear)
  889. vfs_die ("You may not do this");
  890. if (FH->linear == LS_LINEAR_CLOSED || FH->linear == LS_LINEAR_PREOPEN)
  891. return 0;
  892. v = vfs_s_select_on_two (FH_SUPER->u.fish.sockr, 0);
  893. if (((v < 0) && (errno == EINTR)) || v == 0)
  894. return 1;
  895. return 0;
  896. }
  897. default:
  898. return 0;
  899. }
  900. #endif
  901. }
  902. static int
  903. fish_send_command (struct vfs_class *me, struct vfs_s_super *super, const char *cmd, int flags)
  904. {
  905. int r;
  906. r = fish_command (me, super, WAIT_REPLY, "%s", cmd);
  907. vfs_stamp_create (&vfs_fish_ops, super);
  908. if (r != COMPLETE)
  909. ERRNOR (E_REMOTE, -1);
  910. if (flags & OPT_FLUSH)
  911. vfs_s_invalidate (me, super);
  912. return 0;
  913. }
  914. #define PREFIX \
  915. char buf[BUF_LARGE]; \
  916. const char *crpath; \
  917. char *rpath, *mpath = g_strdup (path); \
  918. struct vfs_s_super *super; \
  919. if (!(crpath = vfs_s_get_path_mangle (me, mpath, &super, 0))) \
  920. { \
  921. g_free (mpath); \
  922. return -1; \
  923. } \
  924. rpath = strutils_shell_escape(crpath); \
  925. g_free (mpath);
  926. #define POSTFIX(flags) \
  927. g_free (rpath); \
  928. return fish_send_command(me, super, buf, flags);
  929. static int
  930. fish_chmod (struct vfs_class *me, const char *path, int mode)
  931. {
  932. /* *INDENT-OFF* */
  933. PREFIX
  934. g_snprintf (buf, sizeof (buf),
  935. "#CHMOD %4.4o /%s\n"
  936. "if chmod %4.4o /%s 2>/dev/null; then\n"
  937. "echo '### 000'\n"
  938. "else\n"
  939. "echo '### 500'\n"
  940. "fi\n",
  941. mode & 07777, rpath, mode & 07777, rpath);
  942. POSTFIX (OPT_FLUSH);
  943. /* *INDENT-ON* */
  944. }
  945. /* *INDENT-OFF* */
  946. #define FISH_OP(name, string) \
  947. static int fish_##name (struct vfs_class *me, const char *path1, const char *path2) \
  948. { \
  949. char buf[BUF_LARGE]; \
  950. const char *crpath1, *crpath2; \
  951. char *rpath1, *rpath2, *mpath1, *mpath2; \
  952. struct vfs_s_super *super1, *super2; \
  953. if (!(crpath1 = vfs_s_get_path_mangle (me, mpath1 = g_strdup(path1), &super1, 0))) \
  954. { \
  955. g_free (mpath1); \
  956. return -1; \
  957. } \
  958. if (!(crpath2 = vfs_s_get_path_mangle (me, mpath2 = g_strdup(path2), &super2, 0))) \
  959. { \
  960. g_free (mpath1); \
  961. g_free (mpath2); \
  962. return -1; \
  963. } \
  964. rpath1 = strutils_shell_escape (crpath1); \
  965. g_free (mpath1); \
  966. rpath2 = strutils_shell_escape (crpath2); \
  967. g_free (mpath2); \
  968. g_snprintf (buf, sizeof(buf), string "\n", rpath1, rpath2, rpath1, rpath2); \
  969. g_free (rpath1); \
  970. g_free (rpath2); \
  971. return fish_send_command(me, super2, buf, OPT_FLUSH); \
  972. }
  973. FISH_OP (rename,
  974. "#RENAME /%s /%s\n"
  975. "if mv /%s /%s 2>/dev/null; then\n"
  976. "echo '### 000'\n"
  977. "else\n"
  978. "echo '### 500'\n"
  979. "fi\n")
  980. FISH_OP (link,
  981. "#LINK /%s /%s\n"
  982. "if ln /%s /%s 2>/dev/null; then\n"
  983. "echo '### 000'\n"
  984. "else\n"
  985. "echo '### 500'\n"
  986. "fi\n")
  987. /* *INDENT-ON* */
  988. static int
  989. fish_symlink (struct vfs_class *me, const char *setto, const char *path)
  990. {
  991. char *qsetto;
  992. PREFIX qsetto = strutils_shell_escape (setto);
  993. /* *INDENT-OFF* */
  994. g_snprintf (buf, sizeof (buf),
  995. "#SYMLINK %s /%s\n"
  996. "if ln -s %s /%s 2>/dev/null; then\n"
  997. "echo '### 000'\n"
  998. "else\n"
  999. "echo '### 500'\n"
  1000. "fi\n",
  1001. qsetto, rpath, qsetto, rpath);
  1002. /* *INDENT-ON* */
  1003. g_free (qsetto);
  1004. POSTFIX (OPT_FLUSH);
  1005. }
  1006. static int
  1007. fish_chown (struct vfs_class *me, const char *path, int owner, int group)
  1008. {
  1009. char *sowner, *sgroup;
  1010. struct passwd *pw;
  1011. struct group *gr;
  1012. if ((pw = getpwuid (owner)) == NULL)
  1013. return 0;
  1014. if ((gr = getgrgid (group)) == NULL)
  1015. return 0;
  1016. sowner = pw->pw_name;
  1017. sgroup = gr->gr_name;
  1018. {
  1019. /* *INDENT-OFF* */
  1020. PREFIX
  1021. g_snprintf (buf, sizeof (buf),
  1022. "#CHOWN %s:%s /%s\n"
  1023. "if chown %s:%s /%s 2>/dev/null; then\n"
  1024. "echo '### 000'\n"
  1025. "else\n"
  1026. "echo '### 500'\n"
  1027. "fi\n",
  1028. sowner, sgroup, rpath, sowner, sgroup, rpath);
  1029. /* *INDENT-ON* */
  1030. fish_send_command (me, super, buf, OPT_FLUSH);
  1031. /* FIXME: what should we report if chgrp succeeds but chown fails? */
  1032. /* fish_send_command(me, super, buf, OPT_FLUSH); */
  1033. POSTFIX (OPT_FLUSH)}
  1034. }
  1035. static int
  1036. fish_unlink (struct vfs_class *me, const char *path)
  1037. {
  1038. /* *INDENT-OFF* */
  1039. PREFIX
  1040. g_snprintf (buf, sizeof (buf),
  1041. "#DELE /%s\n"
  1042. "if rm -f /%s 2>/dev/null; then\n"
  1043. "echo '### 000'\n"
  1044. "else\n"
  1045. "echo '### 500'\n"
  1046. "fi\n",
  1047. rpath, rpath);
  1048. /* *INDENT-ON* */
  1049. POSTFIX (OPT_FLUSH);
  1050. }
  1051. static int
  1052. fish_exists (struct vfs_class *me, const char *path)
  1053. {
  1054. /* *INDENT-OFF* */
  1055. PREFIX
  1056. g_snprintf (buf, sizeof (buf),
  1057. "#ISEXISTS /%s\n"
  1058. "ls -l /%s >/dev/null 2>/dev/null\n"
  1059. "echo '### '$?\n",
  1060. rpath, rpath);
  1061. /* *INDENT-ON* */
  1062. g_free (rpath);
  1063. if (fish_send_command (me, super, buf, OPT_FLUSH) == 0)
  1064. return 1;
  1065. return 0;
  1066. }
  1067. static int
  1068. fish_mkdir (struct vfs_class *me, const char *path, mode_t mode)
  1069. {
  1070. int ret_code;
  1071. /* *INDENT-OFF* */
  1072. PREFIX (void) mode;
  1073. g_snprintf (buf, sizeof (buf),
  1074. "#MKD /%s\n"
  1075. "if mkdir /%s 2>/dev/null; then\n"
  1076. "echo '### 000'\n"
  1077. "else\n"
  1078. "echo '### 500'\n"
  1079. "fi\n",
  1080. rpath, rpath);
  1081. /* *INDENT-ON* */
  1082. g_free (rpath);
  1083. ret_code = fish_send_command (me, super, buf, OPT_FLUSH);
  1084. if (ret_code != 0)
  1085. return ret_code;
  1086. if (!fish_exists (me, path))
  1087. {
  1088. ERRNOR (EACCES, -1);
  1089. }
  1090. return 0;
  1091. }
  1092. static int
  1093. fish_rmdir (struct vfs_class *me, const char *path)
  1094. {
  1095. /* *INDENT-OFF* */
  1096. PREFIX
  1097. g_snprintf (buf, sizeof (buf),
  1098. "#RMD /%s\n"
  1099. "if rmdir /%s 2>/dev/null; then\n"
  1100. "echo '### 000'\n"
  1101. "else\n"
  1102. "echo '### 500'\n"
  1103. "fi\n",
  1104. rpath, rpath);
  1105. /* *INDENT-ON* */
  1106. POSTFIX (OPT_FLUSH);
  1107. }
  1108. static int
  1109. fish_fh_open (struct vfs_class *me, struct vfs_s_fh *fh, int flags, int mode)
  1110. {
  1111. (void) mode;
  1112. fh->u.fish.append = 0;
  1113. /* File will be written only, so no need to retrieve it */
  1114. if (((flags & O_WRONLY) == O_WRONLY) && !(flags & (O_RDONLY | O_RDWR)))
  1115. {
  1116. fh->u.fish.append = flags & O_APPEND;
  1117. if (!fh->ino->localname)
  1118. {
  1119. int tmp_handle = vfs_mkstemps (&fh->ino->localname, me->name,
  1120. fh->ino->ent->name);
  1121. if (tmp_handle == -1)
  1122. return -1;
  1123. close (tmp_handle);
  1124. }
  1125. return 0;
  1126. }
  1127. if (!fh->ino->localname)
  1128. if (vfs_s_retrieve_file (me, fh->ino) == -1)
  1129. return -1;
  1130. if (!fh->ino->localname)
  1131. vfs_die ("retrieve_file failed to fill in localname");
  1132. return 0;
  1133. }
  1134. static void
  1135. fish_fill_names (struct vfs_class *me, fill_names_f func)
  1136. {
  1137. struct vfs_s_super *super = MEDATA->supers;
  1138. char *name;
  1139. char gbuf[10];
  1140. while (super)
  1141. {
  1142. const char *flags = "";
  1143. switch (SUP.flags)
  1144. {
  1145. case FISH_FLAG_RSH:
  1146. flags = ":r";
  1147. break;
  1148. case FISH_FLAG_COMPRESSED:
  1149. flags = ":C";
  1150. break;
  1151. default:
  1152. if (SUP.flags > FISH_FLAG_RSH)
  1153. {
  1154. break;
  1155. g_snprintf (gbuf, sizeof (gbuf), ":%d", SUP.flags);
  1156. flags = gbuf;
  1157. }
  1158. break;
  1159. }
  1160. name = g_strconcat ("/#sh:", SUP.user, "@", SUP.host, flags, "/", SUP.cwdir, (char *) NULL);
  1161. (*func) (name);
  1162. g_free (name);
  1163. super = super->next;
  1164. }
  1165. }
  1166. static void *
  1167. fish_open (struct vfs_class *me, const char *file, int flags, int mode)
  1168. {
  1169. /*
  1170. sorry, i've places hack here
  1171. cause fish don't able to open files with O_EXCL flag
  1172. */
  1173. flags &= ~O_EXCL;
  1174. return vfs_s_open (me, file, flags, mode);
  1175. }
  1176. void
  1177. init_fish (void)
  1178. {
  1179. static struct vfs_s_subclass fish_subclass;
  1180. tcp_init ();
  1181. fish_subclass.flags = VFS_S_REMOTE;
  1182. fish_subclass.archive_same = fish_archive_same;
  1183. fish_subclass.open_archive = fish_open_archive;
  1184. fish_subclass.free_archive = fish_free_archive;
  1185. fish_subclass.fh_open = fish_fh_open;
  1186. fish_subclass.dir_load = fish_dir_load;
  1187. fish_subclass.file_store = fish_file_store;
  1188. fish_subclass.linear_start = fish_linear_start;
  1189. fish_subclass.linear_read = fish_linear_read;
  1190. fish_subclass.linear_close = fish_linear_close;
  1191. vfs_s_init_class (&vfs_fish_ops, &fish_subclass);
  1192. vfs_fish_ops.name = "fish";
  1193. vfs_fish_ops.prefix = "sh:";
  1194. vfs_fish_ops.fill_names = fish_fill_names;
  1195. vfs_fish_ops.chmod = fish_chmod;
  1196. vfs_fish_ops.chown = fish_chown;
  1197. vfs_fish_ops.open = fish_open;
  1198. vfs_fish_ops.symlink = fish_symlink;
  1199. vfs_fish_ops.link = fish_link;
  1200. vfs_fish_ops.unlink = fish_unlink;
  1201. vfs_fish_ops.rename = fish_rename;
  1202. vfs_fish_ops.mkdir = fish_mkdir;
  1203. vfs_fish_ops.rmdir = fish_rmdir;
  1204. vfs_fish_ops.ctl = fish_ctl;
  1205. vfs_register_class (&vfs_fish_ops);
  1206. }