Mercurial > hg > toybox
annotate toys/other/blockdev.c @ 1539:3e85af1f7e22 draft
First batch of sed tests.
Only good for TEST_HOST=1 at the moment because the test infrastructure itself
depends on sed, so if an unfinished sed is in the $PATH it goes boing. But
hey, corner cases!
I have... more.
author | Rob Landley <rob@landley.net> |
---|---|
date | Wed, 29 Oct 2014 18:44:33 -0500 |
parents | 8b7ddefcf28c |
children |
rev | line source |
---|---|
1434
08a34594b2c6
A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
1 /* blockdev.c -show/set blockdev information. |
08a34594b2c6
A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
2 * |
08a34594b2c6
A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
3 * Copyright 2014 Sameer Prakash Pradhan <sameer.p.pradhan@gmail.com> |
08a34594b2c6
A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
4 * |
08a34594b2c6
A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
5 * No Standard. |
08a34594b2c6
A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
6 |
08a34594b2c6
A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
7 USE_BLOCKDEV(NEWTOY(blockdev, "<1>1(setro)(setrw)(getro)(getss)(getbsz)(setbsz)#<0(getsz)(getsize)(getsize64)(flushbufs)(rereadpt)",TOYFLAG_USR|TOYFLAG_BIN)) |
08a34594b2c6
A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
8 |
08a34594b2c6
A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
9 config BLOCKDEV |
08a34594b2c6
A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
10 bool "blockdev" |
1445 | 11 default y |
1434
08a34594b2c6
A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
12 help |
1444 | 13 usage: blockdev --OPTION... BLOCKDEV... |
14 | |
15 Call ioctl(s) on each listed block device | |
16 | |
17 OPTIONs: | |
18 --setro Set read only | |
19 --setrw Set read write | |
20 --getro Get read only | |
21 --getss Get sector size | |
22 --getbsz Get block size | |
23 --setbsz BYTES Set block size | |
24 --getsz Get device size in 512-byte sectors | |
25 --getsize Get device size in sectors (deprecated) | |
26 --getsize64 Get device size in bytes | |
27 --flushbufs Flush buffers | |
28 --rereadpt Reread partition table | |
1434
08a34594b2c6
A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
29 */ |
08a34594b2c6
A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
30 |
08a34594b2c6
A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
31 #define FOR_blockdev |
08a34594b2c6
A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
32 #include "toys.h" |
08a34594b2c6
A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
33 #include <linux/fs.h> |
08a34594b2c6
A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
34 |
08a34594b2c6
A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
35 GLOBALS( |
08a34594b2c6
A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
36 long bsz; |
08a34594b2c6
A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
37 ) |
08a34594b2c6
A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
38 |
08a34594b2c6
A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
39 void blockdev_main(void) |
08a34594b2c6
A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
40 { |
1444 | 41 int cmds[] = {BLKRRPART, BLKFLSBUF, BLKGETSIZE64, BLKGETSIZE, BLKGETSIZE64, |
42 BLKBSZSET, BLKBSZGET, BLKSSZGET, BLKROGET, BLKROSET, BLKROSET}; | |
43 char **ss; | |
1434
08a34594b2c6
A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
44 long long val = 0; |
1444 | 45 |
46 if (!toys.optflags) { | |
47 toys.exithelp = 1; | |
48 error_exit("need --option"); | |
49 } | |
50 | |
51 for (ss = toys.optargs; *ss; ss++) { | |
52 int fd = xopen(*ss, O_RDONLY), i; | |
1434
08a34594b2c6
A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
53 |
1444 | 54 // Command line order discarded so perform multiple operations in flag order |
55 for (i = 0; i < 32; i++) { | |
56 long flag = toys.optflags & (1<<i); | |
57 | |
58 if (!flag) continue; | |
59 | |
60 if (flag & FLAG_setbsz) val = TT.bsz; | |
61 else val = !!(flag & FLAG_setro); | |
62 | |
63 xioctl(fd, cmds[i], &val); | |
64 | |
65 flag &= FLAG_setbsz|FLAG_setro|FLAG_flushbufs|FLAG_rereadpt|FLAG_setrw; | |
66 if (!flag) printf("%lld\n", (toys.optflags & FLAG_getsz) ? val >> 9: val); | |
67 } | |
68 xclose(fd); | |
1434
08a34594b2c6
A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
69 } |
08a34594b2c6
A toy _BLOCKDEV_ to get/set block device properties.
Ashwini Sharma <ak.ashwini1981@gmail.com>
parents:
diff
changeset
|
70 } |