PageRenderTime 64ms CodeModel.GetById 4ms RepoModel.GetById 0ms app.codeStats 1ms

/gio/glocalfile.c

https://gitlab.com/ImageMagick/glib
C | 2936 lines | 2317 code | 457 blank | 162 comment | 373 complexity | a1f66693e278f88a20545def2cc45e07 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  1. /* GIO - GLib Input, Output and Streaming Library
  2. *
  3. * Copyright (C) 2006-2007 Red Hat, Inc.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library 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 GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General
  16. * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * Author: Alexander Larsson <alexl@redhat.com>
  19. */
  20. #include "config.h"
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <fcntl.h>
  26. #if G_OS_UNIX
  27. #include <dirent.h>
  28. #include <unistd.h>
  29. #endif
  30. #if HAVE_SYS_STATFS_H
  31. #include <sys/statfs.h>
  32. #endif
  33. #if HAVE_SYS_STATVFS_H
  34. #include <sys/statvfs.h>
  35. #endif
  36. #if HAVE_SYS_VFS_H
  37. #include <sys/vfs.h>
  38. #elif HAVE_SYS_MOUNT_H
  39. #if HAVE_SYS_PARAM_H
  40. #include <sys/param.h>
  41. #endif
  42. #include <sys/mount.h>
  43. #endif
  44. #ifndef O_BINARY
  45. #define O_BINARY 0
  46. #endif
  47. #include "gfileattribute.h"
  48. #include "glocalfile.h"
  49. #include "glocalfileprivate.h"
  50. #include "glocalfileinfo.h"
  51. #include "glocalfileenumerator.h"
  52. #include "glocalfileinputstream.h"
  53. #include "glocalfileoutputstream.h"
  54. #include "glocalfileiostream.h"
  55. #include "glocalfilemonitor.h"
  56. #include "gmountprivate.h"
  57. #include "gunixmounts.h"
  58. #include "gioerror.h"
  59. #include <glib/gstdio.h>
  60. #include "glibintl.h"
  61. #ifdef G_OS_UNIX
  62. #include "glib-unix.h"
  63. #endif
  64. #include "glib-private.h"
  65. #include "glib-private.h"
  66. #ifdef G_OS_WIN32
  67. #include <windows.h>
  68. #include <io.h>
  69. #include <direct.h>
  70. #ifndef FILE_READ_ONLY_VOLUME
  71. #define FILE_READ_ONLY_VOLUME 0x00080000
  72. #endif
  73. #ifndef S_ISDIR
  74. #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
  75. #endif
  76. #ifndef S_ISLNK
  77. #define S_ISLNK(m) (0)
  78. #endif
  79. #ifndef ECANCELED
  80. #define ECANCELED 105
  81. #endif
  82. #endif
  83. static void g_local_file_file_iface_init (GFileIface *iface);
  84. static GFileAttributeInfoList *local_writable_attributes = NULL;
  85. static /* GFileAttributeInfoList * */ gsize local_writable_namespaces = 0;
  86. struct _GLocalFile
  87. {
  88. GObject parent_instance;
  89. char *filename;
  90. };
  91. #define g_local_file_get_type _g_local_file_get_type
  92. G_DEFINE_TYPE_WITH_CODE (GLocalFile, g_local_file, G_TYPE_OBJECT,
  93. G_IMPLEMENT_INTERFACE (G_TYPE_FILE,
  94. g_local_file_file_iface_init))
  95. static char *find_mountpoint_for (const char *file, dev_t dev);
  96. static void
  97. g_local_file_finalize (GObject *object)
  98. {
  99. GLocalFile *local;
  100. local = G_LOCAL_FILE (object);
  101. g_free (local->filename);
  102. G_OBJECT_CLASS (g_local_file_parent_class)->finalize (object);
  103. }
  104. static void
  105. g_local_file_class_init (GLocalFileClass *klass)
  106. {
  107. GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  108. GFileAttributeInfoList *list;
  109. gobject_class->finalize = g_local_file_finalize;
  110. /* Set up attribute lists */
  111. /* Writable attributes: */
  112. list = g_file_attribute_info_list_new ();
  113. g_file_attribute_info_list_add (list,
  114. G_FILE_ATTRIBUTE_UNIX_MODE,
  115. G_FILE_ATTRIBUTE_TYPE_UINT32,
  116. G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
  117. G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
  118. #ifdef G_OS_UNIX
  119. g_file_attribute_info_list_add (list,
  120. G_FILE_ATTRIBUTE_UNIX_UID,
  121. G_FILE_ATTRIBUTE_TYPE_UINT32,
  122. G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
  123. g_file_attribute_info_list_add (list,
  124. G_FILE_ATTRIBUTE_UNIX_GID,
  125. G_FILE_ATTRIBUTE_TYPE_UINT32,
  126. G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
  127. #endif
  128. #ifdef HAVE_SYMLINK
  129. g_file_attribute_info_list_add (list,
  130. G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
  131. G_FILE_ATTRIBUTE_TYPE_BYTE_STRING,
  132. 0);
  133. #endif
  134. #ifdef HAVE_UTIMES
  135. g_file_attribute_info_list_add (list,
  136. G_FILE_ATTRIBUTE_TIME_MODIFIED,
  137. G_FILE_ATTRIBUTE_TYPE_UINT64,
  138. G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
  139. G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
  140. g_file_attribute_info_list_add (list,
  141. G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC,
  142. G_FILE_ATTRIBUTE_TYPE_UINT32,
  143. G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
  144. G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
  145. /* When copying, the target file is accessed. Replicating
  146. * the source access time does not make sense in this case.
  147. */
  148. g_file_attribute_info_list_add (list,
  149. G_FILE_ATTRIBUTE_TIME_ACCESS,
  150. G_FILE_ATTRIBUTE_TYPE_UINT64,
  151. G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
  152. g_file_attribute_info_list_add (list,
  153. G_FILE_ATTRIBUTE_TIME_ACCESS_USEC,
  154. G_FILE_ATTRIBUTE_TYPE_UINT32,
  155. G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
  156. #endif
  157. local_writable_attributes = list;
  158. }
  159. static void
  160. g_local_file_init (GLocalFile *local)
  161. {
  162. }
  163. const char *
  164. _g_local_file_get_filename (GLocalFile *file)
  165. {
  166. return file->filename;
  167. }
  168. static char *
  169. canonicalize_filename (const char *filename)
  170. {
  171. char *canon, *start, *p, *q;
  172. char *cwd;
  173. int i;
  174. if (!g_path_is_absolute (filename))
  175. {
  176. cwd = g_get_current_dir ();
  177. canon = g_build_filename (cwd, filename, NULL);
  178. g_free (cwd);
  179. }
  180. else
  181. canon = g_strdup (filename);
  182. start = (char *)g_path_skip_root (canon);
  183. if (start == NULL)
  184. {
  185. /* This shouldn't really happen, as g_get_current_dir() should
  186. return an absolute pathname, but bug 573843 shows this is
  187. not always happening */
  188. g_free (canon);
  189. return g_build_filename (G_DIR_SEPARATOR_S, filename, NULL);
  190. }
  191. /* POSIX allows double slashes at the start to
  192. * mean something special (as does windows too).
  193. * So, "//" != "/", but more than two slashes
  194. * is treated as "/".
  195. */
  196. i = 0;
  197. for (p = start - 1;
  198. (p >= canon) &&
  199. G_IS_DIR_SEPARATOR (*p);
  200. p--)
  201. i++;
  202. if (i > 2)
  203. {
  204. i -= 1;
  205. start -= i;
  206. memmove (start, start+i, strlen (start+i)+1);
  207. }
  208. /* Make sure we're using the canonical dir separator */
  209. p++;
  210. while (p < start && G_IS_DIR_SEPARATOR (*p))
  211. *p++ = G_DIR_SEPARATOR;
  212. p = start;
  213. while (*p != 0)
  214. {
  215. if (p[0] == '.' && (p[1] == 0 || G_IS_DIR_SEPARATOR (p[1])))
  216. {
  217. memmove (p, p+1, strlen (p+1)+1);
  218. }
  219. else if (p[0] == '.' && p[1] == '.' && (p[2] == 0 || G_IS_DIR_SEPARATOR (p[2])))
  220. {
  221. q = p + 2;
  222. /* Skip previous separator */
  223. p = p - 2;
  224. if (p < start)
  225. p = start;
  226. while (p > start && !G_IS_DIR_SEPARATOR (*p))
  227. p--;
  228. if (G_IS_DIR_SEPARATOR (*p))
  229. *p++ = G_DIR_SEPARATOR;
  230. memmove (p, q, strlen (q)+1);
  231. }
  232. else
  233. {
  234. /* Skip until next separator */
  235. while (*p != 0 && !G_IS_DIR_SEPARATOR (*p))
  236. p++;
  237. if (*p != 0)
  238. {
  239. /* Canonicalize one separator */
  240. *p++ = G_DIR_SEPARATOR;
  241. }
  242. }
  243. /* Remove additional separators */
  244. q = p;
  245. while (*q && G_IS_DIR_SEPARATOR (*q))
  246. q++;
  247. if (p != q)
  248. memmove (p, q, strlen (q)+1);
  249. }
  250. /* Remove trailing slashes */
  251. if (p > start && G_IS_DIR_SEPARATOR (*(p-1)))
  252. *(p-1) = 0;
  253. return canon;
  254. }
  255. GFile *
  256. _g_local_file_new (const char *filename)
  257. {
  258. GLocalFile *local;
  259. local = g_object_new (G_TYPE_LOCAL_FILE, NULL);
  260. local->filename = canonicalize_filename (filename);
  261. return G_FILE (local);
  262. }
  263. /*< internal >
  264. * g_local_file_new_from_dirname_and_basename:
  265. * @dirname: an absolute, canonical directory name
  266. * @basename: the name of a child inside @dirname
  267. *
  268. * Creates a #GFile from @dirname and @basename.
  269. *
  270. * This is more efficient than pasting the fields together for yourself
  271. * and creating a #GFile from the result, and also more efficient than
  272. * creating a #GFile for the dirname and using g_file_get_child().
  273. *
  274. * @dirname must be canonical, as per GLocalFile's opinion of what
  275. * canonical means. This means that you should only pass strings that
  276. * were returned by _g_local_file_get_filename().
  277. *
  278. * Returns: a #GFile
  279. */
  280. GFile *
  281. g_local_file_new_from_dirname_and_basename (const gchar *dirname,
  282. const gchar *basename)
  283. {
  284. GLocalFile *local;
  285. g_return_val_if_fail (dirname != NULL, NULL);
  286. g_return_val_if_fail (basename && basename[0] && !strchr (basename, '/'), NULL);
  287. local = g_object_new (G_TYPE_LOCAL_FILE, NULL);
  288. local->filename = g_build_filename (dirname, basename, NULL);
  289. return G_FILE (local);
  290. }
  291. static gboolean
  292. g_local_file_is_native (GFile *file)
  293. {
  294. return TRUE;
  295. }
  296. static gboolean
  297. g_local_file_has_uri_scheme (GFile *file,
  298. const char *uri_scheme)
  299. {
  300. return g_ascii_strcasecmp (uri_scheme, "file") == 0;
  301. }
  302. static char *
  303. g_local_file_get_uri_scheme (GFile *file)
  304. {
  305. return g_strdup ("file");
  306. }
  307. static char *
  308. g_local_file_get_basename (GFile *file)
  309. {
  310. return g_path_get_basename (G_LOCAL_FILE (file)->filename);
  311. }
  312. static char *
  313. g_local_file_get_path (GFile *file)
  314. {
  315. return g_strdup (G_LOCAL_FILE (file)->filename);
  316. }
  317. static char *
  318. g_local_file_get_uri (GFile *file)
  319. {
  320. return g_filename_to_uri (G_LOCAL_FILE (file)->filename, NULL, NULL);
  321. }
  322. static gboolean
  323. get_filename_charset (const gchar **filename_charset)
  324. {
  325. const gchar **charsets;
  326. gboolean is_utf8;
  327. is_utf8 = g_get_filename_charsets (&charsets);
  328. if (filename_charset)
  329. *filename_charset = charsets[0];
  330. return is_utf8;
  331. }
  332. static gboolean
  333. name_is_valid_for_display (const char *string,
  334. gboolean is_valid_utf8)
  335. {
  336. char c;
  337. if (!is_valid_utf8 &&
  338. !g_utf8_validate (string, -1, NULL))
  339. return FALSE;
  340. while ((c = *string++) != 0)
  341. {
  342. if (g_ascii_iscntrl (c))
  343. return FALSE;
  344. }
  345. return TRUE;
  346. }
  347. static char *
  348. g_local_file_get_parse_name (GFile *file)
  349. {
  350. const char *filename;
  351. char *parse_name;
  352. const gchar *charset;
  353. char *utf8_filename;
  354. char *roundtripped_filename;
  355. gboolean free_utf8_filename;
  356. gboolean is_valid_utf8;
  357. char *escaped_path;
  358. filename = G_LOCAL_FILE (file)->filename;
  359. if (get_filename_charset (&charset))
  360. {
  361. utf8_filename = (char *)filename;
  362. free_utf8_filename = FALSE;
  363. is_valid_utf8 = FALSE; /* Can't guarantee this */
  364. }
  365. else
  366. {
  367. utf8_filename = g_convert (filename, -1,
  368. "UTF-8", charset, NULL, NULL, NULL);
  369. free_utf8_filename = TRUE;
  370. is_valid_utf8 = TRUE;
  371. if (utf8_filename != NULL)
  372. {
  373. /* Make sure we can roundtrip: */
  374. roundtripped_filename = g_convert (utf8_filename, -1,
  375. charset, "UTF-8", NULL, NULL, NULL);
  376. if (roundtripped_filename == NULL ||
  377. strcmp (filename, roundtripped_filename) != 0)
  378. {
  379. g_free (utf8_filename);
  380. utf8_filename = NULL;
  381. }
  382. g_free (roundtripped_filename);
  383. }
  384. }
  385. if (utf8_filename != NULL &&
  386. name_is_valid_for_display (utf8_filename, is_valid_utf8))
  387. {
  388. if (free_utf8_filename)
  389. parse_name = utf8_filename;
  390. else
  391. parse_name = g_strdup (utf8_filename);
  392. }
  393. else
  394. {
  395. #ifdef G_OS_WIN32
  396. char *dup_filename, *p, *backslash;
  397. /* Turn backslashes into forward slashes like
  398. * g_filename_to_uri() would do (but we can't use that because
  399. * it doesn't output IRIs).
  400. */
  401. dup_filename = g_strdup (filename);
  402. filename = p = dup_filename;
  403. while ((backslash = strchr (p, '\\')) != NULL)
  404. {
  405. *backslash = '/';
  406. p = backslash + 1;
  407. }
  408. #endif
  409. escaped_path = g_uri_escape_string (filename,
  410. G_URI_RESERVED_CHARS_ALLOWED_IN_PATH_ELEMENT "/",
  411. TRUE);
  412. parse_name = g_strconcat ("file://",
  413. (*escaped_path != '/') ? "/" : "",
  414. escaped_path,
  415. NULL);
  416. g_free (escaped_path);
  417. #ifdef G_OS_WIN32
  418. g_free (dup_filename);
  419. #endif
  420. if (free_utf8_filename)
  421. g_free (utf8_filename);
  422. }
  423. return parse_name;
  424. }
  425. static GFile *
  426. g_local_file_get_parent (GFile *file)
  427. {
  428. GLocalFile *local = G_LOCAL_FILE (file);
  429. const char *non_root;
  430. char *dirname;
  431. GFile *parent;
  432. /* Check for root; local->filename is guaranteed to be absolute, so
  433. * g_path_skip_root() should never return NULL. */
  434. non_root = g_path_skip_root (local->filename);
  435. g_assert (non_root != NULL);
  436. if (*non_root == 0)
  437. return NULL;
  438. dirname = g_path_get_dirname (local->filename);
  439. parent = _g_local_file_new (dirname);
  440. g_free (dirname);
  441. return parent;
  442. }
  443. static GFile *
  444. g_local_file_dup (GFile *file)
  445. {
  446. GLocalFile *local = G_LOCAL_FILE (file);
  447. return _g_local_file_new (local->filename);
  448. }
  449. static guint
  450. g_local_file_hash (GFile *file)
  451. {
  452. GLocalFile *local = G_LOCAL_FILE (file);
  453. return g_str_hash (local->filename);
  454. }
  455. static gboolean
  456. g_local_file_equal (GFile *file1,
  457. GFile *file2)
  458. {
  459. GLocalFile *local1 = G_LOCAL_FILE (file1);
  460. GLocalFile *local2 = G_LOCAL_FILE (file2);
  461. return g_str_equal (local1->filename, local2->filename);
  462. }
  463. static const char *
  464. match_prefix (const char *path,
  465. const char *prefix)
  466. {
  467. int prefix_len;
  468. prefix_len = strlen (prefix);
  469. if (strncmp (path, prefix, prefix_len) != 0)
  470. return NULL;
  471. /* Handle the case where prefix is the root, so that
  472. * the IS_DIR_SEPRARATOR check below works */
  473. if (prefix_len > 0 &&
  474. G_IS_DIR_SEPARATOR (prefix[prefix_len-1]))
  475. prefix_len--;
  476. return path + prefix_len;
  477. }
  478. static gboolean
  479. g_local_file_prefix_matches (GFile *parent,
  480. GFile *descendant)
  481. {
  482. GLocalFile *parent_local = G_LOCAL_FILE (parent);
  483. GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
  484. const char *remainder;
  485. remainder = match_prefix (descendant_local->filename, parent_local->filename);
  486. if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
  487. return TRUE;
  488. return FALSE;
  489. }
  490. static char *
  491. g_local_file_get_relative_path (GFile *parent,
  492. GFile *descendant)
  493. {
  494. GLocalFile *parent_local = G_LOCAL_FILE (parent);
  495. GLocalFile *descendant_local = G_LOCAL_FILE (descendant);
  496. const char *remainder;
  497. remainder = match_prefix (descendant_local->filename, parent_local->filename);
  498. if (remainder != NULL && G_IS_DIR_SEPARATOR (*remainder))
  499. return g_strdup (remainder + 1);
  500. return NULL;
  501. }
  502. static GFile *
  503. g_local_file_resolve_relative_path (GFile *file,
  504. const char *relative_path)
  505. {
  506. GLocalFile *local = G_LOCAL_FILE (file);
  507. char *filename;
  508. GFile *child;
  509. if (g_path_is_absolute (relative_path))
  510. return _g_local_file_new (relative_path);
  511. filename = g_build_filename (local->filename, relative_path, NULL);
  512. child = _g_local_file_new (filename);
  513. g_free (filename);
  514. return child;
  515. }
  516. static GFileEnumerator *
  517. g_local_file_enumerate_children (GFile *file,
  518. const char *attributes,
  519. GFileQueryInfoFlags flags,
  520. GCancellable *cancellable,
  521. GError **error)
  522. {
  523. GLocalFile *local = G_LOCAL_FILE (file);
  524. return _g_local_file_enumerator_new (local,
  525. attributes, flags,
  526. cancellable, error);
  527. }
  528. static GFile *
  529. g_local_file_get_child_for_display_name (GFile *file,
  530. const char *display_name,
  531. GError **error)
  532. {
  533. GFile *new_file;
  534. char *basename;
  535. basename = g_filename_from_utf8 (display_name, -1, NULL, NULL, NULL);
  536. if (basename == NULL)
  537. {
  538. g_set_error (error, G_IO_ERROR,
  539. G_IO_ERROR_INVALID_FILENAME,
  540. _("Invalid filename %s"), display_name);
  541. return NULL;
  542. }
  543. new_file = g_file_get_child (file, basename);
  544. g_free (basename);
  545. return new_file;
  546. }
  547. #if defined(USE_STATFS) && !defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
  548. static const char *
  549. get_fs_type (long f_type)
  550. {
  551. /* filesystem ids taken from linux manpage */
  552. switch (f_type)
  553. {
  554. case 0xadf5:
  555. return "adfs";
  556. case 0x5346414f:
  557. return "afs";
  558. case 0x0187:
  559. return "autofs";
  560. case 0xADFF:
  561. return "affs";
  562. case 0x42465331:
  563. return "befs";
  564. case 0x1BADFACE:
  565. return "bfs";
  566. case 0x9123683E:
  567. return "btrfs";
  568. case 0xFF534D42:
  569. return "cifs";
  570. case 0x73757245:
  571. return "coda";
  572. case 0x012FF7B7:
  573. return "coh";
  574. case 0x28cd3d45:
  575. return "cramfs";
  576. case 0x1373:
  577. return "devfs";
  578. case 0x00414A53:
  579. return "efs";
  580. case 0x137D:
  581. return "ext";
  582. case 0xEF51:
  583. return "ext2";
  584. case 0xEF53:
  585. return "ext3/ext4";
  586. case 0x4244:
  587. return "hfs";
  588. case 0xF995E849:
  589. return "hpfs";
  590. case 0x958458f6:
  591. return "hugetlbfs";
  592. case 0x9660:
  593. return "isofs";
  594. case 0x72b6:
  595. return "jffs2";
  596. case 0x3153464a:
  597. return "jfs";
  598. case 0x137F:
  599. return "minix";
  600. case 0x138F:
  601. return "minix2";
  602. case 0x2468:
  603. return "minix2";
  604. case 0x2478:
  605. return "minix22";
  606. case 0x4d44:
  607. return "msdos";
  608. case 0x564c:
  609. return "ncp";
  610. case 0x6969:
  611. return "nfs";
  612. case 0x5346544e:
  613. return "ntfs";
  614. case 0x9fa1:
  615. return "openprom";
  616. case 0x9fa0:
  617. return "proc";
  618. case 0x002f:
  619. return "qnx4";
  620. case 0x52654973:
  621. return "reiserfs";
  622. case 0x7275:
  623. return "romfs";
  624. case 0x517B:
  625. return "smb";
  626. case 0x73717368:
  627. return "squashfs";
  628. case 0x012FF7B6:
  629. return "sysv2";
  630. case 0x012FF7B5:
  631. return "sysv4";
  632. case 0x01021994:
  633. return "tmpfs";
  634. case 0x15013346:
  635. return "udf";
  636. case 0x00011954:
  637. return "ufs";
  638. case 0x9fa2:
  639. return "usbdevice";
  640. case 0xa501FCF5:
  641. return "vxfs";
  642. case 0x012FF7B4:
  643. return "xenix";
  644. case 0x58465342:
  645. return "xfs";
  646. case 0x012FD16D:
  647. return "xiafs";
  648. case 0x52345362:
  649. return "reiser4";
  650. default:
  651. return NULL;
  652. }
  653. }
  654. #endif
  655. #ifndef G_OS_WIN32
  656. G_LOCK_DEFINE_STATIC(mount_info_hash);
  657. static GHashTable *mount_info_hash = NULL;
  658. static guint64 mount_info_hash_cache_time = 0;
  659. typedef enum {
  660. MOUNT_INFO_READONLY = 1<<0
  661. } MountInfo;
  662. static gboolean
  663. device_equal (gconstpointer v1,
  664. gconstpointer v2)
  665. {
  666. return *(dev_t *)v1 == *(dev_t *)v2;
  667. }
  668. static guint
  669. device_hash (gconstpointer v)
  670. {
  671. return (guint) *(dev_t *)v;
  672. }
  673. static void
  674. get_mount_info (GFileInfo *fs_info,
  675. const char *path,
  676. GFileAttributeMatcher *matcher)
  677. {
  678. GStatBuf buf;
  679. gboolean got_info;
  680. gpointer info_as_ptr;
  681. guint mount_info;
  682. char *mountpoint;
  683. dev_t *dev;
  684. GUnixMountEntry *mount;
  685. guint64 cache_time;
  686. if (g_lstat (path, &buf) != 0)
  687. return;
  688. G_LOCK (mount_info_hash);
  689. if (mount_info_hash == NULL)
  690. mount_info_hash = g_hash_table_new_full (device_hash, device_equal,
  691. g_free, NULL);
  692. if (g_unix_mounts_changed_since (mount_info_hash_cache_time))
  693. g_hash_table_remove_all (mount_info_hash);
  694. got_info = g_hash_table_lookup_extended (mount_info_hash,
  695. &buf.st_dev,
  696. NULL,
  697. &info_as_ptr);
  698. G_UNLOCK (mount_info_hash);
  699. mount_info = GPOINTER_TO_UINT (info_as_ptr);
  700. if (!got_info)
  701. {
  702. mount_info = 0;
  703. mountpoint = find_mountpoint_for (path, buf.st_dev);
  704. if (mountpoint == NULL)
  705. mountpoint = g_strdup ("/");
  706. mount = g_unix_mount_at (mountpoint, &cache_time);
  707. if (mount)
  708. {
  709. if (g_unix_mount_is_readonly (mount))
  710. mount_info |= MOUNT_INFO_READONLY;
  711. g_unix_mount_free (mount);
  712. }
  713. g_free (mountpoint);
  714. dev = g_new0 (dev_t, 1);
  715. *dev = buf.st_dev;
  716. G_LOCK (mount_info_hash);
  717. mount_info_hash_cache_time = cache_time;
  718. g_hash_table_insert (mount_info_hash, dev, GUINT_TO_POINTER (mount_info));
  719. G_UNLOCK (mount_info_hash);
  720. }
  721. if (mount_info & MOUNT_INFO_READONLY)
  722. g_file_info_set_attribute_boolean (fs_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
  723. }
  724. #endif
  725. #ifdef G_OS_WIN32
  726. static gboolean
  727. is_xp_or_later (void)
  728. {
  729. static int result = -1;
  730. if (result == -1)
  731. {
  732. #ifndef _MSC_VER
  733. OSVERSIONINFOEX ver_info = {0};
  734. DWORDLONG cond_mask = 0;
  735. int op = VER_GREATER_EQUAL;
  736. ver_info.dwOSVersionInfoSize = sizeof ver_info;
  737. ver_info.dwMajorVersion = 5;
  738. ver_info.dwMinorVersion = 1;
  739. VER_SET_CONDITION (cond_mask, VER_MAJORVERSION, op);
  740. VER_SET_CONDITION (cond_mask, VER_MINORVERSION, op);
  741. result = VerifyVersionInfo (&ver_info,
  742. VER_MAJORVERSION | VER_MINORVERSION,
  743. cond_mask) != 0;
  744. #else
  745. result = ((DWORD)(LOBYTE (LOWORD (GetVersion ())))) >= 5;
  746. #endif
  747. }
  748. return result;
  749. }
  750. static wchar_t *
  751. get_volume_for_path (const char *path)
  752. {
  753. long len;
  754. wchar_t *wpath;
  755. wchar_t *result;
  756. wpath = g_utf8_to_utf16 (path, -1, NULL, NULL, NULL);
  757. result = g_new (wchar_t, MAX_PATH);
  758. if (!GetVolumePathNameW (wpath, result, MAX_PATH))
  759. {
  760. char *msg = g_win32_error_message (GetLastError ());
  761. g_critical ("GetVolumePathName failed: %s", msg);
  762. g_free (msg);
  763. g_free (result);
  764. g_free (wpath);
  765. return NULL;
  766. }
  767. len = wcslen (result);
  768. if (len > 0 && result[len-1] != L'\\')
  769. {
  770. result = g_renew (wchar_t, result, len + 2);
  771. result[len] = L'\\';
  772. result[len + 1] = 0;
  773. }
  774. g_free (wpath);
  775. return result;
  776. }
  777. static char *
  778. find_mountpoint_for (const char *file, dev_t dev)
  779. {
  780. wchar_t *wpath;
  781. char *utf8_path;
  782. wpath = get_volume_for_path (file);
  783. if (!wpath)
  784. return NULL;
  785. utf8_path = g_utf16_to_utf8 (wpath, -1, NULL, NULL, NULL);
  786. g_free (wpath);
  787. return utf8_path;
  788. }
  789. static void
  790. get_filesystem_readonly (GFileInfo *info,
  791. const char *path)
  792. {
  793. wchar_t *rootdir;
  794. rootdir = get_volume_for_path (path);
  795. if (rootdir)
  796. {
  797. if (is_xp_or_later ())
  798. {
  799. DWORD flags;
  800. if (GetVolumeInformationW (rootdir, NULL, 0, NULL, NULL, &flags, NULL, 0))
  801. g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY,
  802. (flags & FILE_READ_ONLY_VOLUME) != 0);
  803. }
  804. else
  805. {
  806. if (GetDriveTypeW (rootdir) == DRIVE_CDROM)
  807. g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, TRUE);
  808. }
  809. }
  810. g_free (rootdir);
  811. }
  812. #endif /* G_OS_WIN32 */
  813. #pragma GCC diagnostic push
  814. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  815. static void
  816. g_set_io_error (GError **error,
  817. const gchar *msg,
  818. GFile *file,
  819. gint errsv)
  820. {
  821. GLocalFile *local = G_LOCAL_FILE (file);
  822. gchar *display_name;
  823. display_name = g_filename_display_name (local->filename);
  824. g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv),
  825. msg, display_name, g_strerror (errsv));
  826. g_free (display_name);
  827. }
  828. #pragma GCC diagnostic pop
  829. static GFileInfo *
  830. g_local_file_query_filesystem_info (GFile *file,
  831. const char *attributes,
  832. GCancellable *cancellable,
  833. GError **error)
  834. {
  835. GLocalFile *local = G_LOCAL_FILE (file);
  836. GFileInfo *info;
  837. int statfs_result = 0;
  838. gboolean no_size;
  839. #ifndef G_OS_WIN32
  840. const char *fstype;
  841. #ifdef USE_STATFS
  842. guint64 block_size;
  843. struct statfs statfs_buffer;
  844. #elif defined(USE_STATVFS)
  845. guint64 block_size;
  846. struct statvfs statfs_buffer;
  847. #endif /* USE_STATFS */
  848. #endif /* G_OS_WIN32 */
  849. GFileAttributeMatcher *attribute_matcher;
  850. no_size = FALSE;
  851. #ifdef USE_STATFS
  852. #if STATFS_ARGS == 2
  853. statfs_result = statfs (local->filename, &statfs_buffer);
  854. #elif STATFS_ARGS == 4
  855. statfs_result = statfs (local->filename, &statfs_buffer,
  856. sizeof (statfs_buffer), 0);
  857. #endif /* STATFS_ARGS == 2 */
  858. block_size = statfs_buffer.f_bsize;
  859. /* Many backends can't report free size (for instance the gvfs fuse
  860. backend for backend not supporting this), and set f_bfree to 0,
  861. but it can be 0 for real too. We treat the available == 0 and
  862. free == 0 case as "both of these are invalid".
  863. */
  864. #ifndef G_OS_WIN32
  865. if (statfs_result == 0 &&
  866. statfs_buffer.f_bavail == 0 && statfs_buffer.f_bfree == 0)
  867. no_size = TRUE;
  868. #endif /* G_OS_WIN32 */
  869. #elif defined(USE_STATVFS)
  870. statfs_result = statvfs (local->filename, &statfs_buffer);
  871. block_size = statfs_buffer.f_frsize;
  872. #endif /* USE_STATFS */
  873. if (statfs_result == -1)
  874. {
  875. int errsv = errno;
  876. g_set_io_error (error,
  877. _("Error getting filesystem info for %s: %s"),
  878. file, errsv);
  879. return NULL;
  880. }
  881. info = g_file_info_new ();
  882. attribute_matcher = g_file_attribute_matcher_new (attributes);
  883. if (!no_size &&
  884. g_file_attribute_matcher_matches (attribute_matcher,
  885. G_FILE_ATTRIBUTE_FILESYSTEM_FREE))
  886. {
  887. #ifdef G_OS_WIN32
  888. gchar *localdir = g_path_get_dirname (local->filename);
  889. wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
  890. ULARGE_INTEGER li;
  891. g_free (localdir);
  892. if (GetDiskFreeSpaceExW (wdirname, &li, NULL, NULL))
  893. g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, (guint64)li.QuadPart);
  894. g_free (wdirname);
  895. #else
  896. #if defined(USE_STATFS) || defined(USE_STATVFS)
  897. g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE, block_size * statfs_buffer.f_bavail);
  898. #endif
  899. #endif
  900. }
  901. if (!no_size &&
  902. g_file_attribute_matcher_matches (attribute_matcher,
  903. G_FILE_ATTRIBUTE_FILESYSTEM_SIZE))
  904. {
  905. #ifdef G_OS_WIN32
  906. gchar *localdir = g_path_get_dirname (local->filename);
  907. wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
  908. ULARGE_INTEGER li;
  909. g_free (localdir);
  910. if (GetDiskFreeSpaceExW (wdirname, NULL, &li, NULL))
  911. g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, (guint64)li.QuadPart);
  912. g_free (wdirname);
  913. #else
  914. #if defined(USE_STATFS) || defined(USE_STATVFS)
  915. g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE, block_size * statfs_buffer.f_blocks);
  916. #endif
  917. #endif /* G_OS_WIN32 */
  918. }
  919. if (!no_size &&
  920. g_file_attribute_matcher_matches (attribute_matcher,
  921. G_FILE_ATTRIBUTE_FILESYSTEM_USED))
  922. {
  923. #ifdef G_OS_WIN32
  924. gchar *localdir = g_path_get_dirname (local->filename);
  925. wchar_t *wdirname = g_utf8_to_utf16 (localdir, -1, NULL, NULL, NULL);
  926. ULARGE_INTEGER li_free;
  927. ULARGE_INTEGER li_total;
  928. g_free (localdir);
  929. if (GetDiskFreeSpaceExW (wdirname, &li_free, &li_total, NULL))
  930. g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_USED, (guint64)li_total.QuadPart - (guint64)li_free.QuadPart);
  931. g_free (wdirname);
  932. #else
  933. g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_FILESYSTEM_USED, block_size * (statfs_buffer.f_blocks - statfs_buffer.f_bfree));
  934. #endif /* G_OS_WIN32 */
  935. }
  936. #ifndef G_OS_WIN32
  937. #ifdef USE_STATFS
  938. #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
  939. fstype = g_strdup (statfs_buffer.f_fstypename);
  940. #else
  941. fstype = get_fs_type (statfs_buffer.f_type);
  942. #endif
  943. #elif defined(USE_STATVFS)
  944. #if defined(HAVE_STRUCT_STATVFS_F_FSTYPENAME)
  945. fstype = g_strdup (statfs_buffer.f_fstypename);
  946. #elif defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
  947. fstype = g_strdup (statfs_buffer.f_basetype);
  948. #else
  949. fstype = NULL;
  950. #endif
  951. #endif /* USE_STATFS */
  952. if (fstype &&
  953. g_file_attribute_matcher_matches (attribute_matcher,
  954. G_FILE_ATTRIBUTE_FILESYSTEM_TYPE))
  955. g_file_info_set_attribute_string (info, G_FILE_ATTRIBUTE_FILESYSTEM_TYPE, fstype);
  956. #endif /* G_OS_WIN32 */
  957. if (g_file_attribute_matcher_matches (attribute_matcher,
  958. G_FILE_ATTRIBUTE_FILESYSTEM_READONLY))
  959. {
  960. #ifdef G_OS_WIN32
  961. get_filesystem_readonly (info, local->filename);
  962. #else
  963. get_mount_info (info, local->filename, attribute_matcher);
  964. #endif /* G_OS_WIN32 */
  965. }
  966. if (g_file_attribute_matcher_matches (attribute_matcher,
  967. G_FILE_ATTRIBUTE_FILESYSTEM_REMOTE))
  968. g_file_info_set_attribute_boolean (info, G_FILE_ATTRIBUTE_FILESYSTEM_REMOTE,
  969. g_local_file_is_remote (local->filename));
  970. g_file_attribute_matcher_unref (attribute_matcher);
  971. return info;
  972. }
  973. static GMount *
  974. g_local_file_find_enclosing_mount (GFile *file,
  975. GCancellable *cancellable,
  976. GError **error)
  977. {
  978. GLocalFile *local = G_LOCAL_FILE (file);
  979. GStatBuf buf;
  980. char *mountpoint;
  981. GMount *mount;
  982. if (g_lstat (local->filename, &buf) != 0)
  983. goto error;
  984. mountpoint = find_mountpoint_for (local->filename, buf.st_dev);
  985. if (mountpoint == NULL)
  986. goto error;
  987. mount = _g_mount_get_for_mount_path (mountpoint, cancellable);
  988. g_free (mountpoint);
  989. if (mount)
  990. return mount;
  991. error:
  992. g_set_io_error (error,
  993. /* Translators: This is an error message when trying to find
  994. * the enclosing (user visible) mount of a file, but none
  995. * exists.
  996. */
  997. _("Containing mount for file %s not found"),
  998. file, 0);
  999. return NULL;
  1000. }
  1001. static GFile *
  1002. g_local_file_set_display_name (GFile *file,
  1003. const char *display_name,
  1004. GCancellable *cancellable,
  1005. GError **error)
  1006. {
  1007. GLocalFile *local, *new_local;
  1008. GFile *new_file, *parent;
  1009. GStatBuf statbuf;
  1010. GVfsClass *class;
  1011. GVfs *vfs;
  1012. int errsv;
  1013. parent = g_file_get_parent (file);
  1014. if (parent == NULL)
  1015. {
  1016. g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
  1017. _("Can’t rename root directory"));
  1018. return NULL;
  1019. }
  1020. new_file = g_file_get_child_for_display_name (parent, display_name, error);
  1021. g_object_unref (parent);
  1022. if (new_file == NULL)
  1023. return NULL;
  1024. local = G_LOCAL_FILE (file);
  1025. new_local = G_LOCAL_FILE (new_file);
  1026. if (g_lstat (new_local->filename, &statbuf) == -1)
  1027. {
  1028. errsv = errno;
  1029. if (errsv != ENOENT)
  1030. {
  1031. g_set_io_error (error, _("Error renaming file %s: %s"), new_file, errsv);
  1032. return NULL;
  1033. }
  1034. }
  1035. else
  1036. {
  1037. g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_EXISTS,
  1038. _("Can’t rename file, filename already exists"));
  1039. return NULL;
  1040. }
  1041. if (g_rename (local->filename, new_local->filename) == -1)
  1042. {
  1043. errsv = errno;
  1044. if (errsv == EINVAL)
  1045. /* We can't get a rename file into itself error here,
  1046. * so this must be an invalid filename, on e.g. FAT
  1047. */
  1048. g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_FILENAME,
  1049. _("Invalid filename"));
  1050. else
  1051. g_set_io_error (error,
  1052. _("Error renaming file %s: %s"),
  1053. file, errsv);
  1054. g_object_unref (new_file);
  1055. return NULL;
  1056. }
  1057. vfs = g_vfs_get_default ();
  1058. class = G_VFS_GET_CLASS (vfs);
  1059. if (class->local_file_moved)
  1060. class->local_file_moved (vfs, local->filename, new_local->filename);
  1061. return new_file;
  1062. }
  1063. static GFileInfo *
  1064. g_local_file_query_info (GFile *file,
  1065. const char *attributes,
  1066. GFileQueryInfoFlags flags,
  1067. GCancellable *cancellable,
  1068. GError **error)
  1069. {
  1070. GLocalFile *local = G_LOCAL_FILE (file);
  1071. GFileInfo *info;
  1072. GFileAttributeMatcher *matcher;
  1073. char *basename, *dirname;
  1074. GLocalParentFileInfo parent_info;
  1075. matcher = g_file_attribute_matcher_new (attributes);
  1076. basename = g_path_get_basename (local->filename);
  1077. dirname = g_path_get_dirname (local->filename);
  1078. _g_local_file_info_get_parent_info (dirname, matcher, &parent_info);
  1079. g_free (dirname);
  1080. info = _g_local_file_info_get (basename, local->filename,
  1081. matcher, flags, &parent_info,
  1082. error);
  1083. _g_local_file_info_free_parent_info (&parent_info);
  1084. g_free (basename);
  1085. g_file_attribute_matcher_unref (matcher);
  1086. return info;
  1087. }
  1088. static GFileAttributeInfoList *
  1089. g_local_file_query_settable_attributes (GFile *file,
  1090. GCancellable *cancellable,
  1091. GError **error)
  1092. {
  1093. return g_file_attribute_info_list_ref (local_writable_attributes);
  1094. }
  1095. static GFileAttributeInfoList *
  1096. g_local_file_query_writable_namespaces (GFile *file,
  1097. GCancellable *cancellable,
  1098. GError **error)
  1099. {
  1100. GFileAttributeInfoList *list;
  1101. GVfsClass *class;
  1102. GVfs *vfs;
  1103. if (g_once_init_enter (&local_writable_namespaces))
  1104. {
  1105. /* Writable namespaces: */
  1106. list = g_file_attribute_info_list_new ();
  1107. #ifdef HAVE_XATTR
  1108. g_file_attribute_info_list_add (list,
  1109. "xattr",
  1110. G_FILE_ATTRIBUTE_TYPE_STRING,
  1111. G_FILE_ATTRIBUTE_INFO_COPY_WITH_FILE |
  1112. G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
  1113. g_file_attribute_info_list_add (list,
  1114. "xattr-sys",
  1115. G_FILE_ATTRIBUTE_TYPE_STRING,
  1116. G_FILE_ATTRIBUTE_INFO_COPY_WHEN_MOVED);
  1117. #endif
  1118. vfs = g_vfs_get_default ();
  1119. class = G_VFS_GET_CLASS (vfs);
  1120. if (class->add_writable_namespaces)
  1121. class->add_writable_namespaces (vfs, list);
  1122. g_once_init_leave (&local_writable_namespaces, (gsize)list);
  1123. }
  1124. list = (GFileAttributeInfoList *)local_writable_namespaces;
  1125. return g_file_attribute_info_list_ref (list);
  1126. }
  1127. static gboolean
  1128. g_local_file_set_attribute (GFile *file,
  1129. const char *attribute,
  1130. GFileAttributeType type,
  1131. gpointer value_p,
  1132. GFileQueryInfoFlags flags,
  1133. GCancellable *cancellable,
  1134. GError **error)
  1135. {
  1136. GLocalFile *local = G_LOCAL_FILE (file);
  1137. return _g_local_file_info_set_attribute (local->filename,
  1138. attribute,
  1139. type,
  1140. value_p,
  1141. flags,
  1142. cancellable,
  1143. error);
  1144. }
  1145. static gboolean
  1146. g_local_file_set_attributes_from_info (GFile *file,
  1147. GFileInfo *info,
  1148. GFileQueryInfoFlags flags,
  1149. GCancellable *cancellable,
  1150. GError **error)
  1151. {
  1152. GLocalFile *local = G_LOCAL_FILE (file);
  1153. int res, chained_res;
  1154. GFileIface *default_iface;
  1155. res = _g_local_file_info_set_attributes (local->filename,
  1156. info, flags,
  1157. cancellable,
  1158. error);
  1159. if (!res)
  1160. error = NULL; /* Don't write over error if further errors */
  1161. default_iface = g_type_default_interface_peek (G_TYPE_FILE);
  1162. chained_res = (default_iface->set_attributes_from_info) (file, info, flags, cancellable, error);
  1163. return res && chained_res;
  1164. }
  1165. static GFileInputStream *
  1166. g_local_file_read (GFile *file,
  1167. GCancellable *cancellable,
  1168. GError **error)
  1169. {
  1170. GLocalFile *local = G_LOCAL_FILE (file);
  1171. int fd, ret;
  1172. GLocalFileStat buf;
  1173. fd = g_open (local->filename, O_RDONLY|O_BINARY, 0);
  1174. if (fd == -1)
  1175. {
  1176. int errsv = errno;
  1177. #ifdef G_OS_WIN32
  1178. if (errsv == EACCES)
  1179. {
  1180. ret = _stati64 (local->filename, &buf);
  1181. if (ret == 0 && S_ISDIR (buf.st_mode))
  1182. errsv = EISDIR;
  1183. }
  1184. #endif
  1185. g_set_io_error (error,
  1186. _("Error opening file %s: %s"),
  1187. file, errsv);
  1188. return NULL;
  1189. }
  1190. #ifdef G_OS_WIN32
  1191. ret = _fstati64 (fd, &buf);
  1192. #else
  1193. ret = fstat (fd, &buf);
  1194. #endif
  1195. if (ret == 0 && S_ISDIR (buf.st_mode))
  1196. {
  1197. (void) g_close (fd, NULL);
  1198. g_set_io_error (error,
  1199. _("Error opening file %s: %s"),
  1200. file, EISDIR);
  1201. return NULL;
  1202. }
  1203. return _g_local_file_input_stream_new (fd);
  1204. }
  1205. static GFileOutputStream *
  1206. g_local_file_append_to (GFile *file,
  1207. GFileCreateFlags flags,
  1208. GCancellable *cancellable,
  1209. GError **error)
  1210. {
  1211. return _g_local_file_output_stream_append (G_LOCAL_FILE (file)->filename,
  1212. flags, cancellable, error);
  1213. }
  1214. static GFileOutputStream *
  1215. g_local_file_create (GFile *file,
  1216. GFileCreateFlags flags,
  1217. GCancellable *cancellable,
  1218. GError **error)
  1219. {
  1220. return _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
  1221. FALSE, flags, NULL,
  1222. cancellable, error);
  1223. }
  1224. static GFileOutputStream *
  1225. g_local_file_replace (GFile *file,
  1226. const char *etag,
  1227. gboolean make_backup,
  1228. GFileCreateFlags flags,
  1229. GCancellable *cancellable,
  1230. GError **error)
  1231. {
  1232. return _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
  1233. FALSE,
  1234. etag, make_backup, flags, NULL,
  1235. cancellable, error);
  1236. }
  1237. static GFileIOStream *
  1238. g_local_file_open_readwrite (GFile *file,
  1239. GCancellable *cancellable,
  1240. GError **error)
  1241. {
  1242. GFileOutputStream *output;
  1243. GFileIOStream *res;
  1244. output = _g_local_file_output_stream_open (G_LOCAL_FILE (file)->filename,
  1245. TRUE,
  1246. cancellable, error);
  1247. if (output == NULL)
  1248. return NULL;
  1249. res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
  1250. g_object_unref (output);
  1251. return res;
  1252. }
  1253. static GFileIOStream *
  1254. g_local_file_create_readwrite (GFile *file,
  1255. GFileCreateFlags flags,
  1256. GCancellable *cancellable,
  1257. GError **error)
  1258. {
  1259. GFileOutputStream *output;
  1260. GFileIOStream *res;
  1261. output = _g_local_file_output_stream_create (G_LOCAL_FILE (file)->filename,
  1262. TRUE, flags, NULL,
  1263. cancellable, error);
  1264. if (output == NULL)
  1265. return NULL;
  1266. res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
  1267. g_object_unref (output);
  1268. return res;
  1269. }
  1270. static GFileIOStream *
  1271. g_local_file_replace_readwrite (GFile *file,
  1272. const char *etag,
  1273. gboolean make_backup,
  1274. GFileCreateFlags flags,
  1275. GCancellable *cancellable,
  1276. GError **error)
  1277. {
  1278. GFileOutputStream *output;
  1279. GFileIOStream *res;
  1280. output = _g_local_file_output_stream_replace (G_LOCAL_FILE (file)->filename,
  1281. TRUE,
  1282. etag, make_backup, flags, NULL,
  1283. cancellable, error);
  1284. if (output == NULL)
  1285. return NULL;
  1286. res = _g_local_file_io_stream_new (G_LOCAL_FILE_OUTPUT_STREAM (output));
  1287. g_object_unref (output);
  1288. return res;
  1289. }
  1290. static gboolean
  1291. g_local_file_delete (GFile *file,
  1292. GCancellable *cancellable,
  1293. GError **error)
  1294. {
  1295. GLocalFile *local = G_LOCAL_FILE (file);
  1296. GVfsClass *class;
  1297. GVfs *vfs;
  1298. if (g_remove (local->filename) == -1)
  1299. {
  1300. int errsv = errno;
  1301. /* Posix allows EEXIST too, but the more sane error
  1302. is G_IO_ERROR_NOT_FOUND, and it's what nautilus
  1303. expects */
  1304. if (errsv == EEXIST)
  1305. errsv = ENOTEMPTY;
  1306. g_set_io_error (error,
  1307. _("Error removing file %s: %s"),
  1308. file, errsv);
  1309. return FALSE;
  1310. }
  1311. vfs = g_vfs_get_default ();
  1312. class = G_VFS_GET_CLASS (vfs);
  1313. if (class->local_file_removed)
  1314. class->local_file_removed (vfs, local->filename);
  1315. return TRUE;
  1316. }
  1317. #ifndef G_OS_WIN32
  1318. static char *
  1319. strip_trailing_slashes (const char *path)
  1320. {
  1321. char *path_copy;
  1322. int len;
  1323. path_copy = g_strdup (path);
  1324. len = strlen (path_copy);
  1325. while (len > 1 && path_copy[len-1] == '/')
  1326. path_copy[--len] = 0;
  1327. return path_copy;
  1328. }
  1329. static char *
  1330. expand_symlink (const char *link)
  1331. {
  1332. char *resolved, *canonical, *parent, *link2;
  1333. char symlink_value[4096];
  1334. #ifdef G_OS_WIN32
  1335. #else
  1336. ssize_t res;
  1337. #endif
  1338. #ifdef G_OS_WIN32
  1339. #else
  1340. res = readlink (link, symlink_value, sizeof (symlink_value) - 1);
  1341. if (res == -1)
  1342. return g_strdup (link);
  1343. symlink_value[res] = 0;
  1344. #endif
  1345. if (g_path_is_absolute (symlink_value))
  1346. return canonicalize_filename (symlink_value);
  1347. else
  1348. {
  1349. link2 = strip_trailing_slashes (link);
  1350. parent = g_path_get_dirname (link2);
  1351. g_free (link2);
  1352. resolved = g_build_filename (parent, symlink_value, NULL);
  1353. g_free (parent);
  1354. canonical = canonicalize_filename (resolved);
  1355. g_free (resolved);
  1356. return canonical;
  1357. }
  1358. }
  1359. static char *
  1360. get_parent (const char *path,
  1361. dev_t *parent_dev)
  1362. {
  1363. char *parent, *tmp;
  1364. GStatBuf parent_stat;
  1365. int num_recursions;
  1366. char *path_copy;
  1367. path_copy = strip_trailing_slashes (path);
  1368. parent = g_path_get_dirname (path_copy);
  1369. if (strcmp (parent, ".") == 0 ||
  1370. strcmp (parent, path_copy) == 0)
  1371. {
  1372. g_free (parent);
  1373. g_free (path_copy);
  1374. return NULL;
  1375. }
  1376. g_free (path_copy);
  1377. num_recursions = 0;
  1378. do {
  1379. if (g_lstat (parent, &parent_stat) != 0)
  1380. {
  1381. g_free (parent);
  1382. return NULL;
  1383. }
  1384. if (S_ISLNK (parent_stat.st_mode))
  1385. {
  1386. tmp = parent;
  1387. parent = expand_symlink (parent);
  1388. g_free (tmp);
  1389. }
  1390. num_recursions++;
  1391. if (num_recursions > 12)
  1392. {
  1393. g_free (parent);
  1394. return NULL;
  1395. }
  1396. } while (S_ISLNK (parent_stat.st_mode));
  1397. *parent_dev = parent_stat.st_dev;
  1398. return parent;
  1399. }
  1400. static char *
  1401. expand_all_symlinks (const char *path)
  1402. {
  1403. char *parent, *parent_expanded;
  1404. char *basename, *res;
  1405. dev_t parent_dev;
  1406. parent = get_parent (path, &parent_dev);
  1407. if (parent)
  1408. {
  1409. parent_expanded = expand_all_symlinks (parent);
  1410. g_free (parent);
  1411. basename = g_path_get_basename (path);
  1412. res = g_build_filename (parent_expanded, basename, NULL);
  1413. g_free (basename);
  1414. g_free (parent_expanded);
  1415. }
  1416. else
  1417. res = g_strdup (path);
  1418. return res;
  1419. }
  1420. static char *
  1421. find_mountpoint_for (const char *file,
  1422. dev_t dev)
  1423. {
  1424. char *dir, *parent;
  1425. dev_t dir_dev, parent_dev;
  1426. dir = g_strdup (file);
  1427. dir_dev = dev;
  1428. while (1)
  1429. {
  1430. parent = get_parent (dir, &parent_dev);
  1431. if (parent == NULL)
  1432. return dir;
  1433. if (parent_dev != dir_dev)
  1434. {
  1435. g_free (parent);
  1436. return dir;
  1437. }
  1438. g_free (dir);
  1439. dir = parent;
  1440. }
  1441. }
  1442. char *
  1443. _g_local_file_find_topdir_for (const char *file)
  1444. {
  1445. char *dir;
  1446. char *mountpoint = NULL;
  1447. dev_t dir_dev;
  1448. dir = get_parent (file, &dir_dev);
  1449. if (dir == NULL)
  1450. return NULL;
  1451. mountpoint = find_mountpoint_for (dir, dir_dev);
  1452. g_free (dir);
  1453. return mountpoint;
  1454. }
  1455. static char *
  1456. get_unique_filename (const char *basename,
  1457. int id)
  1458. {
  1459. const char *dot;
  1460. if (id == 1)
  1461. return g_strdup (basename);
  1462. dot = strchr (basename, '.');
  1463. if (dot)
  1464. return g_strdup_printf ("%.*s.%d%s", (int)(dot - basename), basename, id, dot);
  1465. else
  1466. return g_strdup_printf ("%s.%d", basename, id);
  1467. }
  1468. static gboolean
  1469. path_has_prefix (const char *path,
  1470. const char *prefix)
  1471. {
  1472. int prefix_len;
  1473. if (prefix == NULL)
  1474. return TRUE;
  1475. prefix_len = strlen (prefix);
  1476. if (strncmp (path, prefix, prefix_len) == 0 &&
  1477. (prefix_len == 0 || /* empty prefix always matches */
  1478. prefix[prefix_len - 1] == '/' || /* last char in prefix was a /, so it must be in path too */
  1479. path[prefix_len] == 0 ||
  1480. path[prefix_len] == '/'))
  1481. return TRUE;
  1482. return FALSE;
  1483. }
  1484. static char *
  1485. try_make_relative (const char *path,
  1486. const char *base)
  1487. {
  1488. char *path2, *base2;
  1489. char *relative;
  1490. path2 = expand_all_symlinks (path);
  1491. base2 = expand_all_symlinks (base);
  1492. relative = NULL;
  1493. if (path_has_prefix (path2, base2))
  1494. {
  1495. relative = path2 + strlen (base2);
  1496. while (*relative == '/')
  1497. relative ++;
  1498. relative = g_strdup (relative);
  1499. }
  1500. g_free (path2);
  1501. g_free (base2);
  1502. if (relative)
  1503. return relative;
  1504. /* Failed, use abs path */
  1505. return g_strdup (path);
  1506. }
  1507. gboolean
  1508. _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
  1509. {
  1510. static gsize home_dev_set = 0;
  1511. static dev_t home_dev;
  1512. char *topdir, *globaldir, *trashdir, *tmpname;
  1513. uid_t uid;
  1514. char uid_str[32];
  1515. GStatBuf global_stat, trash_stat;
  1516. gboolean res;
  1517. if (g_once_init_enter (&home_dev_set))
  1518. {
  1519. GStatBuf home_stat;
  1520. g_stat (g_get_home_dir (), &home_stat);
  1521. home_dev = home_stat.st_dev;
  1522. g_once_init_leave (&home_dev_set, 1);
  1523. }
  1524. /* Assume we can trash to the home */
  1525. if (dir_dev == home_dev)
  1526. return TRUE;
  1527. topdir = find_mountpoint_for (dirname, dir_dev);
  1528. if (topdir == NULL)
  1529. return FALSE;
  1530. globaldir = g_build_filename (topdir, ".Trash", NULL);
  1531. if (g_lstat (globaldir, &global_stat) == 0 &&
  1532. S_ISDIR (global_stat.st_mode) &&
  1533. (global_stat.st_mode & S_ISVTX) != 0)
  1534. {
  1535. /* got a toplevel sysadmin created dir, assume we
  1536. * can trash to it (we should be able to create a dir)
  1537. * This fails for the FAT case where the ownership of
  1538. * that dir would be wrong though..
  1539. */
  1540. g_free (globaldir);
  1541. g_free (topdir);
  1542. return TRUE;
  1543. }
  1544. g_free (globaldir);
  1545. /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
  1546. uid = geteuid ();
  1547. g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long) uid);
  1548. tmpname = g_strdup_printf (".Trash-%s", uid_str);
  1549. trashdir = g_build_filename (topdir, tmpname, NULL);
  1550. g_free (tmpname);
  1551. if (g_lstat (trashdir, &trash_stat) == 0)
  1552. {
  1553. g_free (topdir);
  1554. g_free (trashdir);
  1555. return S_ISDIR (trash_stat.st_mode) &&
  1556. trash_stat.st_uid == uid;
  1557. }
  1558. g_free (trashdir);
  1559. /* User specific trash didn't exist, can we create it? */
  1560. res = g_access (topdir, W_OK) == 0;
  1561. g_free (topdir);
  1562. return res;
  1563. }
  1564. #ifdef G_OS_UNIX
  1565. gboolean
  1566. _g_local_file_is_lost_found_dir (const char *path, dev_t path_dev)
  1567. {
  1568. gboolean ret = FALSE;
  1569. gchar *mount_dir = NULL;
  1570. size_t mount_dir_len;
  1571. GStatBuf statbuf;
  1572. if (!g_str_has_suffix (path, "/lost+found"))
  1573. goto out;
  1574. mount_dir = find_mountpoint_for (path, path_dev);
  1575. if (mount_dir == NULL)
  1576. goto out;
  1577. mount_dir_len = strlen (mount_dir);
  1578. /* We special-case rootfs ('/') since it's the only case where
  1579. * mount_dir ends in '/'
  1580. */
  1581. if (mount_dir_len == 1)
  1582. mount_dir_len--;
  1583. if (mount_dir_len + strlen ("/lost+found") != strlen (path))
  1584. goto out;
  1585. if (g_lstat (path, &statbuf) != 0)
  1586. goto out;
  1587. if (!(S_ISDIR (statbuf.st_mode) &&
  1588. statbuf.st_uid == 0 &&
  1589. statbuf.st_gid == 0))
  1590. goto out;
  1591. ret = TRUE;
  1592. out:
  1593. g_free (mount_dir);
  1594. return ret;
  1595. }
  1596. #endif
  1597. static gboolean
  1598. g_local_file_trash (GFile *file,
  1599. GCancellable *cancellable,
  1600. GError **error)
  1601. {
  1602. GLocalFile *local = G_LOCAL_FILE (file);
  1603. GStatBuf file_stat, home_stat;
  1604. const char *homedir;
  1605. char *trashdir, *topdir, *infodir, *filesdir;
  1606. char *basename, *trashname, *trashfile, *infoname, *infofile;
  1607. char *original_name, *original_name_escaped;
  1608. int i;
  1609. char *data;
  1610. gboolean is_homedir_trash;
  1611. char delete_time[32];
  1612. int fd;
  1613. GStatBuf trash_stat, global_stat;
  1614. char *dirname, *globaldir;
  1615. GVfsClass *class;
  1616. GVfs *vfs;
  1617. int errsv;
  1618. if (g_lstat (local->filename, &file_stat) != 0)
  1619. {
  1620. errsv = errno;
  1621. g_set_io_error (error,
  1622. _("Error trashing file %s: %s"),
  1623. file, errsv);
  1624. return FALSE;
  1625. }
  1626. homedir = g_get_home_dir ();
  1627. g_stat (homedir, &home_stat);
  1628. is_homedir_trash = FALSE;
  1629. trashdir = NULL;
  1630. if (file_stat.st_dev == home_stat.st_dev)
  1631. {
  1632. is_homedir_trash = TRUE;
  1633. errno = 0;
  1634. trashdir = g_build_filename (g_get_user_data_dir (), "Trash", NULL);
  1635. if (g_mkdir_with_parents (trashdir, 0700) < 0)
  1636. {
  1637. char *display_name;
  1638. errsv = errno;
  1639. display_name = g_filename_display_name (trashdir);
  1640. g_set_error (error, G_IO_ERROR,
  1641. g_io_error_from_errno (errsv),
  1642. _("Unable to create trash dir %s: %s"),
  1643. display_name, g_strerror (errsv));
  1644. g_free (display_name);
  1645. g_free (trashdir);
  1646. return FALSE;
  1647. }
  1648. topdir = g_strdup (g_get_user_data_dir ());
  1649. }
  1650. else
  1651. {
  1652. uid_t uid;
  1653. char uid_str[32];
  1654. uid = geteuid ();
  1655. g_snprintf (uid_str, sizeof (uid_str), "%lu", (unsigned long)uid);
  1656. topdir = _g_local_file_find_topdir_for (local->filename);
  1657. if (topdir == NULL)
  1658. {
  1659. g_set_io_error (error,
  1660. _("Unable to find toplevel directory to trash %s"),
  1661. file, G_IO_ERROR_NOT_SUPPORTED);
  1662. return FALSE;
  1663. }
  1664. /* Try looking for global trash dir $topdir/.Trash/$uid */
  1665. globaldir = g_build_filename (topdir, ".Trash", NULL);
  1666. if (g_lstat (globaldir, &global_stat) == 0 &&
  1667. S_ISDIR (global_stat.st_mode) &&
  1668. (global_stat.st_mode & S_ISVTX) != 0)
  1669. {
  1670. trashdir = g_build_filename (globaldir, uid_str, NULL);
  1671. if (g_lstat (trashdir, &trash_stat) == 0)
  1672. {
  1673. if (!S_ISDIR (trash_stat.st_mode) ||
  1674. trash_stat.st_uid != uid)
  1675. {
  1676. /* Not a directory or not owned by user, ignore */
  1677. g_free (trashdir);
  1678. trashdir = NULL;
  1679. }
  1680. }
  1681. else if (g_mkdir (trashdir, 0700) == -1)
  1682. {
  1683. g_free (trashdir);
  1684. trashdir = NULL;
  1685. }
  1686. }
  1687. g_free (globaldir);
  1688. if (trashdir == NULL)
  1689. {
  1690. gboolean tried_create;
  1691. /* No global trash dir, or it failed the tests, fall back to $topdir/.Trash-$uid */
  1692. dirname = g_strdup_printf (".Trash-%s", uid_str);
  1693. trashdir = g_build_filename (topdir, dirname, NULL);
  1694. g_free (dirname);
  1695. tried_create = FALSE;
  1696. retry:
  1697. if (g_lstat (trashdir, &trash_stat) == 0)
  1698. {
  1699. if (!S_ISDIR (trash_stat.st_mode) ||
  1700. trash_stat.st_uid != uid)
  1701. {
  1702. /* Remove the failed directory */
  1703. if (tried_create)
  1704. g_remove (trashdir);
  1705. /* Not a directory or not owned by user, ignore */
  1706. g_free (trashdir);
  1707. trashdir = NULL;
  1708. }
  1709. }
  1710. else
  1711. {
  1712. if (!tried_create &&
  1713. g_mkdir (trashdir, 0700) != -1)
  1714. {
  1715. /* Ensure that the created dir has the right uid etc.
  1716. This might fail on e.g. a FAT dir */
  1717. tried_create = TRUE;
  1718. goto retry;
  1719. }
  1720. else
  1721. {
  1722. g_free (trashdir);
  1723. trashdir = NULL;
  1724. }
  1725. }
  1726. }
  1727. if (trashdir == NULL)
  1728. {
  1729. g_free (topdir);
  1730. g_set_io_error (error,
  1731. _("Unable to find or create trash directory for %s"),
  1732. file, G_IO_ERROR_NOT_SUPPORTED);
  1733. return FALSE;
  1734. }
  1735. }
  1736. /* Trashdir points to the trash dir with the "info" and "files" subdirectories */
  1737. infodir = g_build_filename (trashdir, "info", NULL);
  1738. filesdir = g_build_filename (trashdir, "files", NULL);
  1739. g_free (trashdir);
  1740. /* Make sure we have the subdirectories */
  1741. if ((g_mkdir (infodir, 0700) == -1 && errno != EEXIST) ||
  1742. (g_mkdir (filesdir, 0700) == -1 && errno != EEXIST))
  1743. {
  1744. g_free (topdir);
  1745. g_free (infodir);
  1746. g_free (filesdir);
  1747. g_set_io_error (error,
  1748. _("Unable to find or create trash directory for %s"),
  1749. file, G_IO_ERROR_NOT_SUPPORTED);
  1750. return FALSE;
  1751. }
  1752. basename = g_path_get_basename (local->filename);
  1753. i = 1;
  1754. trashname = NULL;
  1755. infofile = NULL;
  1756. do {
  1757. g_free (trashname);
  1758. g_free (infofile);
  1759. trashname = get_unique_filename (basename, i++);
  1760. infoname = g_strconcat (trashname, ".trashinfo", NULL);
  1761. infofile = g_build_filename (infodir, infoname, NULL);
  1762. g_free (infoname);
  1763. fd = g_open (infofile, O_CREAT | O_EXCL, 0666);
  1764. errsv = errno;
  1765. } while (fd == -1 && errsv == EEXIST);
  1766. g_free (basename);
  1767. g_free (infodir);
  1768. if (fd == -1)
  1769. {
  1770. errsv = errno;
  1771. g_free (filesdir);
  1772. g_free (topdir);
  1773. g_free (trashname);
  1774. g_free (infofile);
  1775. g_set_io_error (error,
  1776. _("Unable to create trashing info file for %s: %s"),
  1777. file, errsv);
  1778. return FALSE;
  1779. }
  1780. (void) g_close (fd, NULL);
  1781. /* Write the full content of the info file before trashing to make
  1782. * sure someone doesn't read an empty file. See #749314
  1783. */
  1784. /* Use absolute names for homedir */
  1785. if (is_homedir_trash)
  1786. original_name = g_strdup (local->filename);
  1787. else
  1788. original_name = try_make_relative (local->filename, topdir);
  1789. original_name_escaped = g_uri_escape_string (original_name, "/", FALSE);
  1790. g_free (original_name);
  1791. g_free (topdir);
  1792. {
  1793. time_t t;
  1794. struct tm now;
  1795. t = time (NULL);
  1796. localtime_r (&t, &now);
  1797. delete_time[0] = 0;
  1798. strftime(delete_time, sizeof (delete_time), "%Y-%m-%dT%H:%M:%S", &now);
  1799. }
  1800. data = g_strdup_printf ("[Trash Info]\nPath=%s\nDeletionDate=%s\n",
  1801. original_name_escaped, delete_time);
  1802. g_file_set_contents (infofile, data, -1, NULL);
  1803. /* TODO: Maybe we should verify that you can delete the file from the trash
  1804. * before moving it? OTOH, that is hard, as it needs a recursive scan
  1805. */
  1806. trashfile = g_build_filename (filesdir, trashname, NULL);
  1807. g_free (filesdir);
  1808. if (g_rename (local->filename, trashfile) == -1)
  1809. {
  1810. errsv = errno;
  1811. g_unlink (infofile);
  1812. g_free (trashname);
  1813. g_free (infofile);
  1814. g_free (trashfile);
  1815. if (errsv == EXDEV)
  1816. /* The trash dir was actually on another fs anyway!?
  1817. * This can happen when the same device is mounted multiple
  1818. * times, or with bind mounts of the same fs.
  1819. */
  1820. g_set_io_error (error,
  1821. _("Unable to trash file %s across filesystem boundaries"),
  1822. file, ENOTSUP);
  1823. else
  1824. g_set_io_error (error,
  1825. _("Unable to trash file %s: %s"),
  1826. file, errsv);
  1827. return FALSE;
  1828. }
  1829. vfs = g_vfs_get_default ();
  1830. class = G_VFS_GET_CLASS (vfs);
  1831. if (class->local_file_moved)
  1832. class->local_file_moved (vfs, local->filename, trashfile);
  1833. g_free (trashfile);
  1834. /* TODO: Do we need to update mtime/atime here after the move? */
  1835. g_free (infofile);
  1836. g_free (data);
  1837. g_free (original_name_escaped);
  1838. g_free (trashname);
  1839. return TRUE;
  1840. }
  1841. #else /* G_OS_WIN32 */
  1842. gboolean
  1843. _g_local_file_has_trash_dir (const char *dirname, dev_t dir_dev)
  1844. {
  1845. return FALSE; /* XXX ??? */
  1846. }
  1847. static gboolean
  1848. g_local_file_trash (GFile *file,
  1849. GCancellable *cancellable,
  1850. GError **error)
  1851. {
  1852. GLocalFile *local = G_LOCAL_FILE (file);
  1853. SHFILEOPSTRUCTW op = {0};
  1854. gboolean success;
  1855. wchar_t *wfilename;
  1856. long len;
  1857. wfilename = g_utf8_to_utf16 (local->filename, -1, NULL, &len, NULL);
  1858. /* SHFILEOPSTRUCT.pFrom is double-zero-terminated */
  1859. wfilename = g_renew (wchar_t, wfilename, len + 2);
  1860. wfilename[len + 1] = 0;
  1861. op.wFunc = FO_DELETE;
  1862. op.pFrom = wfilename;
  1863. op.fFlags = FOF_ALLOWUNDO;
  1864. success = SHFileOperationW (&op) == 0;
  1865. if (success && op.fAnyOperationsAborted)
  1866. {
  1867. if (cancellable && !g_cancellable_is_cancelled (cancellable))
  1868. g_cancellable_cancel (cancellable);
  1869. g_set_io_error (error,
  1870. _("Unable to trash file %s: %s"),
  1871. file, ECANCELED);
  1872. success = FALSE;
  1873. }
  1874. else if (!success)
  1875. g_set_io_error (error,
  1876. _("Unable to trash file %s"),
  1877. file, 0);
  1878. g_free (wfilename);
  1879. return success;
  1880. }
  1881. #endif /* G_OS_WIN32 */
  1882. static gboolean
  1883. g_local_file_make_directory (GFile *file,
  1884. GCancellable *cancellable,
  1885. GError **error)
  1886. {
  1887. GLocalFile *local = G_LOCAL_FILE (file);
  1888. if (g_mkdir (local->filename, 0777) == -1)
  1889. {
  1890. int errsv = errno;
  1891. if (errsv == EINVAL)
  1892. /* This must be an invalid filename, on e.g. FAT */
  1893. g_set_error_literal (error, G_IO_ERROR,
  1894. G_IO_ERROR_INVALID_FILENAME,
  1895. _("Invalid filename"));
  1896. else
  1897. g_set_io_error (error,
  1898. _("Error creating directory %s: %s"),
  1899. file, errsv);
  1900. return FALSE;
  1901. }
  1902. return TRUE;
  1903. }
  1904. static gboolean
  1905. g_local_file_make_symbolic_link (GFile *file,
  1906. const char *symlink_value,
  1907. GCancellable *cancellable,
  1908. GError **error)
  1909. {
  1910. #ifdef HAVE_SYMLINK
  1911. GLocalFile *local = G_LOCAL_FILE (file);
  1912. if (symlink (symlink_value, local->filename) == -1)
  1913. {
  1914. int errsv = errno;
  1915. if (errsv == EINVAL)
  1916. /* This must be an invalid filename, on e.g. FAT */
  1917. g_set_error_literal (error, G_IO_ERROR,
  1918. G_IO_ERROR_INVALID_FILENAME,
  1919. _("Invalid filename"));
  1920. else if (errsv == EPERM)
  1921. g_set_error (error, G_IO_ERROR,
  1922. G_IO_ERROR_NOT_SUPPORTED,
  1923. _("Filesystem does not support symbolic links"));
  1924. else
  1925. g_set_io_error (error,
  1926. _("Error making symbolic link %s: %s"),
  1927. file, errsv);
  1928. return FALSE;
  1929. }
  1930. return TRUE;
  1931. #else
  1932. g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, _("Symbolic links not supported"));
  1933. return FALSE;
  1934. #endif
  1935. }
  1936. static gboolean
  1937. g_local_file_copy (GFile *source,
  1938. GFile *destination,
  1939. GFileCopyFlags flags,
  1940. GCancellable *cancellable,
  1941. GFileProgressCallback progress_callback,
  1942. gpointer progress_callback_data,
  1943. GError **error)
  1944. {
  1945. /* Fall back to default copy */
  1946. g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Copy not supported");
  1947. return FALSE;
  1948. }
  1949. static gboolean
  1950. g_local_file_move (GFile *source,
  1951. GFile *destination,
  1952. GFileCopyFlags flags,
  1953. GCancellable *cancellable,
  1954. GFileProgressCallback progress_callback,
  1955. gpointer progress_callback_data,
  1956. GError **error)
  1957. {
  1958. GLocalFile *local_source, *local_destination;
  1959. GStatBuf statbuf;
  1960. gboolean destination_exist, source_is_dir;
  1961. char *backup_name;
  1962. int res;
  1963. off_t source_size;
  1964. GVfsClass *class;
  1965. GVfs *vfs;
  1966. if (!G_IS_LOCAL_FILE (source) ||
  1967. !G_IS_LOCAL_FILE (destination))
  1968. {
  1969. /* Fall back to default move */
  1970. g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Move not supported");
  1971. return FALSE;
  1972. }
  1973. local_source = G_LOCAL_FILE (source);
  1974. local_destination = G_LOCAL_FILE (destination);
  1975. res = g_lstat (local_source->filename, &statbuf);
  1976. if (res == -1)
  1977. {
  1978. int errsv = errno;
  1979. g_set_io_error (error,
  1980. _("Error moving file %s: %s"),
  1981. source, errsv);
  1982. return FALSE;
  1983. }
  1984. source_is_dir = S_ISDIR (statbuf.st_mode);
  1985. source_size = statbuf.st_size;
  1986. destination_exist = FALSE;
  1987. res = g_lstat (local_destination->filename, &statbuf);
  1988. if (res == 0)
  1989. {
  1990. destination_exist = TRUE; /* Target file exists */
  1991. if (flags & G_FILE_COPY_OVERWRITE)
  1992. {
  1993. /* Always fail on dirs, even with overwrite */
  1994. if (S_ISDIR (statbuf.st_mode))
  1995. {
  1996. if (source_is_dir)
  1997. g_set_error_literal (error,
  1998. G_IO_ERROR,
  1999. G_IO_ERROR_WOULD_MERGE,
  2000. _("Can’t move directory over directory"));
  2001. else
  2002. g_set_error_literal (error,
  2003. G_IO_ERROR,
  2004. G_IO_ERROR_IS_DIRECTORY,
  2005. _("Can’t copy over directory"));
  2006. return FALSE;
  2007. }
  2008. }
  2009. else
  2010. {
  2011. g_set_io_error (error,
  2012. _("Error moving file %s: %s"),
  2013. source, EEXIST);
  2014. return FALSE;
  2015. }
  2016. }
  2017. if (flags & G_FILE_COPY_BACKUP && destination_exist)
  2018. {
  2019. backup_name = g_strconcat (local_destination->filename, "~", NULL);
  2020. if (g_rename (local_destination->filename, backup_name) == -1)
  2021. {
  2022. g_set_error_literal (error,
  2023. G_IO_ERROR,
  2024. G_IO_ERROR_CANT_CREATE_BACKUP,
  2025. _("Backup file creation failed"));
  2026. g_free (backup_name);
  2027. return FALSE;
  2028. }
  2029. g_free (backup_name);
  2030. destination_exist = FALSE; /* It did, but no more */
  2031. }
  2032. if (source_is_dir && destination_exist && (flags & G_FILE_COPY_OVERWRITE))
  2033. {
  2034. /* Source is a dir, destination exists (and is not a dir, because that would have failed
  2035. earlier), and we're overwriting. Manually remove the target so we can do the rename. */
  2036. res = g_unlink (local_destination->filename);
  2037. if (res == -1)
  2038. {
  2039. int errsv = errno;
  2040. g_set_error (error, G_IO_ERROR,
  2041. g_io_error_from_errno (errsv),
  2042. _("Error removing target file: %s"),
  2043. g_strerror (errsv));
  2044. return FALSE;
  2045. }
  2046. }
  2047. if (g_rename (local_source->filename, local_destination->filename) == -1)
  2048. {
  2049. int errsv = errno;
  2050. if (errsv == EXDEV)
  2051. /* This will cause the fallback code to run */
  2052. g_set_error_literal (error, G_IO_ERROR,
  2053. G_IO_ERROR_NOT_SUPPORTED,
  2054. _("Move between mounts not supported"));
  2055. else if (errsv == EINVAL)
  2056. /* This must be an invalid filename, on e.g. FAT, or
  2057. we're trying to move the file into itself...
  2058. We return invalid filename for both... */
  2059. g_set_error_literal (error, G_IO_ERROR,
  2060. G_IO_ERROR_INVALID_FILENAME,
  2061. _("Invalid filename"));
  2062. else
  2063. g_set_io_error (error,
  2064. _("Error moving file %s: %s"),
  2065. source, errsv);
  2066. return FALSE;
  2067. }
  2068. vfs = g_vfs_get_default ();
  2069. class = G_VFS_GET_CLASS (vfs);
  2070. if (class->local_file_moved)
  2071. class->local_file_moved (vfs, local_source->filename, local_destination->filename);
  2072. /* Make sure we send full copied size */
  2073. if (progress_callback)
  2074. progress_callback (source_size, source_size, progress_callback_data);
  2075. return TRUE;
  2076. }
  2077. #ifdef G_OS_WIN32
  2078. gboolean
  2079. g_local_file_is_remote (const gchar *filename)
  2080. {
  2081. return FALSE;
  2082. }
  2083. #else
  2084. static gboolean
  2085. is_remote_fs (const gchar *filename)
  2086. {
  2087. const char *fsname = NULL;
  2088. #ifdef USE_STATFS
  2089. struct statfs statfs_buffer;
  2090. int statfs_result = 0;
  2091. #if STATFS_ARGS == 2
  2092. statfs_result = statfs (filename, &statfs_buffer);
  2093. #elif STATFS_ARGS == 4
  2094. statfs_result = statfs (filename, &statfs_buffer, sizeof (statfs_buffer), 0);
  2095. #endif
  2096. #elif defined(USE_STATVFS)
  2097. struct statvfs statfs_buffer;
  2098. int statfs_result = 0;
  2099. statfs_result = statvfs (filename, &statfs_buffer);
  2100. #else
  2101. return FALSE;
  2102. #endif
  2103. if (statfs_result == -1)
  2104. return FALSE;
  2105. #ifdef USE_STATFS
  2106. #if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
  2107. fsname = statfs_buffer.f_fstypename;
  2108. #else
  2109. fsname = get_fs_type (statfs_buffer.f_type);
  2110. #endif
  2111. #elif defined(USE_STATVFS) && defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
  2112. fsname = statfs_buffer.f_basetype;
  2113. #endif
  2114. if (fsname != NULL)
  2115. {
  2116. if (strcmp (fsname, "nfs") == 0)
  2117. return TRUE;
  2118. if (strcmp (fsname, "nfs4") == 0)
  2119. return TRUE;
  2120. }
  2121. return FALSE;
  2122. }
  2123. gboolean
  2124. g_local_file_is_remote (const gchar *filename)
  2125. {
  2126. static gboolean remote_home;
  2127. static gsize initialized;
  2128. const gchar *home;
  2129. home = g_get_home_dir ();
  2130. if (path_has_prefix (filename, home))
  2131. {
  2132. if (g_once_init_enter (&initialized))
  2133. {
  2134. remote_home = is_remote_fs (home);
  2135. g_once_init_leave (&initialized, TRUE);
  2136. }
  2137. return remote_home;
  2138. }
  2139. return FALSE;
  2140. }
  2141. #endif /* !G_OS_WIN32 */
  2142. static GFileMonitor*
  2143. g_local_file_monitor_dir (GFile *file,
  2144. GFileMonitorFlags flags,
  2145. GCancellable *cancellable,
  2146. GError **error)
  2147. {
  2148. GLocalFile *local_file = G_LOCAL_FILE (file);
  2149. return g_local_file_monitor_new_for_path (local_file->filename, TRUE, flags, error);
  2150. }
  2151. static GFileMonitor*
  2152. g_local_file_monitor_file (GFile *file,
  2153. GFileMonitorFlags flags,
  2154. GCancellable *cancellable,
  2155. GError **error)
  2156. {
  2157. GLocalFile *local_file = G_LOCAL_FILE (file);
  2158. return g_local_file_monitor_new_for_path (local_file->filename, FALSE, flags, error);
  2159. }
  2160. /* Here is the GLocalFile implementation of g_file_measure_disk_usage().
  2161. *
  2162. * If available, we use fopenat() in preference to filenames for
  2163. * efficiency and safety reasons. We know that fopenat() is available
  2164. * based on if AT_FDCWD is defined. POSIX guarantees that this will be
  2165. * defined as a macro.
  2166. *
  2167. * We use a linked list of stack-allocated GSList nodes in order to be
  2168. * able to reconstruct the filename for error messages. We actually
  2169. * pass the filename to operate on through the top node of the list.
  2170. *
  2171. * In case we're using openat(), this top filename will be a basename
  2172. * which should be opened in the directory which has also had its fd
  2173. * passed along. If we're not using openat() then it will be a full
  2174. * absolute filename.
  2175. */
  2176. static gboolean
  2177. g_local_file_measure_size_error (GFileMeasureFlags flags,
  2178. gint saved_errno,
  2179. GSList *name,
  2180. GError **error)
  2181. {
  2182. /* Only report an error if we were at the toplevel or if the caller
  2183. * requested reporting of all errors.
  2184. */
  2185. if ((name->next == NULL) || (flags & G_FILE_MEASURE_REPORT_ANY_ERROR))
  2186. {
  2187. GString *filename;
  2188. GSList *node;
  2189. /* Skip some work if there is no error return */
  2190. if (!error)
  2191. return FALSE;
  2192. #ifdef AT_FDCWD
  2193. /* If using openat() we need to rebuild the filename for the message */
  2194. filename = g_string_new (name->data);
  2195. for (node = name->next; node; node = node->next)
  2196. {
  2197. gchar *utf8;
  2198. g_string_prepend_c (filename, G_DIR_SEPARATOR);
  2199. utf8 = g_filename_display_name (node->data);
  2200. g_string_prepend (filename, utf8);
  2201. g_free (utf8);
  2202. }
  2203. #else
  2204. {
  2205. gchar *utf8;
  2206. /* Otherwise, we already have it, so just use it. */
  2207. node = name;
  2208. filename = g_string_new (NULL);
  2209. utf8 = g_filename_display_name (node->data);
  2210. g_string_append (filename, utf8);
  2211. g_free (utf8);
  2212. }
  2213. #endif
  2214. g_set_error (error, G_IO_ERROR, g_io_error_from_errno (saved_errno),
  2215. _("Could not determine the disk usage of %s: %s"),
  2216. filename->str, g_strerror (saved_errno));
  2217. g_string_free (filename, TRUE);
  2218. return FALSE;
  2219. }
  2220. else
  2221. /* We're not reporting this error... */
  2222. return TRUE;
  2223. }
  2224. typedef struct
  2225. {
  2226. GFileMeasureFlags flags;
  2227. dev_t contained_on;
  2228. GCancellable *cancellable;
  2229. GFileMeasureProgressCallback progress_callback;
  2230. gpointer progress_data;
  2231. guint64 disk_usage;
  2232. guint64 num_dirs;
  2233. guint64 num_files;
  2234. guint64 last_progress_report;
  2235. } MeasureState;
  2236. static gboolean
  2237. g_local_file_measure_size_of_contents (gint fd,
  2238. GSList *dir_name,
  2239. MeasureState *state,
  2240. GError **error);
  2241. static gboolean
  2242. g_local_file_measure_size_of_file (gint parent_fd,
  2243. GSList *name,
  2244. MeasureState *state,
  2245. GError **error)
  2246. {
  2247. GLocalFileStat buf;
  2248. if (g_cancellable_set_error_if_cancelled (state->cancellable, error))
  2249. return FALSE;
  2250. #if defined (AT_FDCWD)
  2251. if (fstatat (parent_fd, name->data, &buf, AT_SYMLINK_NOFOLLOW) != 0)
  2252. {
  2253. int errsv = errno;
  2254. return g_local_file_measure_size_error (state->flags, errsv, name, error);
  2255. }
  2256. #elif defined (HAVE_LSTAT) || !defined (G_OS_WIN32)
  2257. if (g_lstat (name->data, &buf) != 0)
  2258. {
  2259. int errsv = errno;
  2260. return g_local_file_measure_size_error (state->flags, errsv, name, error);
  2261. }
  2262. #else
  2263. {
  2264. const char *filename = (const gchar *) name->data;
  2265. wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);
  2266. int retval;
  2267. int save_errno;
  2268. int len;
  2269. if (wfilename == NULL)
  2270. return g_local_file_measure_size_error (state->flags, errno, name, error);
  2271. len = wcslen (wfilename);
  2272. while (len > 0 && G_IS_DIR_SEPARATOR (wfilename[len-1]))
  2273. len--;
  2274. if (len > 0 &&
  2275. (!g_path_is_absolute (filename) || len > g_path_skip_root (filename) - filename))
  2276. wfilename[len] = '\0';
  2277. retval = _wstati64 (wfilename, &buf);
  2278. save_errno = errno;
  2279. g_free (wfilename);
  2280. errno = save_errno;
  2281. if (retval != 0)
  2282. return g_local_file_measure_size_error (state->flags, errno, name, error);
  2283. }
  2284. #endif
  2285. if (name->next)
  2286. {
  2287. /* If not at the toplevel, check for a device boundary. */
  2288. if (state->flags & G_FILE_MEASURE_NO_XDEV)
  2289. if (state->contained_on != buf.st_dev)
  2290. return TRUE;
  2291. }
  2292. else
  2293. {
  2294. /* If, however, this is the toplevel, set the device number so
  2295. * that recursive invocations can compare against it.
  2296. */
  2297. state->contained_on = buf.st_dev;
  2298. }
  2299. #if defined (HAVE_STRUCT_STAT_ST_BLOCKS)
  2300. if (~state->flags & G_FILE_MEASURE_APPARENT_SIZE)
  2301. state->disk_usage += buf.st_blocks * G_GUINT64_CONSTANT (512);
  2302. else
  2303. #endif
  2304. state->disk_usage += buf.st_size;
  2305. if (S_ISDIR (buf.st_mode))
  2306. state->num_dirs++;
  2307. else
  2308. state->num_files++;
  2309. if (state->progress_callback)
  2310. {
  2311. /* We could attempt to do some cleverness here in order to avoid
  2312. * calling clock_gettime() so much, but we're doing stats and opens
  2313. * all over the place already...
  2314. */
  2315. if (state->last_progress_report)
  2316. {
  2317. guint64 now;
  2318. now = g_get_monotonic_time ();
  2319. if (state->last_progress_report + 200 * G_TIME_SPAN_MILLISECOND < now)
  2320. {
  2321. (* state->progress_callback) (TRUE,
  2322. state->disk_usage, state->num_dirs, state->num_files,
  2323. state->progress_data);
  2324. state->last_progress_report = now;
  2325. }
  2326. }
  2327. else
  2328. {
  2329. /* We must do an initial report to inform that more reports
  2330. * will be coming.
  2331. */
  2332. (* state->progress_callback) (TRUE, 0, 0, 0, state->progress_data);
  2333. state->last_progress_report = g_get_monotonic_time ();
  2334. }
  2335. }
  2336. if (S_ISDIR (buf.st_mode))
  2337. {
  2338. int dir_fd = -1;
  2339. int errsv;
  2340. if (g_cancellable_set_error_if_cancelled (state->cancellable, error))
  2341. return FALSE;
  2342. #ifdef AT_FDCWD
  2343. #ifdef HAVE_OPEN_O_DIRECTORY
  2344. dir_fd = openat (parent_fd, name->data, O_RDONLY|O_DIRECTORY);
  2345. #else
  2346. dir_fd = openat (parent_fd, name->data, O_RDONLY);
  2347. #endif
  2348. errsv = errno;
  2349. if (dir_fd < 0)
  2350. return g_local_file_measure_size_error (state->flags, errsv, name, error);
  2351. #endif
  2352. if (!g_local_file_measure_size_of_contents (dir_fd, name, state, error))
  2353. return FALSE;
  2354. }
  2355. return TRUE;
  2356. }
  2357. static gboolean
  2358. g_local_file_measure_size_of_contents (gint fd,
  2359. GSList *dir_name,
  2360. MeasureState *state,
  2361. GError **error)
  2362. {
  2363. gboolean success = TRUE;
  2364. const gchar *name;
  2365. GDir *dir;
  2366. #ifdef AT_FDCWD
  2367. {
  2368. /* If this fails, we want to preserve the errno from fopendir() */
  2369. DIR *dirp;
  2370. dirp = fdopendir (fd);
  2371. dir = dirp ? GLIB_PRIVATE_CALL(g_dir_new_from_dirp) (dirp) : NULL;
  2372. }
  2373. #else
  2374. dir = GLIB_PRIVATE_CALL(g_dir_open_with_errno) (dir_name->data, 0);
  2375. #endif
  2376. if (dir == NULL)
  2377. {
  2378. gint saved_errno = errno;
  2379. #ifdef AT_FDCWD
  2380. close (fd);
  2381. #endif
  2382. return g_local_file_measure_size_error (state->flags, saved_errno, dir_name, error);
  2383. }
  2384. while (success && (name = g_dir_read_name (dir)))
  2385. {
  2386. GSList node;
  2387. node.next = dir_name;
  2388. #ifdef AT_FDCWD
  2389. node.data = (gchar *) name;
  2390. #else
  2391. node.data = g_build_filename (dir_name->data, name, NULL);
  2392. #endif
  2393. success = g_local_file_measure_size_of_file (fd, &node, state, error);
  2394. #ifndef AT_FDCWD
  2395. g_free (node.data);
  2396. #endif
  2397. }
  2398. g_dir_close (dir);
  2399. return success;
  2400. }
  2401. static gboolean
  2402. g_local_file_measure_disk_usage (GFile *file,
  2403. GFileMeasureFlags flags,
  2404. GCancellable *cancellable,
  2405. GFileMeasureProgressCallback progress_callback,
  2406. gpointer progress_data,
  2407. guint64 *disk_usage,
  2408. guint64 *num_dirs,
  2409. guint64 *num_files,
  2410. GError **error)
  2411. {
  2412. GLocalFile *local_file = G_LOCAL_FILE (file);
  2413. MeasureState state = { 0, };
  2414. gint root_fd = -1;
  2415. GSList node;
  2416. state.flags = flags;
  2417. state.cancellable = cancellable;
  2418. state.progress_callback = progress_callback;
  2419. state.progress_data = progress_data;
  2420. #ifdef AT_FDCWD
  2421. root_fd = AT_FDCWD;
  2422. #endif
  2423. node.data = local_file->filename;
  2424. node.next = NULL;
  2425. if (!g_local_file_measure_size_of_file (root_fd, &node, &state, error))
  2426. return FALSE;
  2427. if (disk_usage)
  2428. *disk_usage = state.disk_usage;
  2429. if (num_dirs)
  2430. *num_dirs = state.num_dirs;
  2431. if (num_files)
  2432. *num_files = state.num_files;
  2433. return TRUE;
  2434. }
  2435. static void
  2436. g_local_file_file_iface_init (GFileIface *iface)
  2437. {
  2438. iface->dup = g_local_file_dup;
  2439. iface->hash = g_local_file_hash;
  2440. iface->equal = g_local_file_equal;
  2441. iface->is_native = g_local_file_is_native;
  2442. iface->has_uri_scheme = g_local_file_has_uri_scheme;
  2443. iface->get_uri_scheme = g_local_file_get_uri_scheme;
  2444. iface->get_basename = g_local_file_get_basename;
  2445. iface->get_path = g_local_file_get_path;
  2446. iface->get_uri = g_local_file_get_uri;
  2447. iface->get_parse_name = g_local_file_get_parse_name;
  2448. iface->get_parent = g_local_file_get_parent;
  2449. iface->prefix_matches = g_local_file_prefix_matches;
  2450. iface->get_relative_path = g_local_file_get_relative_path;
  2451. iface->resolve_relative_path = g_local_file_resolve_relative_path;
  2452. iface->get_child_for_display_name = g_local_file_get_child_for_display_name;
  2453. iface->set_display_name = g_local_file_set_display_name;
  2454. iface->enumerate_children = g_local_file_enumerate_children;
  2455. iface->query_info = g_local_file_query_info;
  2456. iface->query_filesystem_info = g_local_file_query_filesystem_info;
  2457. iface->find_enclosing_mount = g_local_file_find_enclosing_mount;
  2458. iface->query_settable_attributes = g_local_file_query_settable_attributes;
  2459. iface->query_writable_namespaces = g_local_file_query_writable_namespaces;
  2460. iface->set_attribute = g_local_file_set_attribute;
  2461. iface->set_attributes_from_info = g_local_file_set_attributes_from_info;
  2462. iface->read_fn = g_local_file_read;
  2463. iface->append_to = g_local_file_append_to;
  2464. iface->create = g_local_file_create;
  2465. iface->replace = g_local_file_replace;
  2466. iface->open_readwrite = g_local_file_open_readwrite;
  2467. iface->create_readwrite = g_local_file_create_readwrite;
  2468. iface->replace_readwrite = g_local_file_replace_readwrite;
  2469. iface->delete_file = g_local_file_delete;
  2470. iface->trash = g_local_file_trash;
  2471. iface->make_directory = g_local_file_make_directory;
  2472. iface->make_symbolic_link = g_local_file_make_symbolic_link;
  2473. iface->copy = g_local_file_copy;
  2474. iface->move = g_local_file_move;
  2475. iface->monitor_dir = g_local_file_monitor_dir;
  2476. iface->monitor_file = g_local_file_monitor_file;
  2477. iface->measure_disk_usage = g_local_file_measure_disk_usage;
  2478. iface->supports_thread_contexts = TRUE;
  2479. }