--- linux-1.6.12.c 2004-08-15 14:34:25.000000000 +0200 +++ linux.c 2004-08-17 17:05:01.421265000 +0200 @@ -221,7 +221,7 @@ || ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR)) static char* _device_get_part_path (PedDevice* dev, int num); -static int _partition_is_mounted_by_path (const char* path); +static int _partition_is_mounted (const char* part_name) ; static int _is_ide_major (int major) @@ -863,7 +863,7 @@ int status; part_name = _device_get_part_path (dev, i); - status = _partition_is_mounted_by_path (part_name); + status = _partition_is_mounted (part_name); ped_free (part_name); if (status) @@ -899,7 +899,7 @@ name = _device_get_part_path (dev, i); if (!name) break; - if (!_partition_is_mounted_by_path (name)) { + if (!_partition_is_mounted (name)) { fd = open (name, O_WRONLY, 0); if (fd > 0) { ioctl (fd, BLKFLSBUF); @@ -1470,158 +1470,80 @@ return _device_get_part_path (part->disk->dev, part->num); } -/* FIXME: should this be part of the API? Should it report a warning if the - * stat fails? */ static int -_partition_is_root_device (const char* part_name) +_mounted_search( const char* file_name, unsigned int part_major, unsigned int part_minor ) { - unsigned int root_dev; + int found = 0; struct stat part_stat; - FILE* proc_real_root_dev; - - /* even though this is an error, it probably means the partition - * doesn't exist, etc., and therefore isn't root ;) - */ - if (stat (part_name, &part_stat)) + char line[512], part_name[256]; + FILE* file = fopen (file_name, "r"); + + if ( ! file ) return 0; - - proc_real_root_dev = fopen ("/proc/sys/kernel/real-root-dev", "r"); - if (!proc_real_root_dev) - goto error; - fscanf (proc_real_root_dev, "%d", &root_dev); - fclose (proc_real_root_dev); - return root_dev == part_stat.st_rdev; - -error: - return -1; -} - -/* returns: - * 1 == mounted - * 0 == not mounted - * -1 == if unknown - * - * NOTE: /proc/mounts doesn't list the real root device (in Linux), but - * /dev/root instead. Since the root device is always mounted (by - * definition), this is checked separately in _partition_is_mounted_by_path() - */ -static int -_mount_table_search (const char* mtab_path, const char* part_name) -{ - char line [256]; - FILE* mtab; - - mtab = fopen (mtab_path, "r"); - if (!mtab) - goto error; - - while (1) { - if (!fgets (line, 256, mtab)) { - if (feof (mtab)) { - fclose (mtab); - return 0; - } else { - goto error_close_mtab; - } - } - if (strncmp (line, part_name, strlen (part_name)) == 0 - && isspace (line [strlen (part_name)])) { - fclose (mtab); - return 1; + + while (fgets (line, 512, file) ) + { + sscanf (line, "%s", part_name); + if ( stat (part_name, &part_stat) == 0 && + part_major == major (part_stat.st_rdev) && + part_minor == minor (part_stat.st_rdev) ) + { + found = 1; + break; } - } - -error_close_mtab: - fclose (mtab); -error: - ped_exception_throw ( - PED_EXCEPTION_WARNING, - PED_EXCEPTION_IGNORE, - _("Error reading %s (%s) to determine if partition is " - "mounted."), - mtab_path, strerror (errno)); + } + + fclose( file ) ; - return -1; + return found; } static int -_partition_is_mounted_by_path (const char* path) +_partition_is_mounted (const char* part_name) { - static int can_probe = 1; - - if (!can_probe) - return 0; - - switch (_partition_is_root_device (path)) { - case 1: return 1; - case -1: goto unknown; - } - switch (_mount_table_search ("/proc/mounts", path)) { - case 1: return 1; - case -1: - switch (_mount_table_search ("/etc/mtab", path)) { - case 1: return 1; - case -1: goto unknown; - } - } - switch (_mount_table_search ("/proc/swaps", path)) { - case 1: return 1; - case -1: goto unknown; - } - return 0; - -unknown: - can_probe = 0; - return ped_exception_throw ( - PED_EXCEPTION_WARNING, - PED_EXCEPTION_IGNORE_CANCEL, - _("Unable to determine if partitions are mounted via " - "/proc/mounts or /etc/mtab. Make sure you don't " - "attempt to resize or modify mounted file systems. " - "(Even read-only mounted)")) - != PED_EXCEPTION_IGNORE; -} - -static int -_partition_is_mounted (const PedPartition* part) -{ - char* part_name; int status; - - PED_ASSERT (part != NULL, return 0); - - if (part->num == -1) - return 0; - - part_name = _device_get_part_path (part->geom.dev, part->num); - if (!part_name) + unsigned int major, minor; + struct stat part_stat; + + if ( stat (part_name, &part_stat) != 0 ) return 0; - - status = _partition_is_mounted_by_path (part_name); - ped_free (part_name); + + major = major (part_stat.st_rdev) ; + minor = minor (part_stat.st_rdev) ; + + status = _mounted_search( "/etc/mtab", major, minor ) ; + + if ( ! status ) + status = _mounted_search( "/proc/swaps", major, minor ) ; + return status; } static int linux_partition_is_busy (const PedPartition* part) { + int status; PedPartition* walk; + char* part_name; PED_ASSERT (part != NULL, return 0); + + part_name = _device_get_part_path (part->geom.dev, part->num); - if (_partition_is_mounted (part)) - return 1; - + status = _partition_is_mounted (part_name) ; + if (part->type == PED_PARTITION_EXTENDED) { for (walk = part->part_list; walk; walk = walk->next) { if (!ped_partition_is_active (walk)) continue; if (linux_partition_is_busy (walk)) - return 1; + { status = 1; break; } } } + + ped_free( part_name ); - return 0; + return status; } static int