source: calamares/trunk/fuentes/src/modules/welcome/checker/partman_devices.c @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 13 months ago

sync with github

File size: 3.5 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
4 *
5 *   Based on parted_devices.c, from partman-base.
6 *   <http://anonscm.debian.org/cgit/d-i/partman-base.git>
7 *
8 *   Calamares is free software: you can redistribute it and/or modify
9 *   it under the terms of the GNU General Public License as published by
10 *   the Free Software Foundation, either version 3 of the License, or
11 *   (at your option) any later version.
12 *
13 *   Calamares is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include "partman_devices.h"
23
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <fcntl.h>
27#include <sys/ioctl.h>
28#include <unistd.h>
29#include <string.h>
30
31#ifdef __linux__
32/* from <linux/cdrom.h> */
33#define CDROM_GET_CAPABILITY    0x5331  /* get capabilities */
34#endif /* __linux__ */
35
36#ifdef __FreeBSD_kernel__
37#include <sys/cdio.h>
38#include <errno.h>
39#endif
40
41#include <parted/parted.h>
42
43#if defined(__linux__)
44static int
45is_cdrom(const char *path)
46{
47    int fd = -1;
48    int ret = -1;
49
50    fd = open(path, O_RDONLY | O_NONBLOCK);
51    if (fd >= 0)
52    {
53        ret = ioctl(fd, CDROM_GET_CAPABILITY, NULL);
54        close(fd);
55    }
56
57    if (ret >= 0)
58        return 1;
59    else
60        return 0;
61}
62#elif defined(__FreeBSD_kernel__) /* !__linux__ */
63static int
64is_cdrom(const char *path)
65{
66    int fd;
67
68    fd = open(path, O_RDONLY | O_NONBLOCK);
69    ioctl(fd, CDIOCCAPABILITY, NULL);   
70    close(fd);
71
72    if (errno != EBADF && errno != ENOTTY)
73        return 1;
74    else
75        return 0;
76}
77#else /* !__linux__ && !__FreeBSD_kernel__ */
78#define is_cdrom(path) 0
79#endif
80
81#if defined(__linux__)
82static int
83is_floppy(const char *path)
84{
85    return (strstr(path, "/dev/floppy") != NULL ||
86        strstr(path, "/dev/fd") != NULL);
87}
88#elif defined(__FreeBSD_kernel__) /* !__linux__ */
89static int
90is_floppy(const char *path)
91{
92    return (strstr(path, "/dev/fd") != NULL);
93}
94#else /* !__linux__ && !__FreeBSD_kernel__ */
95#define is_floppy(path) 0
96#endif
97
98static long long
99process_device(PedDevice *dev)
100{
101    if (dev->read_only)
102        return -1;
103    if (is_cdrom(dev->path) || is_floppy(dev->path))
104        return -1;
105    /* Exclude compcache (http://code.google.com/p/compcache/) */
106    if (strstr(dev->path, "/dev/ramzswap") != NULL ||
107        strstr(dev->path, "/dev/zram") != NULL)
108        return -1;
109    return dev->length * dev->sector_size;
110}
111
112int
113check_big_enough(long long required_space)
114{
115    PedDevice *dev = NULL;
116    ped_exception_fetch_all();
117    ped_device_probe_all();
118
119    bool big_enough = false;
120    for (dev = NULL; NULL != (dev = ped_device_get_next(dev));)
121    {
122        long long dev_size = process_device(dev);
123        if (dev_size > required_space)
124        {
125            big_enough = true;
126            break;
127        }
128    }
129
130    // We would free the devices to release allocated memory,
131    // but other modules might be using partman use well,
132    // and they can hold pointers to libparted structures in
133    // other threads.
134    //
135    // So prefer to leak memory, instead.
136    //
137    // ped_device_free_all();
138
139    return big_enough;
140}
141
142/*
143Local variables:
144indent-tabs-mode: nil
145c-file-style: "linux"
146c-font-lock-extra-types: ("Ped\\sw+")
147End:
148*/
Note: See TracBrowser for help on using the repository browser.