blob: 16e1c095e90180804b9262983b072c15cdbab870 [file] [log] [blame]
Szymon Starzyckie160f812013-07-24 17:08:04 -07001/*
2 * Copyright (c) 2009-2013, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google, Inc. nor the names of its contributors
15 * may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31#include <sys/types.h>
32#include <sys/stat.h>
33#include <unistd.h>
34#include <stdio.h>
35#include <sys/ioctl.h>
36#include <linux/fs.h>
37
38#include "utils.h"
39#include "debug.h"
40
41#ifndef BLKDISCARD
42#define BLKDISCARD _IO(0x12,119)
43#endif
44
45#ifndef BLKSECDISCARD
46#define BLKSECDISCARD _IO(0x12,125)
47#endif
48
49
50int get_stream_size(FILE *stream) {
51 int size;
52 fseek(stream, 0, SEEK_END);
53 size = ftell(stream);
54 fseek(stream, 0, SEEK_SET);
55 return size;
56}
57
58uint64_t get_block_device_size(int fd)
59{
60 uint64_t size = 0;
61 int ret;
62
63 ret = ioctl(fd, BLKGETSIZE64, &size);
64
65 if (ret)
66 return 0;
67
68 return size;
69}
70
71uint64_t get_file_size(int fd)
72{
73 struct stat buf;
74 int ret;
75 int64_t computed_size;
76
77 ret = fstat(fd, &buf);
78 if (ret)
79 return 0;
80
81 if (S_ISREG(buf.st_mode))
82 computed_size = buf.st_size;
83 else if (S_ISBLK(buf.st_mode))
84 computed_size = get_block_device_size(fd);
85 else
86 computed_size = 0;
87
88 return computed_size;
89}
90
91uint64_t get_file_size64(int fd)
92{
93 struct stat64 buf;
94 int ret;
95 uint64_t computed_size;
96
97 ret = fstat64(fd, &buf);
98 if (ret)
99 return 0;
100
101 if (S_ISREG(buf.st_mode))
102 computed_size = buf.st_size;
103 else if (S_ISBLK(buf.st_mode))
104 computed_size = get_block_device_size(fd);
105 else
106 computed_size = 0;
107
108 return computed_size;
109}
110
111
112char *strip(char *str)
113{
114 int n;
115
116 n = strspn(str, " \t");
117 str += n;
118 n = strcspn(str, " \t");
119 str[n] = '\0';
120
121 return str;
122}
123
124int wipe_block_device(int fd, int64_t len)
125{
126 uint64_t range[2];
127 int ret;
128
129 range[0] = 0;
130 range[1] = len;
131 ret = ioctl(fd, BLKSECDISCARD, &range);
132 if (ret < 0) {
133 range[0] = 0;
134 range[1] = len;
135 ret = ioctl(fd, BLKDISCARD, &range);
136 if (ret < 0) {
137 D(WARN, "Discard failed\n");
138 return 1;
139 } else {
140 D(WARN, "Wipe via secure discard failed, used discard instead\n");
141 return 0;
142 }
143 }
144
145 return 0;
146}
147