~csurbhi/ubuntu/maverick/e2fsprogs/e2fsprogs.fix-505719

« back to all changes in this revision

Viewing changes to lib/ext2fs/getsize.c

  • Committer: Bazaar Package Importer
  • Author(s): Matt Zimmerman
  • Date: 2004-09-19 09:43:14 UTC
  • mto: (8.1.1 lenny) (1.2.3 upstream)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20040919094314-2tafd19i76fhu6ei
Tags: upstream-1.35
ImportĀ upstreamĀ versionĀ 1.35

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * getsize.c --- get the size of a partition.
3
3
 * 
4
4
 * Copyright (C) 1995, 1995 Theodore Ts'o.
 
5
 * Copyright (C) 2003 VMware, Inc.
5
6
 *
 
7
 * Windows version of ext2fs_get_device_size by Chris Li, VMware.
 
8
 * 
6
9
 * %Begin-Header%
7
10
 * This file may be redistributed under the terms of the GNU Public
8
11
 * License.
25
28
#include <linux/fd.h>
26
29
#endif
27
30
#ifdef HAVE_SYS_DISKLABEL_H
 
31
#include <sys/param.h> /* for __FreeBSD_version */
28
32
#include <sys/ioctl.h>
29
33
#include <sys/disklabel.h>
30
34
#endif /* HAVE_SYS_DISKLABEL_H */
 
35
#ifdef HAVE_SYS_DISK_H
 
36
#include <sys/queue.h> /* for LIST_HEAD */
 
37
#include <sys/disk.h>
 
38
#endif /* HAVE_SYS_DISK_H */
31
39
 
32
40
#if defined(__linux__) && defined(_IO) && !defined(BLKGETSIZE)
33
41
#define BLKGETSIZE _IO(0x12,96) /* return device size */
34
42
#endif
35
43
 
 
44
#ifdef APPLE_DARWIN
 
45
#include <sys/ioctl.h>
 
46
#include <sys/disk.h>
 
47
 
 
48
#define BLKGETSIZE DKIOCGETBLOCKCOUNT32
 
49
#endif /* APPLE_DARWIN */
 
50
 
36
51
#include "ext2_fs.h"
37
52
#include "ext2fs.h"
38
53
 
 
54
#if defined(__CYGWIN__) || defined (WIN32)
 
55
#include "windows.h"
 
56
#include "winioctl.h"
 
57
 
 
58
errcode_t ext2fs_get_device_size(const char *file, int blocksize,
 
59
                                 blk_t *retblocks)
 
60
{
 
61
        HANDLE dev;
 
62
        PARTITION_INFORMATION pi;
 
63
        DISK_GEOMETRY gi;
 
64
        DWORD retbytes;
 
65
        LARGE_INTEGER filesize;
 
66
 
 
67
        dev = CreateFile(file, GENERIC_READ, 
 
68
                         FILE_SHARE_READ | FILE_SHARE_WRITE ,
 
69
                         NULL,  OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,  NULL); 
 
70
 
 
71
        if (dev == INVALID_HANDLE_VALUE)
 
72
                return EBADF;
 
73
        if (DeviceIoControl(dev, IOCTL_DISK_GET_PARTITION_INFO,
 
74
                            &pi, sizeof(PARTITION_INFORMATION),
 
75
                            &pi, sizeof(PARTITION_INFORMATION),
 
76
                            &retbytes, NULL)) {
 
77
 
 
78
                *retblocks = pi.PartitionLength.QuadPart / blocksize;
 
79
        
 
80
        } else if (DeviceIoControl(dev, IOCTL_DISK_GET_DRIVE_GEOMETRY,
 
81
                                &gi, sizeof(DISK_GEOMETRY),
 
82
                                &gi, sizeof(DISK_GEOMETRY),
 
83
                                &retbytes, NULL)) {
 
84
 
 
85
                *retblocks = gi.BytesPerSector *
 
86
                             gi.SectorsPerTrack *
 
87
                             gi.TracksPerCylinder *
 
88
                             gi.Cylinders.QuadPart / blocksize;
 
89
 
 
90
        } else if (GetFileSizeEx(dev, &filesize)) {
 
91
                *retblocks = filesize.QuadPart / blocksize;
 
92
        }
 
93
 
 
94
        CloseHandle(dev);
 
95
        return 0;
 
96
}
 
97
 
 
98
#else
 
99
 
39
100
static int valid_offset (int fd, ext2_loff_t offset)
40
101
{
41
102
        char ch;
91
152
        }
92
153
#endif
93
154
#ifdef HAVE_SYS_DISKLABEL_H
 
155
#if defined(__FreeBSD__) && __FreeBSD_version < 500040
 
156
        /* old disklabel interface */
94
157
        part = strlen(file) - 1;
95
158
        if (part >= 0) {
96
159
                ch = file[part];
109
172
                        return 0;
110
173
                }
111
174
        }
 
175
#else /* __FreeBSD_version < 500040 */
 
176
        {
 
177
            off_t ms;
 
178
            u_int bs;
 
179
            if (ioctl(fd, DIOCGMEDIASIZE, &ms) >= 0) {
 
180
                *retblocks = ms / blocksize;
 
181
                return 0;
 
182
            }
 
183
        }
 
184
#endif /* __FreeBSD_version < 500040 */
112
185
#endif /* HAVE_SYS_DISKLABEL_H */
113
186
 
114
187
        /*
134
207
        return 0;
135
208
}
136
209
 
 
210
#endif /* WIN32 */
 
211
 
137
212
#ifdef DEBUG
138
213
int main(int argc, char **argv)
139
214
{