File: bolt-fs.c

package info (click to toggle)
bolt 0.7-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,396 kB
  • sloc: ansic: 21,784; python: 1,574; xml: 426; sh: 25; makefile: 13
file content (158 lines) | stat: -rw-r--r-- 3,699 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Copyright © 2017 Red Hat, Inc
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *       Christian J. Kellner <christian@kellner.me>
 */

#include "config.h"

#include "bolt-error.h"
#include "bolt-io.h"
#include "bolt-fs.h"

#include <dirent.h>
#include <errno.h>

gboolean
bolt_fs_make_parent_dirs (GFile   *target,
                          GError **error)
{
  g_autoptr(GError) err = NULL;
  g_autoptr(GFile) p = NULL;
  gboolean ok;

  g_return_val_if_fail (G_IS_FILE (target), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  p = g_file_get_parent (target);

  ok = g_file_make_directory_with_parents (p, NULL, &err);
  if (!ok && !bolt_err_exists (err))
    return bolt_error_propagate (error, &err);

  return TRUE;
}

static void
cleanup_dir (DIR *d)
{
  struct dirent *de = NULL;

  while ((de = readdir (d)) != NULL)
    {
      g_autoptr(GError) error = NULL;
      int uflag = 0;

      if (!g_strcmp0 (de->d_name, ".") ||
          !g_strcmp0 (de->d_name, ".."))
        continue;

      if (de->d_type == DT_DIR)
        {
          DIR *cd;

          cd = bolt_opendir_at (dirfd (d), de->d_name, O_RDONLY, &error);
          if (cd == NULL)
            continue;

          cleanup_dir (cd);
          uflag = AT_REMOVEDIR;
          closedir (cd);
        }

      bolt_unlink_at (dirfd (d), de->d_name, uflag, NULL);
    }
}

gboolean
bolt_fs_cleanup_dir (const char *target,
                     GError    **error)
{
  DIR *d = NULL;

  g_return_val_if_fail (target != NULL, FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  d = bolt_opendir (target, error);
  if (d == NULL)
    return FALSE;

  cleanup_dir (d);
  (void) bolt_closedir (d, NULL);

  return bolt_rmdir (target, error);
}

#define TOUCH_FLAGS O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC

static inline void
timespec_set_from_uint64 (struct timespec *to,
                          guint64          from)
{
  if (from > 0)
    {
      to->tv_sec = (time_t) from;
      to->tv_nsec = 0;
    }
  else
    {
      to->tv_sec = 0;
      to->tv_nsec = UTIME_OMIT;
    }
}

gboolean
bolt_fs_touch (GFile   *target,
               guint64  atime,
               guint64  mtime,
               GError **error)
{
  g_autofree char *path = NULL;
  struct timespec times[2];
  gboolean ok;
  int fd;
  int r;

  g_return_val_if_fail (target != NULL, FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  path = g_file_get_path (target);

  fd = bolt_open (path, TOUCH_FLAGS, 0664, error);

  if (fd < 0)
    return FALSE;

  /* times[0] is the "last access time" (atime) */
  timespec_set_from_uint64 (&times[0], atime);

  /* times[1] is the "last modification time" (mtime). */
  timespec_set_from_uint64 (&times[1], mtime);

  r = futimens (fd, times);

  if (r == -1)
    {
      int errsave = errno;
      g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsave),
                   "could not touch file: %s", g_strerror (errsave));
    }

  ok = bolt_close (fd, error);

  return r != -1 && ok;
}