]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - spaceman/file.c
libfrog: move crc32c.h to libfrog/
[thirdparty/xfsprogs-dev.git] / spaceman / file.c
CommitLineData
959ef981 1// SPDX-License-Identifier: GPL-2.0
48ec2905
DC
2/*
3 * Copyright (c) 2004-2005 Silicon Graphics, Inc.
4 * Copyright (c) 2012 Red Hat, Inc.
5 * All Rights Reserved.
48ec2905
DC
6 */
7
8#include "libxfs.h"
9#include <sys/mman.h>
10#include "command.h"
11#include "input.h"
12#include "init.h"
cccf6abc 13#include "path.h"
48ec2905 14#include "space.h"
fee68490 15#include "libfrog/fsgeom.h"
48ec2905
DC
16
17static cmdinfo_t print_cmd;
18
19fileio_t *filetable;
20int filecount;
21fileio_t *file;
22
23static void
24print_fileio(
25 fileio_t *file,
26 int index,
27 int braces)
28{
29 printf(_("%c%03d%c %-14s\n"), braces? '[' : ' ', index,
30 braces? ']' : ' ', file->name);
31}
32
33static int
34print_f(
35 int argc,
36 char **argv)
37{
38 int i;
39
40 for (i = 0; i < filecount; i++)
41 print_fileio(&filetable[i], i, &filetable[i] == file);
42 return 0;
43}
44
45int
46openfile(
47 char *path,
2668d323 48 struct xfs_fsop_geom *geom,
cccf6abc 49 struct fs_path *fs_path)
48ec2905 50{
cccf6abc 51 struct fs_path *fsp;
9612817d 52 int fd, ret;
48ec2905
DC
53
54 fd = open(path, 0);
55 if (fd < 0) {
56 perror(path);
57 return -1;
58 }
59
9612817d
DW
60 ret = xfrog_geometry(fd, geom);
61 if (ret) {
62 if (ret == ENOTTY)
6fb70877
DW
63 fprintf(stderr,
64_("%s: Not on a mounted XFS filesystem.\n"),
65 path);
9612817d
DW
66 else {
67 errno = ret;
6fb70877 68 perror("XFS_IOC_FSGEOMETRY");
9612817d 69 }
48ec2905
DC
70 close(fd);
71 return -1;
72 }
cccf6abc
DC
73
74 if (fs_path) {
75 fsp = fs_table_lookup(path, FS_MOUNT_POINT);
76 if (!fsp) {
77 fprintf(stderr, _("%s: cannot find mount point."),
78 path);
1cda9090 79 close(fd);
cccf6abc
DC
80 return -1;
81 }
82 memcpy(fs_path, fsp, sizeof(struct fs_path));
83 }
48ec2905
DC
84 return fd;
85}
86
87int
88addfile(
89 char *name,
90 int fd,
2668d323 91 struct xfs_fsop_geom *geometry,
cccf6abc 92 struct fs_path *fs_path)
48ec2905
DC
93{
94 char *filename;
95
96 filename = strdup(name);
97 if (!filename) {
98 perror("strdup");
99 close(fd);
100 return -1;
101 }
102
103 /* Extend the table of currently open files */
104 filetable = (fileio_t *)realloc(filetable, /* growing */
105 ++filecount * sizeof(fileio_t));
106 if (!filetable) {
107 perror("realloc");
108 filecount = 0;
109 free(filename);
110 close(fd);
111 return -1;
112 }
113
114 /* Finally, make this the new active open file */
115 file = &filetable[filecount - 1];
116 file->fd = fd;
117 file->name = filename;
118 file->geom = *geometry;
cccf6abc 119 memcpy(&file->fs_path, fs_path, sizeof(file->fs_path));
48ec2905
DC
120 return 0;
121}
122
123void
124print_init(void)
125{
126 print_cmd.name = "print";
127 print_cmd.altname = "p";
128 print_cmd.cfunc = print_f;
129 print_cmd.argmin = 0;
130 print_cmd.argmax = 0;
131 print_cmd.flags = CMD_FLAG_ONESHOT;
132 print_cmd.oneline = _("list current open files");
133
134 add_command(&print_cmd);
135}