fish.c 40 KB

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