File: unix_utils-c.c

package info (click to toggle)
libguestfs 1%3A1.44.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 118,932 kB
  • sloc: ansic: 458,017; ml: 51,424; sh: 13,191; java: 9,578; makefile: 7,931; cs: 6,328; haskell: 5,674; python: 3,871; perl: 3,528; erlang: 2,446; xml: 1,347; ruby: 350; pascal: 257; javascript: 157; lex: 135; yacc: 128; cpp: 10
file content (385 lines) | stat: -rw-r--r-- 9,989 bytes parent folder | download
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/* libguestfs OCaml tools common code
 * Copyright (C) 2009-2019 Red Hat Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <errno.h>
#include <sys/types.h>

#ifdef HAVE_SYS_STATFS_H
#include <sys/statfs.h>
#endif

#ifdef HAVE_SYS_STATVFS_H
#include <sys/statvfs.h>
#endif

#ifdef HAVE_SYS_VFS_H
#include <sys/vfs.h>
#endif

#if MAJOR_IN_MKDEV
#include <sys/mkdev.h>
#elif MAJOR_IN_SYSMACROS
#include <sys/sysmacros.h>
/* else it's in sys/types.h, included above */
#endif

#ifdef HAVE_LINUX_MAGIC_H
#include <linux/magic.h>
#endif

#ifdef HAVE_WINDOWS_H
#include <windows.h>
#endif

#ifdef HAVE_SYS_MOUNT_H
#include <sys/mount.h>
#endif

#include <caml/alloc.h>
#include <caml/fail.h>
#include <caml/memory.h>
#include <caml/mlvalues.h>
#include <caml/unixsupport.h>

extern value guestfs_int_mllib_dev_t_makedev (value majv, value minv);
extern value guestfs_int_mllib_dev_t_major (value devv);
extern value guestfs_int_mllib_dev_t_minor (value devv);
extern value guestfs_int_mllib_unsetenv (value strv);
extern int guestfs_int_mllib_exit (value rv) __attribute__((noreturn));
extern value guestfs_int_mllib_fnmatch (value patternv, value strv, value flagsv);
extern value guestfs_int_mllib_sync (value unitv);
extern value guestfs_int_mllib_fsync_file (value filenamev);
extern value guestfs_int_mllib_mkdtemp (value val_pattern);
extern value guestfs_int_mllib_realpath (value pathv);
extern value guestfs_int_mllib_statvfs_statvfs (value pathv);
extern value guestfs_int_mllib_statvfs_is_network_filesystem (value pathv);
extern value guestfs_int_mllib_sysconf_nr_processors_online (value unitv);

/* NB: This is a "noalloc" call. */
value
guestfs_int_mllib_dev_t_makedev (value majv, value minv)
{
  return Val_int (makedev (Int_val (majv), Int_val (minv)));
}

/* NB: This is a "noalloc" call. */
value
guestfs_int_mllib_dev_t_major (value devv)
{
  return Val_int (major (Int_val (devv)));
}

/* NB: This is a "noalloc" call. */
value
guestfs_int_mllib_dev_t_minor (value devv)
{
  return Val_int (minor (Int_val (devv)));
}

/* NB: This is a "noalloc" call. */
value
guestfs_int_mllib_unsetenv (value strv)
{
  unsetenv (String_val (strv));
  return Val_unit;
}

/* NB: This is a "noalloc" call. */
int
guestfs_int_mllib_exit (value rv)
{
  _exit (Int_val (rv));
}

/* NB: These flags must appear in the same order as unix_utils.ml */
static int flags[] = {
  FNM_NOESCAPE,
  FNM_PATHNAME,
  FNM_PERIOD,
  FNM_FILE_NAME,
  FNM_LEADING_DIR,
  FNM_CASEFOLD,
};

value
guestfs_int_mllib_fnmatch (value patternv, value strv, value flagsv)
{
  CAMLparam3 (patternv, strv, flagsv);
  int f = 0, r;

  /* Convert flags to bitmask. */
  while (flagsv != Val_int (0)) {
    f |= flags[Int_val (Field (flagsv, 0))];
    flagsv = Field (flagsv, 1);
  }

  r = fnmatch (String_val (patternv), String_val (strv), f);

  if (r == 0)
    CAMLreturn (Val_true);
  else if (r == FNM_NOMATCH)
    CAMLreturn (Val_false);
  else {
    /* XXX The fnmatch specification doesn't mention what errors can
     * be returned by fnmatch.  Assume they are errnos for now.
     */
    unix_error (errno, (char *) "fnmatch", patternv);
  }
}


/* NB: This is a "noalloc" call. */
value
guestfs_int_mllib_sync (value unitv)
{
  sync ();
  return Val_unit;
}

/* Flush all writes associated with the named file to the disk.
 *
 * Note the wording in the SUS definition:
 *
 * "The fsync() function forces all currently queued I/O operations
 * associated with the file indicated by file descriptor fildes to the
 * synchronised I/O completion state."
 *
 * http://pubs.opengroup.org/onlinepubs/007908775/xsh/fsync.html
 */
value
guestfs_int_mllib_fsync_file (value filenamev)
{
  CAMLparam1 (filenamev);
  const char *filename = String_val (filenamev);
  int fd, err;

  /* Note to do fsync you have to open for write. */
  fd = open (filename, O_RDWR);
  if (fd == -1)
    unix_error (errno, (char *) "open", filenamev);

  if (fsync (fd) == -1) {
    err = errno;
    close (fd);
    unix_error (err, (char *) "fsync", filenamev);
  }

  if (close (fd) == -1)
    unix_error (errno, (char *) "close", filenamev);

  CAMLreturn (Val_unit);
}

value
guestfs_int_mllib_mkdtemp (value val_pattern)
{
  CAMLparam1 (val_pattern);
  CAMLlocal1 (rv);
  char *pattern, *ret;

  pattern = strdup (String_val (val_pattern));
  if (pattern == NULL)
    unix_error (errno, (char *) "strdup", val_pattern);

  ret = mkdtemp (pattern);
  if (ret == NULL)
    unix_error (errno, (char *) "mkdtemp", val_pattern);

  rv = caml_copy_string (ret);
  free (pattern);

  CAMLreturn (rv);
}

value
guestfs_int_mllib_realpath (value pathv)
{
  CAMLparam1 (pathv);
  CAMLlocal1 (rv);
  char *r;

  r = realpath (String_val (pathv), NULL);
  if (r == NULL)
    unix_error (errno, (char *) "realpath", pathv);

  rv = caml_copy_string (r);
  free (r);
  CAMLreturn (rv);
}

value
guestfs_int_mllib_statvfs_statvfs (value pathv)
{
  CAMLparam1 (pathv);
  int64_t f_bsize;
  int64_t f_frsize;
  int64_t f_blocks;
  int64_t f_bfree;
  int64_t f_bavail;
  int64_t f_files;
  int64_t f_ffree;
  int64_t f_favail;
  int64_t f_fsid;
  int64_t f_flag;
  int64_t f_namemax;
  CAMLlocal2 (rv, v);

#ifdef HAVE_STATVFS
  struct statvfs buf;

  if (statvfs (String_val (pathv), &buf) == -1)
    unix_error (errno, (char *) "statvfs", pathv);

  f_bsize = buf.f_bsize;
  f_frsize = buf.f_frsize;
  f_blocks = buf.f_blocks;
  f_bfree = buf.f_bfree;
  f_bavail = buf.f_bavail;
  f_files = buf.f_files;
  f_ffree = buf.f_ffree;
  f_favail = buf.f_favail;
  f_fsid = buf.f_fsid;
  f_flag = buf.f_flag;
  f_namemax = buf.f_namemax;

#else /* !HAVE_STATVFS */
#  if WIN32
  ULONGLONG free_bytes_available; /* for user - similar to bavail */
  ULONGLONG total_number_of_bytes;
  ULONGLONG total_number_of_free_bytes; /* for everyone - bfree */

  if (!GetDiskFreeSpaceEx (String_val (pathv),
                           (PULARGE_INTEGER) &free_bytes_available,
                           (PULARGE_INTEGER) &total_number_of_bytes,
                           (PULARGE_INTEGER) &total_number_of_free_bytes))
    unix_error (EIO, (char *) "statvfs: GetDiskFreeSpaceEx", pathv);

  /* XXX I couldn't determine how to get block size.  MSDN has a
   * unhelpful hard-coded list here:
   *   http://support.microsoft.com/kb/140365
   * but this depends on the filesystem type, the size of the disk and
   * the version of Windows.  So this code assumes the disk is NTFS
   * and the version of Windows is >= Win2K.
   */
  if (total_number_of_bytes < UINT64_C (16) * 1024 * 1024 * 1024 * 1024)
    f_bsize = 4096;
  else if (total_number_of_bytes < UINT64_C (32) * 1024 * 1024 * 1024 * 1024)
    f_bsize = 8192;
  else if (total_number_of_bytes < UINT64_C (64) * 1024 * 1024 * 1024 * 1024)
    f_bsize = 16384;
  else if (total_number_of_bytes < UINT64_C (128) * 1024 * 1024 * 1024 * 1024)
    f_bsize = 32768;
  else
    f_bsize = 65536;

  /* As with stat, -1 indicates a field is not known. */
  f_frsize = ret->bsize;
  f_blocks = total_number_of_bytes / ret->bsize;
  f_bfree = total_number_of_free_bytes / ret->bsize;
  f_bavail = free_bytes_available / ret->bsize;
  f_files = -1;
  f_ffree = -1;
  f_favail = -1;
  f_fsid = -1;
  f_flag = -1;
  f_namemax = FILENAME_MAX;

#  else /* !WIN32 */
#error "no statvfs or equivalent function available"
#  endif /* !WIN32 */
#endif /* !HAVE_STATVFS */

  /* Construct the return struct. */
  rv = caml_alloc (11, 0);
  v = caml_copy_int64 (f_bsize);
  Store_field (rv, 0, v);
  v = caml_copy_int64 (f_frsize);
  Store_field (rv, 1, v);
  v = caml_copy_int64 (f_blocks);
  Store_field (rv, 2, v);
  v = caml_copy_int64 (f_bfree);
  Store_field (rv, 3, v);
  v = caml_copy_int64 (f_bavail);
  Store_field (rv, 4, v);
  v = caml_copy_int64 (f_files);
  Store_field (rv, 5, v);
  v = caml_copy_int64 (f_ffree);
  Store_field (rv, 6, v);
  v = caml_copy_int64 (f_favail);
  Store_field (rv, 7, v);
  v = caml_copy_int64 (f_fsid);
  Store_field (rv, 8, v);
  v = caml_copy_int64 (f_flag);
  Store_field (rv, 9, v);
  v = caml_copy_int64 (f_namemax);
  Store_field (rv, 10, v);

  CAMLreturn (rv);
}

/* NB: This is a "noalloc" call. */
value
guestfs_int_mllib_statvfs_is_network_filesystem (value pathv)
{
#ifdef HAVE_STATFS
  struct statfs buf;

  if (statfs (String_val (pathv), &buf) == -1)
    unix_error (errno, (char *) "statvfs", pathv);

  /* Some but not all of these are defined in <linux/magic.h>. */
#ifndef CIFS_MAGIC_NUMBER
#define CIFS_MAGIC_NUMBER 0xff534d42
#endif
#ifndef NFS_SUPER_MAGIC
#define NFS_SUPER_MAGIC 0x6969
#endif
#ifndef SMB_SUPER_MAGIC
#define SMB_SUPER_MAGIC 0x517b
#endif

  return Val_bool ((unsigned int) buf.f_type == CIFS_MAGIC_NUMBER ||
                   (unsigned int) buf.f_type == NFS_SUPER_MAGIC ||
                   (unsigned int) buf.f_type == SMB_SUPER_MAGIC);
#else
  return Val_bool (0);
#endif
}

/* NB: This is a "noalloc" call. */
value
guestfs_int_mllib_sysconf_nr_processors_online (value unitv)
{
#ifdef _SC_NPROCESSORS_ONLN
  long n;

  n = sysconf (_SC_NPROCESSORS_ONLN);
  if (n > 0) return Val_int (n);
#endif
  /* Return a safe value so that callers don't need to deal with errors. */
  return Val_int (1);
}