PageRenderTime 80ms CodeModel.GetById 29ms 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

Large files files are truncated, but you can click here to view the full file

  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"),

Large files files are truncated, but you can click here to view the full file