fish.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  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_strdup_printf (_("fish: Password is required for %s"), SUP.user);
  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. }