fish.c 44 KB

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