vfs.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843
  1. /* Virtual File System switch code
  2. Copyright (C) 1995 The Free Software Foundation
  3. Written by: 1995 Miguel de Icaza
  4. 1995 Jakub Jelinek
  5. 1998 Pavel Machek
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public License
  8. as published by the Free Software Foundation; either version 2 of
  9. the License, or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this program; if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  17. /* Warning: funtions like extfs_lstat() have right to destroy any
  18. * strings you pass to them. This is acutally ok as you g_strdup what
  19. * you are passing to them, anyway; still, beware. */
  20. /* Namespace: exports *many* functions with vfs_ prefix; exports
  21. parse_ls_lga and friends which do not have that prefix. */
  22. #include <config.h>
  23. #ifndef NO_SYSLOG_H
  24. # include <syslog.h>
  25. #endif
  26. #include <stdio.h>
  27. #include <stdlib.h> /* For atol() */
  28. #include <stdarg.h>
  29. #include <string.h>
  30. #include <errno.h>
  31. #include <sys/types.h>
  32. #include <signal.h>
  33. #include "utilvfs.h"
  34. #include "../src/panel.h" /* get_current_panel() */
  35. #include "../src/layout.h" /* get_current_type() */
  36. #include "../src/wtools.h" /* input_dialog() */
  37. #include "xdirentry.h"
  38. #include "vfs.h"
  39. #include "extfs.h" /* FIXME: we should not know anything about our modules */
  40. #include "names.h"
  41. #ifdef USE_NETCODE
  42. # include "tcputil.h"
  43. #endif
  44. int vfs_timeout = 60; /* VFS timeout in seconds */
  45. static int vfs_flags = 0; /* Flags */
  46. /* They keep track of the current directory */
  47. static vfs *current_vfs = &vfs_local_ops;
  48. static char *current_dir = NULL;
  49. /*
  50. * FIXME: this is broken. It depends on mc not crossing border on month!
  51. */
  52. static int current_mday;
  53. static int current_mon;
  54. static int current_year;
  55. /* FIXME: Open files managed by the vfs layer, should be dynamical */
  56. #define MAX_VFS_FILES 100
  57. static struct {
  58. void *fs_info;
  59. vfs *operations;
  60. } vfs_file_table [MAX_VFS_FILES];
  61. static int
  62. get_bucket (void)
  63. {
  64. int i;
  65. /* 0, 1, 2 are reserved file descriptors, while (DIR *) 0 means error */
  66. for (i = 3; i < MAX_VFS_FILES; i++){
  67. if (!vfs_file_table [i].fs_info)
  68. return i;
  69. }
  70. vfs_die ("No more virtual file handles");
  71. return 0;
  72. }
  73. /* vfs_local_ops needs to be the first one */
  74. static vfs *vfs_list = &vfs_local_ops;
  75. static int
  76. vfs_register (vfs *vfs)
  77. {
  78. if (!vfs)
  79. vfs_die("You can not register NULL.");
  80. if (vfs->init) /* vfs has own initialization function */
  81. if (!(*vfs->init)(vfs)) /* but it failed */
  82. return 0;
  83. vfs->next = vfs_list;
  84. vfs_list = vfs;
  85. return 1;
  86. }
  87. static vfs *
  88. vfs_type_from_op (char *path)
  89. {
  90. vfs *vfs;
  91. if (!path)
  92. vfs_die ("vfs_type_from_op got NULL: impossible");
  93. for (vfs = vfs_list; vfs != &vfs_local_ops; vfs = vfs->next){
  94. if (vfs->which) {
  95. if ((*vfs->which) (vfs, path) == -1)
  96. continue;
  97. return vfs;
  98. }
  99. if (!strncmp (path, vfs->prefix, strlen (vfs->prefix)))
  100. return vfs;
  101. }
  102. return NULL; /* shut up stupid gcc */
  103. }
  104. /* Strip known vfs suffixes from a filename (possible improvement: strip
  105. suffix from last path component).
  106. Returns a malloced string which has to be freed. */
  107. char *
  108. vfs_strip_suffix_from_filename (const char *filename)
  109. {
  110. vfs *vfs;
  111. char *semi;
  112. char *p;
  113. if (!filename)
  114. vfs_die("vfs_strip_suffix_from_path got NULL: impossible");
  115. p = g_strdup (filename);
  116. if (!(semi = strrchr (p, '#')))
  117. return p;
  118. for (vfs = vfs_list; vfs != &vfs_local_ops; vfs = vfs->next){
  119. if (vfs->which){
  120. if ((*vfs->which) (vfs, semi + 1) == -1)
  121. continue;
  122. *semi = '\0'; /* Found valid suffix */
  123. return p;
  124. }
  125. if (!strncmp (semi + 1, vfs->prefix, strlen (vfs->prefix))) {
  126. *semi = '\0'; /* Found valid suffix */
  127. return p;
  128. }
  129. }
  130. return p;
  131. }
  132. static int
  133. path_magic (const char *path)
  134. {
  135. struct stat buf;
  136. if (vfs_flags & FL_ALWAYS_MAGIC)
  137. return 1;
  138. if (!stat(path, &buf))
  139. return 0;
  140. return 1;
  141. }
  142. /*
  143. * Splits path '/p1#op/inpath' into inpath,op; returns which vfs it is.
  144. * What is left in path is p1. You still want to g_free(path), you DON'T
  145. * want to free neither *inpath nor *op
  146. */
  147. vfs *
  148. vfs_split (char *path, char **inpath, char **op)
  149. {
  150. char *semi;
  151. char *slash;
  152. vfs *ret;
  153. if (!path)
  154. vfs_die("Can not split NULL");
  155. semi = strrchr (path, '#');
  156. if (!semi || !path_magic(path))
  157. return NULL;
  158. slash = strchr (semi, PATH_SEP);
  159. *semi = 0;
  160. if (op)
  161. *op = NULL;
  162. if (inpath)
  163. *inpath = NULL;
  164. if (slash)
  165. *slash = 0;
  166. if ((ret = vfs_type_from_op (semi+1))){
  167. if (op)
  168. *op = semi + 1;
  169. if (inpath)
  170. *inpath = slash ? slash + 1 : NULL;
  171. return ret;
  172. }
  173. if (slash)
  174. *slash = PATH_SEP;
  175. ret = vfs_split (path, inpath, op);
  176. *semi = '#';
  177. return ret;
  178. }
  179. static vfs *
  180. vfs_rosplit (char *path)
  181. {
  182. char *semi;
  183. char *slash;
  184. vfs *ret;
  185. g_return_val_if_fail(path, NULL);
  186. semi = strrchr (path, '#');
  187. if (!semi || !path_magic (path))
  188. return NULL;
  189. slash = strchr (semi, PATH_SEP);
  190. *semi = 0;
  191. if (slash)
  192. *slash = 0;
  193. ret = vfs_type_from_op (semi+1);
  194. if (!ret && (vfs_flags & FL_NO_LOCALHASH))
  195. return &vfs_nil_ops;
  196. if (slash)
  197. *slash = PATH_SEP;
  198. if (!ret)
  199. ret = vfs_rosplit (path);
  200. *semi = '#';
  201. return ret;
  202. }
  203. vfs *
  204. vfs_type (char *path)
  205. {
  206. vfs *vfs;
  207. vfs = vfs_rosplit(path);
  208. if (!vfs)
  209. vfs = &vfs_local_ops;
  210. return vfs;
  211. }
  212. static struct vfs_stamping *stamps;
  213. /*
  214. * Returns the number of seconds remaining to the vfs timeout
  215. *
  216. * FIXME: currently this is set to 10 seconds. We should compute this.
  217. */
  218. int
  219. vfs_timeouts ()
  220. {
  221. return stamps ? 10 : 0;
  222. }
  223. static void
  224. vfs_addstamp (vfs *v, vfsid id, struct vfs_stamping *parent)
  225. {
  226. if (v != &vfs_local_ops && id != (vfsid)-1){
  227. struct vfs_stamping *stamp;
  228. struct vfs_stamping *last_stamp = NULL;
  229. for (stamp = stamps; stamp != NULL; stamp = stamp->next) {
  230. if (stamp->v == v && stamp->id == id){
  231. gettimeofday(&(stamp->time), NULL);
  232. return;
  233. }
  234. last_stamp = stamp;
  235. }
  236. stamp = g_new (struct vfs_stamping, 1);
  237. stamp->v = v;
  238. stamp->id = id;
  239. if (parent){
  240. struct vfs_stamping *st = stamp;
  241. while (parent){
  242. st->parent = g_new (struct vfs_stamping, 1);
  243. *st->parent = *parent;
  244. parent = parent->parent;
  245. st = st->parent;
  246. }
  247. st->parent = 0;
  248. }
  249. else
  250. stamp->parent = 0;
  251. gettimeofday (&(stamp->time), NULL);
  252. stamp->next = 0;
  253. if (stamps) {
  254. /* Add to the end */
  255. last_stamp->next = stamp;
  256. } else {
  257. /* Add first element */
  258. stamps = stamp;
  259. }
  260. }
  261. }
  262. void
  263. vfs_stamp (vfs *v, vfsid id)
  264. {
  265. struct vfs_stamping *stamp;
  266. for (stamp = stamps; stamp != NULL; stamp = stamp->next)
  267. if (stamp->v == v && stamp->id == id){
  268. gettimeofday (&(stamp->time), NULL);
  269. if (stamp->parent != NULL)
  270. vfs_stamp (stamp->parent->v, stamp->parent->id);
  271. return;
  272. }
  273. }
  274. void
  275. vfs_rm_parents (struct vfs_stamping *stamp)
  276. {
  277. struct vfs_stamping *parent;
  278. while (stamp) {
  279. parent = stamp->parent;
  280. g_free (stamp);
  281. stamp = parent;
  282. }
  283. }
  284. void
  285. vfs_rmstamp (vfs *v, vfsid id, int removeparents)
  286. {
  287. struct vfs_stamping *stamp, *st1;
  288. for (stamp = stamps, st1 = NULL; stamp != NULL; st1 = stamp, stamp = stamp->next)
  289. if (stamp->v == v && stamp->id == id){
  290. if (stamp->parent != NULL){
  291. if (removeparents)
  292. vfs_rmstamp (stamp->parent->v, stamp->parent->id, 1);
  293. vfs_rm_parents (stamp->parent);
  294. }
  295. if (st1 == NULL){
  296. stamps = stamp->next;
  297. } else {
  298. st1->next = stamp->next;
  299. }
  300. g_free (stamp);
  301. return;
  302. }
  303. }
  304. static int
  305. ferrno (vfs *vfs)
  306. {
  307. return vfs->ferrno ? (*vfs->ferrno)(vfs) : E_UNKNOWN;
  308. /* Hope that error message is obscure enough ;-) */
  309. }
  310. int
  311. mc_open (const char *filename, int flags, ...)
  312. {
  313. int handle;
  314. int mode;
  315. void *info;
  316. va_list ap;
  317. char *file = vfs_canon (filename);
  318. vfs *vfs = vfs_type (file);
  319. /* Get the mode flag */ /* FIXME: should look if O_CREAT is present */
  320. va_start (ap, flags);
  321. mode = va_arg (ap, int);
  322. va_end (ap);
  323. if (!vfs->open) {
  324. errno = -EOPNOTSUPP;
  325. return -1;
  326. }
  327. info = (*vfs->open) (vfs, file, flags, mode); /* open must be supported */
  328. g_free (file);
  329. if (!info){
  330. errno = ferrno (vfs);
  331. return -1;
  332. }
  333. handle = get_bucket ();
  334. vfs_file_table [handle].fs_info = info;
  335. vfs_file_table [handle].operations = vfs;
  336. return handle;
  337. }
  338. #define vfs_op(handle) vfs_file_table [handle].operations
  339. #define vfs_info(handle) vfs_file_table [handle].fs_info
  340. #define vfs_free_bucket(handle) vfs_info(handle) = 0;
  341. #define MC_OP(name, inarg, callarg, pre, post) \
  342. int mc_##name inarg \
  343. { \
  344. vfs *vfs; \
  345. int result; \
  346. \
  347. pre \
  348. result = vfs->name ? (*vfs->name)callarg : -1; \
  349. post \
  350. if (result == -1) \
  351. errno = vfs->name ? ferrno (vfs) : E_NOTSUPP; \
  352. return result; \
  353. }
  354. #define MC_NAMEOP(name, inarg, callarg) \
  355. MC_OP (name, inarg, callarg, path = vfs_canon (path); vfs = vfs_type (path);, g_free (path); )
  356. #define MC_HANDLEOP(name, inarg, callarg) \
  357. MC_OP (name, inarg, callarg, if (handle == -1) return -1; vfs = vfs_op (handle);, ;)
  358. MC_HANDLEOP(read, (int handle, char *buffer, int count), (vfs_info (handle), buffer, count) )
  359. int
  360. mc_ctl (int handle, int ctlop, int arg)
  361. {
  362. vfs *vfs = vfs_op (handle);
  363. return vfs->ctl ? (*vfs->ctl)(vfs_info (handle), ctlop, arg) : 0;
  364. }
  365. int
  366. mc_setctl (char *path, int ctlop, char *arg)
  367. {
  368. vfs *vfs;
  369. int result;
  370. if (!path)
  371. vfs_die("You don't want to pass NULL to mc_setctl.");
  372. path = vfs_canon (path);
  373. vfs = vfs_type (path);
  374. result = vfs->setctl ? (*vfs->setctl)(vfs, path, ctlop, arg) : 0;
  375. g_free (path);
  376. return result;
  377. }
  378. int
  379. mc_close (int handle)
  380. {
  381. vfs *vfs;
  382. int result;
  383. if (handle == -1 || !vfs_info (handle))
  384. return -1;
  385. vfs = vfs_op (handle);
  386. if (handle < 3)
  387. return close (handle);
  388. if (!vfs->close)
  389. vfs_die ("VFS must support close.\n");
  390. result = (*vfs->close)(vfs_info (handle));
  391. vfs_free_bucket (handle);
  392. if (result == -1)
  393. errno = ferrno (vfs);
  394. return result;
  395. }
  396. DIR *
  397. mc_opendir (char *dirname)
  398. {
  399. int handle, *handlep;
  400. void *info;
  401. vfs *vfs;
  402. dirname = vfs_canon (dirname);
  403. vfs = vfs_type (dirname);
  404. info = vfs->opendir ? (*vfs->opendir)(vfs, dirname) : NULL;
  405. g_free (dirname);
  406. if (!info){
  407. errno = vfs->opendir ? ferrno (vfs) : E_NOTSUPP;
  408. return NULL;
  409. }
  410. handle = get_bucket ();
  411. vfs_file_table [handle].fs_info = info;
  412. vfs_file_table [handle].operations = vfs;
  413. handlep = g_new (int, 1);
  414. *handlep = handle;
  415. return (DIR *) handlep;
  416. }
  417. /* This should strip the non needed part of a path name */
  418. #define vfs_name(x) x
  419. void
  420. mc_seekdir (DIR *dirp, int offset)
  421. {
  422. int handle;
  423. vfs *vfs;
  424. if (!dirp){
  425. errno = EFAULT;
  426. return;
  427. }
  428. handle = *(int *) dirp;
  429. vfs = vfs_op (handle);
  430. if (vfs->seekdir)
  431. (*vfs->seekdir) (vfs_info (handle), offset);
  432. else
  433. errno = E_NOTSUPP;
  434. }
  435. #define MC_DIROP(name, type, onerr ) \
  436. type mc_##name (DIR *dirp) \
  437. { \
  438. int handle; \
  439. vfs *vfs; \
  440. type result; \
  441. \
  442. if (!dirp){ \
  443. errno = EFAULT; \
  444. return onerr; \
  445. } \
  446. handle = *(int *) dirp; \
  447. vfs = vfs_op (handle); \
  448. result = vfs->name ? (*vfs->name) (vfs_info (handle)) : onerr; \
  449. if (result == onerr) \
  450. errno = vfs->name ? ferrno(vfs) : E_NOTSUPP; \
  451. return result; \
  452. }
  453. MC_DIROP (readdir, struct dirent *, NULL)
  454. MC_DIROP (telldir, int, -1)
  455. int
  456. mc_closedir (DIR *dirp)
  457. {
  458. int handle = *(int *) dirp;
  459. vfs *vfs = vfs_op (handle);
  460. int result;
  461. result = vfs->closedir ? (*vfs->closedir)(vfs_info (handle)) : -1;
  462. vfs_free_bucket (handle);
  463. g_free (dirp);
  464. return result;
  465. }
  466. int mc_stat (const char *filename, struct stat *buf) {
  467. vfs *vfs;
  468. int result;
  469. char *path;
  470. path = vfs_canon (filename); vfs = vfs_type (path);
  471. result = vfs->stat ? (*vfs->stat) (vfs, vfs_name (path), buf) : -1;
  472. g_free (path);
  473. if (result == -1)
  474. errno = vfs->name ? ferrno (vfs) : E_NOTSUPP;
  475. return result;
  476. }
  477. int mc_lstat (const char *filename, struct stat *buf) {
  478. vfs *vfs;
  479. int result;
  480. char *path;
  481. path = vfs_canon (filename); vfs = vfs_type (path);
  482. result = vfs->lstat ? (*vfs->lstat) (vfs, vfs_name (path), buf) : -1;
  483. g_free (path);
  484. if (result == -1)
  485. errno = vfs->name ? ferrno (vfs) : E_NOTSUPP;
  486. return result;
  487. }
  488. int mc_fstat (int handle, struct stat *buf) {
  489. vfs *vfs;
  490. int result;
  491. if (handle == -1)
  492. return -1;
  493. vfs = vfs_op (handle);
  494. result = vfs->fstat ? (*vfs->fstat) (vfs_info (handle), buf) : -1;
  495. if (result == -1)
  496. errno = vfs->name ? ferrno (vfs) : E_NOTSUPP;
  497. return result;
  498. }
  499. /*
  500. * You must g_strdup whatever this function returns.
  501. */
  502. static const char *
  503. mc_return_cwd (void)
  504. {
  505. char *p;
  506. struct stat my_stat, my_stat2;
  507. if (!vfs_rosplit (current_dir)){
  508. p = g_get_current_dir ();
  509. if (!p) /* One of the directories in the path is not readable */
  510. return current_dir;
  511. /* Otherwise check if it is O.K. to use the current_dir */
  512. if (!cd_symlinks ||
  513. mc_stat (p, &my_stat) ||
  514. mc_stat (current_dir, &my_stat2) ||
  515. my_stat.st_ino != my_stat2.st_ino ||
  516. my_stat.st_dev != my_stat2.st_dev){
  517. g_free (current_dir);
  518. current_dir = p;
  519. return p;
  520. } /* Otherwise we return current_dir below */
  521. g_free (p);
  522. }
  523. return current_dir;
  524. }
  525. char *
  526. mc_get_current_wd (char *buffer, int size)
  527. {
  528. const char *cwd = mc_return_cwd();
  529. strncpy (buffer, cwd, size - 1);
  530. buffer [size - 1] = 0;
  531. return buffer;
  532. }
  533. MC_NAMEOP (chmod, (char *path, int mode), (vfs, vfs_name (path), mode))
  534. MC_NAMEOP (chown, (char *path, int owner, int group), (vfs, vfs_name (path), owner, group))
  535. MC_NAMEOP (utime, (char *path, struct utimbuf *times), (vfs, vfs_name (path), times))
  536. MC_NAMEOP (readlink, (char *path, char *buf, int bufsiz), (vfs, vfs_name (path), buf, bufsiz))
  537. MC_NAMEOP (unlink, (char *path), (vfs, vfs_name (path)))
  538. MC_NAMEOP (symlink, (char *name1, char *path), (vfs, vfs_name (name1), vfs_name (path)))
  539. #define MC_RENAMEOP(name) \
  540. int mc_##name (const char *fname1, const char *fname2) \
  541. { \
  542. vfs *vfs; \
  543. int result; \
  544. \
  545. char *name2, *name1 = vfs_canon (fname1); \
  546. vfs = vfs_type (name1); \
  547. name2 = vfs_canon (fname2); \
  548. if (vfs != vfs_type (name2)){ \
  549. errno = EXDEV; \
  550. g_free (name1); \
  551. g_free (name2); \
  552. return -1; \
  553. } \
  554. \
  555. result = vfs->name ? (*vfs->name)(vfs, vfs_name (name1), vfs_name (name2)) : -1; \
  556. g_free (name1); \
  557. g_free (name2); \
  558. if (result == -1) \
  559. errno = vfs->name ? ferrno (vfs) : E_NOTSUPP; \
  560. return result; \
  561. }
  562. MC_RENAMEOP (link)
  563. MC_RENAMEOP (rename)
  564. MC_HANDLEOP (write, (int handle, char *buf, int nbyte), (vfs_info (handle), buf, nbyte))
  565. off_t mc_lseek (int fd, off_t offset, int whence)
  566. {
  567. vfs *vfs;
  568. int result;
  569. if (fd == -1)
  570. return -1;
  571. vfs = vfs_op (fd);
  572. result = vfs->lseek ? (*vfs->lseek)(vfs_info (fd), offset, whence) : -1;
  573. if (result == -1)
  574. errno = vfs->lseek ? ferrno (vfs) : E_NOTSUPP;
  575. return result;
  576. }
  577. /*
  578. * remove //, /./ and /../, local should point to big enough buffer
  579. */
  580. #define ISSLASH(a) (!a || (a == '/'))
  581. char *
  582. vfs_canon (const char *path)
  583. {
  584. if (!path)
  585. vfs_die("Cannot canonicalize NULL");
  586. /* Tilde expansion */
  587. if (*path == '~'){
  588. char *local, *result;
  589. local = tilde_expand (path);
  590. if (local){
  591. result = vfs_canon (local);
  592. g_free (local);
  593. return result;
  594. }
  595. }
  596. /* Relative to current directory */
  597. if (*path != PATH_SEP){
  598. char *local, *result;
  599. local = concat_dir_and_file (current_dir, path);
  600. result = vfs_canon (local);
  601. g_free (local);
  602. return result;
  603. }
  604. /*
  605. * So we have path of following form:
  606. * /p1/p2#op/.././././p3#op/p4. Good luck.
  607. */
  608. {
  609. char *result = g_strdup (path);
  610. canonicalize_pathname (result);
  611. return result;
  612. }
  613. }
  614. vfsid
  615. vfs_ncs_getid (vfs *nvfs, char *dir, struct vfs_stamping **par)
  616. {
  617. vfsid nvfsid;
  618. dir = concat_dir_and_file (dir, "");
  619. nvfsid = (*nvfs->getid)(nvfs, dir, par);
  620. g_free (dir);
  621. return nvfsid;
  622. }
  623. static int
  624. is_parent (vfs * nvfs, vfsid nvfsid, struct vfs_stamping *parent)
  625. {
  626. struct vfs_stamping *stamp;
  627. for (stamp = parent; stamp; stamp = stamp->parent)
  628. if (stamp->v == nvfs && stamp->id == nvfsid)
  629. break;
  630. return (stamp ? 1 : 0);
  631. }
  632. void
  633. vfs_add_noncurrent_stamps (vfs * oldvfs, vfsid oldvfsid, struct vfs_stamping *parent)
  634. {
  635. vfs *nvfs, *n2vfs, *n3vfs;
  636. vfsid nvfsid, n2vfsid, n3vfsid;
  637. struct vfs_stamping *par, *stamp;
  638. int f;
  639. /* FIXME: As soon as we convert to multiple panels, this stuff
  640. has to change. It works like this: We do not time out the
  641. vfs's which are current in any panel and on the other
  642. side we add the old directory with all its parents which
  643. are not in any panel (if we find such one, we stop adding
  644. parents to the time-outing structure. */
  645. /* There are three directories we have to take care of: current_dir,
  646. cpanel->cwd and opanel->cwd. Athough most of the time either
  647. current_dir and cpanel->cwd or current_dir and opanel->cwd are the
  648. same, it's possible that all three are different -- Norbert */
  649. if (!cpanel)
  650. return;
  651. nvfs = vfs_type (current_dir);
  652. nvfsid = vfs_ncs_getid (nvfs, current_dir, &par);
  653. vfs_rmstamp (nvfs, nvfsid, 1);
  654. f = is_parent (oldvfs, oldvfsid, par);
  655. vfs_rm_parents (par);
  656. if ((nvfs == oldvfs && nvfsid == oldvfsid) || oldvfsid == (vfsid *)-1 || f){
  657. return;
  658. }
  659. if (get_current_type () == view_listing){
  660. n2vfs = vfs_type (cpanel->cwd);
  661. n2vfsid = vfs_ncs_getid (n2vfs, cpanel->cwd, &par);
  662. f = is_parent (oldvfs, oldvfsid, par);
  663. vfs_rm_parents (par);
  664. if ((n2vfs == oldvfs && n2vfsid == oldvfsid) || f)
  665. return;
  666. } else {
  667. n2vfs = (vfs *) -1;
  668. n2vfsid = (vfs *) -1;
  669. }
  670. if (get_other_type () == view_listing){
  671. n3vfs = vfs_type (opanel->cwd);
  672. n3vfsid = vfs_ncs_getid (n3vfs, opanel->cwd, &par);
  673. f = is_parent (oldvfs, oldvfsid, par);
  674. vfs_rm_parents (par);
  675. if ((n3vfs == oldvfs && n3vfsid == oldvfsid) || f)
  676. return;
  677. } else {
  678. n3vfs = (vfs *)-1;
  679. n3vfsid = (vfs *)-1;
  680. }
  681. if ((*oldvfs->nothingisopen) (oldvfsid)){
  682. if (oldvfs == &vfs_extfs_ops && ((extfs_archive *) oldvfsid)->name == 0){
  683. /* Free the resources immediatly when we leave a mtools fs
  684. ('cd a:') instead of waiting for the vfs-timeout */
  685. (oldvfs->free) (oldvfsid);
  686. } else
  687. vfs_addstamp (oldvfs, oldvfsid, parent);
  688. for (stamp = parent; stamp != NULL; stamp = stamp->parent){
  689. if ((stamp->v == nvfs && stamp->id == nvfsid) ||
  690. (stamp->v == n2vfs && stamp->id == n2vfsid) ||
  691. (stamp->v == n3vfs && stamp->id == n3vfsid) ||
  692. stamp->id == (vfsid) - 1 ||
  693. !(*stamp->v->nothingisopen) (stamp->id))
  694. break;
  695. if (stamp->v == &vfs_extfs_ops && ((extfs_archive *) stamp->id)->name == 0){
  696. (stamp->v->free) (stamp->id);
  697. vfs_rmstamp (stamp->v, stamp->id, 0);
  698. } else
  699. vfs_addstamp (stamp->v, stamp->id, stamp->parent);
  700. }
  701. }
  702. }
  703. static void
  704. vfs_stamp_path (char *path)
  705. {
  706. vfs *vfs;
  707. vfsid id;
  708. struct vfs_stamping *par, *stamp;
  709. vfs = vfs_type (path);
  710. id = vfs_ncs_getid (vfs, path, &par);
  711. vfs_addstamp (vfs, id, par);
  712. for (stamp = par; stamp != NULL; stamp = stamp->parent)
  713. vfs_addstamp (stamp->v, stamp->id, stamp->parent);
  714. vfs_rm_parents (par);
  715. }
  716. void
  717. vfs_add_current_stamps (void)
  718. {
  719. vfs_stamp_path (current_dir);
  720. if (cpanel) {
  721. if (get_current_type () == view_listing)
  722. vfs_stamp_path (cpanel->cwd);
  723. }
  724. if (opanel) {
  725. if (get_other_type () == view_listing)
  726. vfs_stamp_path (opanel->cwd);
  727. }
  728. }
  729. /* This function is really broken */
  730. int
  731. mc_chdir (char *path)
  732. {
  733. char *a, *b;
  734. int result;
  735. char *p = NULL;
  736. vfs *oldvfs;
  737. vfsid oldvfsid;
  738. struct vfs_stamping *parent;
  739. a = current_dir; /* Save a copy for case of failure */
  740. current_dir = vfs_canon (path);
  741. current_vfs = vfs_type (current_dir);
  742. b = g_strdup (current_dir);
  743. result = (*current_vfs->chdir) ? (*current_vfs->chdir)(current_vfs, vfs_name (b)) : -1;
  744. g_free (b);
  745. if (result == -1){
  746. errno = ferrno (current_vfs);
  747. g_free (current_dir);
  748. current_vfs = vfs_type (a);
  749. current_dir = a;
  750. } else {
  751. oldvfs = vfs_type (a);
  752. oldvfsid = vfs_ncs_getid (oldvfs, a, &parent);
  753. g_free (a);
  754. vfs_add_noncurrent_stamps (oldvfs, oldvfsid, parent);
  755. vfs_rm_parents (parent);
  756. }
  757. if (*current_dir){
  758. p = strchr (current_dir, 0) - 1;
  759. if (*p == PATH_SEP && p > current_dir)
  760. *p = 0; /* Sometimes we assume no trailing slash on cwd */
  761. }
  762. return result;
  763. }
  764. int
  765. vfs_current_is_local (void)
  766. {
  767. return current_vfs == &vfs_local_ops;
  768. }
  769. int
  770. vfs_file_is_local (const char *file)
  771. {
  772. char *filename = vfs_canon (file);
  773. vfs *vfs = vfs_type (filename);
  774. g_free (filename);
  775. return vfs == &vfs_local_ops;
  776. }
  777. int
  778. vfs_file_is_ftp (char *filename)
  779. {
  780. #ifdef USE_NETCODE
  781. vfs *vfs;
  782. filename = vfs_canon (filename);
  783. vfs = vfs_type (filename);
  784. g_free (filename);
  785. return vfs == &vfs_ftpfs_ops;
  786. #else
  787. return 0;
  788. #endif
  789. }
  790. int
  791. vfs_file_is_smb (char *filename)
  792. {
  793. #ifdef WITH_SMBFS
  794. #ifdef USE_NETCODE
  795. vfs *vfs;
  796. filename = vfs_canon (filename);
  797. vfs = vfs_type (filename);
  798. g_free (filename);
  799. return vfs == &vfs_smbfs_ops;
  800. #endif /* USE_NETCODE */
  801. #endif /* WITH_SMBFS */
  802. return 0;
  803. }
  804. char *vfs_get_current_dir (void)
  805. {
  806. return current_dir;
  807. }
  808. static void vfs_setup_wd (void)
  809. {
  810. current_dir = g_strdup (PATH_SEP_STR);
  811. if (!(vfs_flags & FL_NO_CWDSETUP))
  812. mc_return_cwd();
  813. if (strlen(current_dir)>MC_MAXPATHLEN-2)
  814. vfs_die ("Current dir too long.\n");
  815. }
  816. MC_NAMEOP (mkdir, (char *path, mode_t mode), (vfs, vfs_name (path), mode))
  817. MC_NAMEOP (rmdir, (char *path), (vfs, vfs_name (path)))
  818. MC_NAMEOP (mknod, (char *path, int mode, int dev), (vfs, vfs_name (path), mode, dev))
  819. #ifdef HAVE_MMAP
  820. static struct mc_mmapping {
  821. caddr_t addr;
  822. void *vfs_info;
  823. vfs *vfs;
  824. struct mc_mmapping *next;
  825. } *mc_mmaparray = NULL;
  826. caddr_t
  827. mc_mmap (caddr_t addr, size_t len, int prot, int flags, int fd, off_t offset)
  828. {
  829. vfs *vfs;
  830. caddr_t result;
  831. struct mc_mmapping *mcm;
  832. if (fd == -1)
  833. return (caddr_t) -1;
  834. vfs = vfs_op (fd);
  835. result = vfs->mmap ? (*vfs->mmap)(vfs, addr, len, prot, flags, vfs_info (fd), offset) : (caddr_t)-1;
  836. if (result == (caddr_t)-1){
  837. errno = ferrno (vfs);
  838. return (caddr_t)-1;
  839. }
  840. mcm =g_new (struct mc_mmapping, 1);
  841. mcm->addr = result;
  842. mcm->vfs_info = vfs_info (fd);
  843. mcm->vfs = vfs;
  844. mcm->next = mc_mmaparray;
  845. mc_mmaparray = mcm;
  846. return result;
  847. }
  848. int
  849. mc_munmap (caddr_t addr, size_t len)
  850. {
  851. struct mc_mmapping *mcm, *mcm2 = NULL;
  852. for (mcm = mc_mmaparray; mcm != NULL; mcm2 = mcm, mcm = mcm->next){
  853. if (mcm->addr == addr){
  854. if (mcm2 == NULL)
  855. mc_mmaparray = mcm->next;
  856. else
  857. mcm2->next = mcm->next;
  858. if (mcm->vfs->munmap)
  859. (*mcm->vfs->munmap)(mcm->vfs, addr, len, mcm->vfs_info);
  860. g_free (mcm);
  861. return 0;
  862. }
  863. }
  864. return -1;
  865. }
  866. #endif
  867. char *
  868. mc_def_getlocalcopy (vfs *vfs, char *filename)
  869. {
  870. char *tmp;
  871. int fdin, fdout, i;
  872. char buffer[8192];
  873. struct stat mystat;
  874. char *ext = NULL;
  875. char *ptr;
  876. fdin = mc_open (filename, O_RDONLY);
  877. if (fdin == -1)
  878. return NULL;
  879. /* Try to preserve existing extension */
  880. for (ptr = filename + strlen(filename) - 1; ptr >= filename; ptr--) {
  881. if (*ptr == '.') {
  882. ext = ptr;
  883. break;
  884. }
  885. if (!isalnum((unsigned char) *ptr))
  886. break;
  887. }
  888. fdout = mc_mkstemps (&tmp, "mclocalcopy", ext);
  889. if (fdout == -1)
  890. goto fail;
  891. while ((i = mc_read (fdin, buffer, sizeof (buffer))) > 0){
  892. if (write (fdout, buffer, i) != i)
  893. goto fail;
  894. }
  895. if (i == -1)
  896. goto fail;
  897. i = mc_close (fdin);
  898. fdin = -1;
  899. if (i==-1)
  900. goto fail;
  901. if (close (fdout)==-1)
  902. goto fail;
  903. if (mc_stat (filename, &mystat) != -1){
  904. chmod (tmp, mystat.st_mode);
  905. }
  906. return tmp;
  907. fail:
  908. if (fdout) close(fdout);
  909. if (fdin) mc_close (fdin);
  910. g_free (tmp);
  911. return NULL;
  912. }
  913. char *
  914. mc_getlocalcopy (const char *pathname)
  915. {
  916. char *result;
  917. char *path = vfs_canon (pathname);
  918. vfs *vfs = vfs_type (path);
  919. result = vfs->getlocalcopy ? (*vfs->getlocalcopy)(vfs, vfs_name (path)) :
  920. mc_def_getlocalcopy (vfs, vfs_name (path));
  921. g_free (path);
  922. if (!result)
  923. errno = ferrno (vfs);
  924. return result;
  925. }
  926. int
  927. mc_def_ungetlocalcopy (vfs *vfs, char *filename, char *local, int has_changed)
  928. { /* Dijkstra probably hates me... But he should teach me how to do this nicely. */
  929. int fdin = -1, fdout = -1, i;
  930. if (has_changed){
  931. char buffer [8192];
  932. fdin = open (local, O_RDONLY);
  933. if (fdin == -1)
  934. goto failed;
  935. fdout = mc_open (filename, O_WRONLY | O_TRUNC);
  936. if (fdout == -1)
  937. goto failed;
  938. while ((i = read (fdin, buffer, sizeof (buffer))) > 0){
  939. if (mc_write (fdout, buffer, i) != i)
  940. goto failed;
  941. }
  942. if (i == -1)
  943. goto failed;
  944. if (close (fdin)==-1) {
  945. fdin = -1;
  946. goto failed;
  947. }
  948. fdin = -1;
  949. if (mc_close (fdout)==-1) {
  950. fdout = -1;
  951. goto failed;
  952. }
  953. }
  954. unlink (local);
  955. g_free (local);
  956. return 0;
  957. failed:
  958. message_1s (1, _("Changes to file lost"), filename);
  959. if (fdout!=-1) mc_close(fdout);
  960. if (fdin!=-1) close(fdin);
  961. unlink (local);
  962. g_free (local);
  963. return -1;
  964. }
  965. int
  966. mc_ungetlocalcopy (const char *pathname, char *local, int has_changed)
  967. {
  968. int return_value = 0;
  969. char *path = vfs_canon (pathname);
  970. vfs *vfs = vfs_type (path);
  971. return_value = vfs->ungetlocalcopy ?
  972. (*vfs->ungetlocalcopy)(vfs, vfs_name (path), local, has_changed) :
  973. mc_def_ungetlocalcopy (vfs, vfs_name (path), local, has_changed);
  974. g_free (path);
  975. return return_value;
  976. }
  977. /*
  978. * Hmm, as timeout is minute or so, do we need to care about usecs?
  979. */
  980. static inline int
  981. timeoutcmp (struct timeval *t1, struct timeval *t2)
  982. {
  983. return ((t1->tv_sec < t2->tv_sec)
  984. || ((t1->tv_sec == t2->tv_sec) && (t1->tv_usec <= t2->tv_usec)));
  985. }
  986. /* This is called from timeout handler with now = 0, or can be called
  987. with now = 1 to force freeing all filesystems that are not in use */
  988. void
  989. vfs_expire (int now)
  990. {
  991. static int locked = 0;
  992. struct timeval time;
  993. struct vfs_stamping *stamp, *st;
  994. /* Avoid recursive invocation, e.g. when one of the free functions
  995. calls message_1s */
  996. if (locked)
  997. return;
  998. locked = 1;
  999. gettimeofday (&time, NULL);
  1000. time.tv_sec -= vfs_timeout;
  1001. for (stamp = stamps; stamp != NULL;){
  1002. if (now || (timeoutcmp (&stamp->time, &time))){
  1003. st = stamp->next;
  1004. (*stamp->v->free) (stamp->id);
  1005. vfs_rmstamp (stamp->v, stamp->id, 0);
  1006. stamp = st;
  1007. } else
  1008. stamp = stamp->next;
  1009. }
  1010. locked = 0;
  1011. }
  1012. void
  1013. vfs_timeout_handler (void)
  1014. {
  1015. vfs_expire (0);
  1016. }
  1017. void
  1018. vfs_init (void)
  1019. {
  1020. time_t current_time;
  1021. struct tm *t;
  1022. memset (vfs_file_table, 0, sizeof (vfs_file_table));
  1023. current_time = time (NULL);
  1024. t = localtime (&current_time);
  1025. current_mday = t->tm_mday;
  1026. current_mon = t->tm_mon;
  1027. current_year = t->tm_year;
  1028. /* We do not want to register vfs_local_ops */
  1029. #ifdef USE_NETCODE
  1030. tcp_init();
  1031. vfs_register (&vfs_ftpfs_ops);
  1032. vfs_register (&vfs_fish_ops);
  1033. #ifdef WITH_SMBFS
  1034. vfs_register (&vfs_smbfs_ops);
  1035. #endif /* WITH_SMBFS */
  1036. #ifdef WITH_MCFS
  1037. vfs_register (&vfs_mcfs_ops);
  1038. #endif /* WITH_SMBFS */
  1039. #endif /* USE_NETCODE */
  1040. vfs_register (&vfs_extfs_ops);
  1041. vfs_register (&vfs_sfs_ops);
  1042. vfs_register (&vfs_tarfs_ops);
  1043. vfs_register (&vfs_cpiofs_ops);
  1044. #ifdef USE_EXT2FSLIB
  1045. vfs_register (&vfs_undelfs_ops);
  1046. #endif /* USE_EXT2FSLIB */
  1047. vfs_setup_wd ();
  1048. }
  1049. void
  1050. vfs_shut (void)
  1051. {
  1052. struct vfs_stamping *stamp, *st;
  1053. vfs *vfs;
  1054. for (stamp = stamps, stamps = 0; stamp != NULL;){
  1055. (*stamp->v->free)(stamp->id);
  1056. st = stamp->next;
  1057. g_free (stamp);
  1058. stamp = st;
  1059. }
  1060. if (stamps)
  1061. vfs_rmstamp (stamps->v, stamps->id, 1);
  1062. if (current_dir)
  1063. g_free (current_dir);
  1064. for (vfs=vfs_list; vfs; vfs=vfs->next)
  1065. if (vfs->done)
  1066. (*vfs->done) (vfs);
  1067. }
  1068. /*
  1069. * These ones grab information from the VFS
  1070. * and handles them to an upper layer
  1071. */
  1072. void
  1073. vfs_fill_names (void (*func)(char *))
  1074. {
  1075. vfs *vfs;
  1076. for (vfs=vfs_list; vfs; vfs=vfs->next)
  1077. if (vfs->fill_names)
  1078. (*vfs->fill_names) (vfs, func);
  1079. }
  1080. /* Following stuff (parse_ls_lga) is used by ftpfs and extfs */
  1081. #define MAXCOLS 30
  1082. static char *columns [MAXCOLS]; /* Points to the string in column n */
  1083. static int column_ptr [MAXCOLS]; /* Index from 0 to the starting positions of the columns */
  1084. int
  1085. vfs_split_text (char *p)
  1086. {
  1087. char *original = p;
  1088. int numcols;
  1089. memset (columns, 0, sizeof (columns));
  1090. for (numcols = 0; *p && numcols < MAXCOLS; numcols++){
  1091. while (*p == ' ' || *p == '\r' || *p == '\n'){
  1092. *p = 0;
  1093. p++;
  1094. }
  1095. columns [numcols] = p;
  1096. column_ptr [numcols] = p - original;
  1097. while (*p && *p != ' ' && *p != '\r' && *p != '\n')
  1098. p++;
  1099. }
  1100. return numcols;
  1101. }
  1102. static int
  1103. is_num (int idx)
  1104. {
  1105. char *column = columns[idx];
  1106. if (!column || column[0] < '0' || column[0] > '9')
  1107. return 0;
  1108. return 1;
  1109. }
  1110. /* Return 1 for MM-DD-YY and MM-DD-YYYY */
  1111. static int
  1112. is_dos_date (const char *str)
  1113. {
  1114. int len;
  1115. if (!str)
  1116. return 0;
  1117. len = strlen (str);
  1118. if (len != 8 && len != 10)
  1119. return 0;
  1120. if (str[2] != str[5])
  1121. return 0;
  1122. if (!strchr ("\\-/", (int) str[2]))
  1123. return 0;
  1124. return 1;
  1125. }
  1126. static int
  1127. is_week (char *str, struct tm *tim)
  1128. {
  1129. static const char *week = "SunMonTueWedThuFriSat";
  1130. char *pos;
  1131. if (!str)
  1132. return 0;
  1133. if ((pos = strstr (week, str)) != NULL) {
  1134. if (tim != NULL)
  1135. tim->tm_wday = (pos - week) / 3;
  1136. return 1;
  1137. }
  1138. return 0;
  1139. }
  1140. static int
  1141. is_month (char *str, struct tm *tim)
  1142. {
  1143. static const char *month = "JanFebMarAprMayJunJulAugSepOctNovDec";
  1144. char *pos;
  1145. if (!str)
  1146. return 0;
  1147. if ((pos = strstr (month, str)) != NULL) {
  1148. if (tim != NULL)
  1149. tim->tm_mon = (pos - month) / 3;
  1150. return 1;
  1151. }
  1152. return 0;
  1153. }
  1154. static int
  1155. is_time (char *str, struct tm *tim)
  1156. {
  1157. char *p, *p2;
  1158. if (!str)
  1159. return 0;
  1160. if ((p = strchr (str, ':')) && (p2 = strrchr (str, ':'))) {
  1161. if (p != p2) {
  1162. if (sscanf
  1163. (str, "%2d:%2d:%2d", &tim->tm_hour, &tim->tm_min,
  1164. &tim->tm_sec) != 3)
  1165. return 0;
  1166. } else {
  1167. if (sscanf (str, "%2d:%2d", &tim->tm_hour, &tim->tm_min) != 2)
  1168. return 0;
  1169. }
  1170. } else
  1171. return 0;
  1172. return 1;
  1173. }
  1174. static int is_year (char *str, struct tm *tim)
  1175. {
  1176. long year;
  1177. if (!str)
  1178. return 0;
  1179. if (strchr (str, ':'))
  1180. return 0;
  1181. if (strlen (str) != 4)
  1182. return 0;
  1183. if (sscanf (str, "%ld", &year) != 1)
  1184. return 0;
  1185. if (year < 1900 || year > 3000)
  1186. return 0;
  1187. tim->tm_year = (int) (year - 1900);
  1188. return 1;
  1189. }
  1190. /*
  1191. * FIXME: this is broken. Consider following entry:
  1192. * -rwx------ 1 root root 1 Aug 31 10:04 2904 1234
  1193. * where "2904 1234" is filename. Well, this code decodes it as year :-(.
  1194. */
  1195. int
  1196. vfs_parse_filetype (char c)
  1197. {
  1198. switch (c) {
  1199. case 'd': return S_IFDIR;
  1200. case 'b': return S_IFBLK;
  1201. case 'c': return S_IFCHR;
  1202. case 'l': return S_IFLNK;
  1203. case 's': /* Socket */
  1204. #ifdef S_IFSOCK
  1205. return S_IFSOCK;
  1206. #else
  1207. /* If not supported, we fall through to IFIFO */
  1208. return S_IFIFO;
  1209. #endif
  1210. case 'D': /* Solaris door */
  1211. #ifdef S_IFDOOR
  1212. return S_IFDOOR;
  1213. #else
  1214. return S_IFIFO;
  1215. #endif
  1216. case 'p': return S_IFIFO;
  1217. case 'm': case 'n': /* Don't know what these are :-) */
  1218. case '-': case '?': return S_IFREG;
  1219. default: return -1;
  1220. }
  1221. }
  1222. int vfs_parse_filemode (const char *p)
  1223. { /* converts rw-rw-rw- into 0666 */
  1224. int res = 0;
  1225. switch (*(p++)){
  1226. case 'r': res |= 0400; break;
  1227. case '-': break;
  1228. default: return -1;
  1229. }
  1230. switch (*(p++)){
  1231. case 'w': res |= 0200; break;
  1232. case '-': break;
  1233. default: return -1;
  1234. }
  1235. switch (*(p++)){
  1236. case 'x': res |= 0100; break;
  1237. case 's': res |= 0100 | S_ISUID; break;
  1238. case 'S': res |= S_ISUID; break;
  1239. case '-': break;
  1240. default: return -1;
  1241. }
  1242. switch (*(p++)){
  1243. case 'r': res |= 0040; break;
  1244. case '-': break;
  1245. default: return -1;
  1246. }
  1247. switch (*(p++)){
  1248. case 'w': res |= 0020; break;
  1249. case '-': break;
  1250. default: return -1;
  1251. }
  1252. switch (*(p++)){
  1253. case 'x': res |= 0010; break;
  1254. case 's': res |= 0010 | S_ISGID; break;
  1255. case 'l': /* Solaris produces these */
  1256. case 'S': res |= S_ISGID; break;
  1257. case '-': break;
  1258. default: return -1;
  1259. }
  1260. switch (*(p++)){
  1261. case 'r': res |= 0004; break;
  1262. case '-': break;
  1263. default: return -1;
  1264. }
  1265. switch (*(p++)){
  1266. case 'w': res |= 0002; break;
  1267. case '-': break;
  1268. default: return -1;
  1269. }
  1270. switch (*(p++)){
  1271. case 'x': res |= 0001; break;
  1272. case 't': res |= 0001 | S_ISVTX; break;
  1273. case 'T': res |= S_ISVTX; break;
  1274. case '-': break;
  1275. default: return -1;
  1276. }
  1277. return res;
  1278. }
  1279. /* This function parses from idx in the columns[] array */
  1280. int
  1281. vfs_parse_filedate (int idx, time_t *t)
  1282. {
  1283. char *p;
  1284. struct tm tim;
  1285. int d[3];
  1286. int got_year = 0;
  1287. /* Let's setup default time values */
  1288. tim.tm_year = current_year;
  1289. tim.tm_mon = current_mon;
  1290. tim.tm_mday = current_mday;
  1291. tim.tm_hour = 0;
  1292. tim.tm_min = 0;
  1293. tim.tm_sec = 0;
  1294. tim.tm_isdst = -1; /* Let mktime() try to guess correct dst offset */
  1295. p = columns[idx++];
  1296. /* We eat weekday name in case of extfs */
  1297. if (is_week (p, &tim))
  1298. p = columns[idx++];
  1299. /* Month name */
  1300. if (is_month (p, &tim)) {
  1301. /* And we expect, it followed by day number */
  1302. if (is_num (idx))
  1303. tim.tm_mday = (int) atol (columns[idx++]);
  1304. else
  1305. return 0; /* No day */
  1306. } else {
  1307. /* We usually expect:
  1308. Mon DD hh:mm
  1309. Mon DD YYYY
  1310. But in case of extfs we allow these date formats:
  1311. Mon DD YYYY hh:mm
  1312. Mon DD hh:mm YYYY
  1313. Wek Mon DD hh:mm:ss YYYY
  1314. MM-DD-YY hh:mm
  1315. where Mon is Jan-Dec, DD, MM, YY two digit day, month, year,
  1316. YYYY four digit year, hh, mm, ss two digit hour, minute or second. */
  1317. /* Special case with MM-DD-YY or MM-DD-YYYY */
  1318. if (is_dos_date (p)) {
  1319. p[2] = p[5] = '-';
  1320. if (sscanf (p, "%2d-%2d-%d", &d[0], &d[1], &d[2]) == 3) {
  1321. /* Months are zero based */
  1322. if (d[0] > 0)
  1323. d[0]--;
  1324. if (d[2] > 1900) {
  1325. d[2] -= 1900;
  1326. } else {
  1327. /* Y2K madness */
  1328. if (d[2] < 70)
  1329. d[2] += 100;
  1330. }
  1331. tim.tm_mon = d[0];
  1332. tim.tm_mday = d[1];
  1333. tim.tm_year = d[2];
  1334. got_year = 1;
  1335. } else
  1336. return 0; /* sscanf failed */
  1337. } else
  1338. return 0; /* unsupported format */
  1339. }
  1340. /* Here we expect to find time and/or year */
  1341. if (is_num (idx)) {
  1342. if (is_time (columns[idx], &tim)
  1343. || (got_year = is_year (columns[idx], &tim))) {
  1344. idx++;
  1345. /* This is a special case for ctime() or Mon DD YYYY hh:mm */
  1346. if (is_num (idx) && (columns[idx + 1][0])) {
  1347. if (got_year) {
  1348. if (is_time (columns[idx], &tim))
  1349. idx++; /* time also */
  1350. } else {
  1351. if ((got_year = is_year (columns[idx], &tim)))
  1352. idx++; /* year also */
  1353. }
  1354. }
  1355. } /* only time or date */
  1356. } else
  1357. return 0; /* Nor time or date */
  1358. /*
  1359. * If the date is less than 6 months in the past, it is shown without year
  1360. * other dates in the past or future are shown with year but without time
  1361. * This does not check for years before 1900 ... I don't know, how
  1362. * to represent them at all
  1363. */
  1364. if (!got_year && current_mon < 6 && current_mon < tim.tm_mon
  1365. && tim.tm_mon - current_mon >= 6)
  1366. tim.tm_year--;
  1367. if ((*t = mktime (&tim)) < 0)
  1368. *t = 0;
  1369. return idx;
  1370. }
  1371. int
  1372. vfs_parse_ls_lga (const char *p, struct stat *s, char **filename, char **linkname)
  1373. {
  1374. int idx, idx2, num_cols;
  1375. int i;
  1376. char *p_copy = NULL;
  1377. char *t = NULL;
  1378. const char *line = p;
  1379. if (strncmp (p, "total", 5) == 0)
  1380. return 0;
  1381. if ((i = vfs_parse_filetype(*(p++))) == -1)
  1382. goto error;
  1383. s->st_mode = i;
  1384. if (*p == ' ') /* Notwell 4 */
  1385. p++;
  1386. if (*p == '['){
  1387. if (strlen (p) <= 8 || p [8] != ']')
  1388. goto error;
  1389. /* Should parse here the Notwell permissions :) */
  1390. if (S_ISDIR (s->st_mode))
  1391. s->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IXUSR | S_IXGRP | S_IXOTH);
  1392. else
  1393. s->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR);
  1394. p += 9;
  1395. } else {
  1396. if ((i = vfs_parse_filemode(p)) == -1)
  1397. goto error;
  1398. s->st_mode |= i;
  1399. p += 9;
  1400. /* This is for an extra ACL attribute (HP-UX) */
  1401. if (*p == '+')
  1402. p++;
  1403. }
  1404. p_copy = g_strdup(p);
  1405. num_cols = vfs_split_text (p_copy);
  1406. s->st_nlink = atol (columns [0]);
  1407. if (s->st_nlink <= 0)
  1408. goto error;
  1409. if (!is_num (1))
  1410. s->st_uid = finduid (columns [1]);
  1411. else
  1412. s->st_uid = (uid_t) atol (columns [1]);
  1413. /* Mhm, the ls -lg did not produce a group field */
  1414. for (idx = 3; idx <= 5; idx++)
  1415. if (is_month(columns [idx], NULL) || is_week(columns [idx], NULL) || is_dos_date(columns[idx]))
  1416. break;
  1417. if (idx == 6 || (idx == 5 && !S_ISCHR (s->st_mode) && !S_ISBLK (s->st_mode)))
  1418. goto error;
  1419. /* We don't have gid */
  1420. if (idx == 3 || (idx == 4 && (S_ISCHR(s->st_mode) || S_ISBLK (s->st_mode))))
  1421. idx2 = 2;
  1422. else {
  1423. /* We have gid field */
  1424. if (is_num (2))
  1425. s->st_gid = (gid_t) atol (columns [2]);
  1426. else
  1427. s->st_gid = findgid (columns [2]);
  1428. idx2 = 3;
  1429. }
  1430. /* This is device */
  1431. if (S_ISCHR (s->st_mode) || S_ISBLK (s->st_mode)){
  1432. int maj, min;
  1433. if (!is_num (idx2) || sscanf(columns [idx2], " %d,", &maj) != 1)
  1434. goto error;
  1435. if (!is_num (++idx2) || sscanf(columns [idx2], " %d", &min) != 1)
  1436. goto error;
  1437. #ifdef HAVE_ST_RDEV
  1438. s->st_rdev = ((maj & 0xff) << 8) | (min & 0xffff00ff);
  1439. #endif
  1440. s->st_size = 0;
  1441. } else {
  1442. /* Common file size */
  1443. if (!is_num (idx2))
  1444. goto error;
  1445. s->st_size = (size_t) atol (columns [idx2]);
  1446. #ifdef HAVE_ST_RDEV
  1447. s->st_rdev = 0;
  1448. #endif
  1449. }
  1450. idx = vfs_parse_filedate(idx, &s->st_mtime);
  1451. if (!idx)
  1452. goto error;
  1453. /* Use resulting time value */
  1454. s->st_atime = s->st_ctime = s->st_mtime;
  1455. /* s->st_dev and s->st_ino must be initialized by vfs_s_new_inode () */
  1456. #ifdef HAVE_ST_BLKSIZE
  1457. s->st_blksize = 512;
  1458. #endif
  1459. #ifdef HAVE_ST_BLOCKS
  1460. s->st_blocks = (s->st_size + 511) / 512;
  1461. #endif
  1462. for (i = idx + 1, idx2 = 0; i < num_cols; i++ )
  1463. if (strcmp (columns [i], "->") == 0){
  1464. idx2 = i;
  1465. break;
  1466. }
  1467. if (((S_ISLNK (s->st_mode) ||
  1468. (num_cols == idx + 3 && s->st_nlink > 1))) /* Maybe a hardlink? (in extfs) */
  1469. && idx2){
  1470. if (filename){
  1471. *filename = g_strndup (p + column_ptr [idx], column_ptr [idx2] - column_ptr [idx] - 1);
  1472. }
  1473. if (linkname){
  1474. t = g_strdup (p + column_ptr [idx2+1]);
  1475. *linkname = t;
  1476. }
  1477. } else {
  1478. /* Extract the filename from the string copy, not from the columns
  1479. * this way we have a chance of entering hidden directories like ". ."
  1480. */
  1481. if (filename){
  1482. /*
  1483. *filename = g_strdup (columns [idx++]);
  1484. */
  1485. t = g_strdup (p + column_ptr [idx]);
  1486. *filename = t;
  1487. }
  1488. if (linkname)
  1489. *linkname = NULL;
  1490. }
  1491. if (t) {
  1492. int p = strlen (t);
  1493. if ((--p > 0) && (t [p] == '\r' || t [p] == '\n'))
  1494. t [p] = 0;
  1495. if ((--p > 0) && (t [p] == '\r' || t [p] == '\n'))
  1496. t [p] = 0;
  1497. }
  1498. g_free (p_copy);
  1499. return 1;
  1500. error:
  1501. {
  1502. static int errorcount = 0;
  1503. if (++errorcount < 5) {
  1504. message_1s (1, _("Cannot parse:"), (p_copy && *p_copy) ? p_copy : line);
  1505. } else if (errorcount == 5)
  1506. message_1s (1, _("Error"), _("More parsing errors will be ignored."));
  1507. }
  1508. g_free (p_copy);
  1509. return 0;
  1510. }
  1511. void
  1512. vfs_die (const char *m)
  1513. {
  1514. message_1s (1, _("Internal error:"), m);
  1515. exit (1);
  1516. }
  1517. void
  1518. vfs_print_stats (const char *fs_name, const char *action, const char *file_name, off_t have, off_t need)
  1519. {
  1520. static char *i18n_percent_transf_format = NULL, *i18n_transf_format = NULL;
  1521. if (i18n_percent_transf_format == NULL) {
  1522. i18n_percent_transf_format = _("%s: %s: %s %3d%% (%lu bytes transferred)");
  1523. i18n_transf_format = _("%s: %s: %s %lu bytes transferred");
  1524. }
  1525. if (need)
  1526. print_vfs_message (i18n_percent_transf_format, fs_name, action,
  1527. file_name, (int)((double)have*100/need), (unsigned long) have);
  1528. else
  1529. print_vfs_message (i18n_transf_format,
  1530. fs_name, action, file_name, (unsigned long) have);
  1531. }
  1532. char *
  1533. vfs_get_password (char *msg)
  1534. {
  1535. return (char *) input_dialog (msg, _("Password:"), INPUT_PASSWORD);
  1536. }
  1537. /*
  1538. * Returns vfs path corresponding to given url. If passed string is
  1539. * not recognized as url, g_strdup(url) is returned.
  1540. */
  1541. char *
  1542. vfs_translate_url (char *url)
  1543. {
  1544. if (strncmp (url, "ftp://", 6) == 0)
  1545. return g_strconcat ("/#ftp:", url + 6, NULL);
  1546. else if (strncmp (url, "a:", 2) == 0)
  1547. return g_strdup ("/#a");
  1548. else
  1549. return g_strdup (url);
  1550. }