fish.c 44 KB

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