📄 cfdisk.c
字号:
putchar(BELL); /* CTRL-G */ warning_last_time = TRUE; }}static voidfatal(char *s, int ret) { char *err1 = _("FATAL ERROR"); char *err2 = _("Press any key to exit cfdisk"); if (curses_started) { char *str = xmalloc(strlen(s) + strlen(err1) + strlen(err2) + 10); sprintf(str, "%s: %s", err1, s); if (strlen(str) > COLS) str[COLS] = 0; mvaddstr(WARNING_START, (COLS-strlen(str))/2, str); sprintf(str, "%s", err2); if (strlen(str) > COLS) str[COLS] = 0; mvaddstr(WARNING_START+1, (COLS-strlen(str))/2, str); putchar(BELL); /* CTRL-G */ refresh(); (void)getch(); die_x(ret); } else { fprintf(stderr, "%s: %s\n", err1, s); exit(ret); }}static voiddie(int dummy) { die_x(0);}static voiddie_x(int ret) { signal(SIGINT, old_SIGINT); signal(SIGTERM, old_SIGTERM);#ifdef SLCURSES SLsmg_gotorc(LINES-1, 0); SLsmg_refresh();#else mvcur(0, COLS-1, LINES-1, 0);#endif nl(); endwin(); printf("\n"); fdexit(ret);}static voidread_sector(char *buffer, long long sect_num) { if (ext2_llseek(fd, sect_num*SECTOR_SIZE, SEEK_SET) < 0) fatal(_("Cannot seek on disk drive"), 2); if (read(fd, buffer, SECTOR_SIZE) != SECTOR_SIZE) fatal(_("Cannot read disk drive"), 2);}static voidwrite_sector(char *buffer, long long sect_num) { if (ext2_llseek(fd, sect_num*SECTOR_SIZE, SEEK_SET) < 0) fatal(_("Cannot seek on disk drive"), 2); if (write(fd, buffer, SECTOR_SIZE) != SECTOR_SIZE) fatal(_("Cannot write disk drive"), 2);}static voiddos_copy_to_info(char *to, int tosz, char *from, int fromsz) { int i; for(i=0; i<tosz && i<fromsz && isascii(from[i]); i++) to[i] = from[i]; to[i] = 0;}static voidget_dos_label(int i) { char sector[128];#define DOS_OSTYPE_OFFSET 3#define DOS_LABEL_OFFSET 43#define DOS_FSTYPE_OFFSET 54#define DOS_OSTYPE_SZ 8#define DOS_LABEL_SZ 11#define DOS_FSTYPE_SZ 8 long long offset; offset = (p_info[i].first_sector + p_info[i].offset) * SECTOR_SIZE; if (ext2_llseek(fd, offset, SEEK_SET) == offset && read(fd, §or, sizeof(sector)) == sizeof(sector)) { dos_copy_to_info(p_info[i].ostype, OSTYPESZ, sector+DOS_OSTYPE_OFFSET, DOS_OSTYPE_SZ); dos_copy_to_info(p_info[i].volume_label, LABELSZ, sector+DOS_LABEL_OFFSET, DOS_LABEL_SZ); dos_copy_to_info(p_info[i].fstype, FSTYPESZ, sector+DOS_FSTYPE_OFFSET, DOS_FSTYPE_SZ); }}#define REISERFS_SUPER_MAGIC_STRING "ReIsErFs"#define REISER2FS_SUPER_MAGIC_STRING "ReIsEr2Fs"struct reiserfs_super_block { char s_dummy0[ 52]; char s_magic [ 12]; char s_dummy1[140];};static intis_reiserfs_magic_string(const struct reiserfs_super_block *rs) { return (!strncmp(rs->s_magic, REISERFS_SUPER_MAGIC_STRING, strlen(REISERFS_SUPER_MAGIC_STRING)) || !strncmp(rs->s_magic, REISER2FS_SUPER_MAGIC_STRING, strlen(REISER2FS_SUPER_MAGIC_STRING)));}static voidget_linux_label(int i) {#define EXT2LABELSZ 16#define EXT2_SUPER_MAGIC 0xEF53#define EXT3_FEATURE_COMPAT_HAS_JOURNAL 0x0004 struct ext2_super_block { char s_dummy0[56]; unsigned char s_magic[2]; char s_dummy1[34]; unsigned char s_feature_compat[4]; char s_dummy2[24]; char s_volume_name[EXT2LABELSZ]; char s_last_mounted[64]; char s_dummy3[824]; } e2fsb;#define REISERFS_DISK_OFFSET_IN_BYTES (64 * 1024) struct reiserfs_super_block reiserfsb;#define XFS_SUPER_MAGIC "XFSB"#define XFSLABELSZ 12 struct xfs_super_block { unsigned char s_magic[4]; unsigned char s_dummy0[104]; unsigned char s_fname[XFSLABELSZ]; unsigned char s_dummy1[904]; } xfsb; char *label; long long offset; int j; offset = (p_info[i].first_sector + p_info[i].offset) * SECTOR_SIZE + 1024; if (ext2_llseek(fd, offset, SEEK_SET) == offset && read(fd, &e2fsb, sizeof(e2fsb)) == sizeof(e2fsb) && e2fsb.s_magic[0] + (e2fsb.s_magic[1]<<8) == EXT2_SUPER_MAGIC) { label = e2fsb.s_volume_name; for(j=0; j<EXT2LABELSZ && j<LABELSZ && isprint(label[j]); j++) p_info[i].volume_label[j] = label[j]; p_info[i].volume_label[j] = 0; /* ext2 or ext3? */ if (e2fsb.s_feature_compat[0]&EXT3_FEATURE_COMPAT_HAS_JOURNAL) strncpy(p_info[i].fstype, "ext3", FSTYPESZ); else strncpy(p_info[i].fstype, "ext2", FSTYPESZ); return; } offset = (p_info[i].first_sector + p_info[i].offset) * SECTOR_SIZE + 0; if (ext2_llseek(fd, offset, SEEK_SET) == offset && read(fd, &xfsb, sizeof(xfsb)) == sizeof(xfsb) && !strcmp(xfsb.s_magic, XFS_SUPER_MAGIC)) { label = xfsb.s_fname; for(j=0; j<XFSLABELSZ && j<LABELSZ && isprint(label[j]); j++) p_info[i].volume_label[j] = label[j]; p_info[i].volume_label[j] = 0; strncpy(p_info[i].fstype, "xfs", FSTYPESZ); return; } /* reiserfs? */ offset = (p_info[i].first_sector + p_info[i].offset) * SECTOR_SIZE + REISERFS_DISK_OFFSET_IN_BYTES; if (ext2_llseek(fd, offset, SEEK_SET) == offset && read(fd, &reiserfsb, 1024) == 1024 && is_reiserfs_magic_string(&reiserfsb)) { strncpy(p_info[i].fstype, "reiserfs", FSTYPESZ); return; }}static voidcheck_part_info(void) { int i, pri = 0, log = 0; for (i = 0; i < num_parts; i++) if (p_info[i].id > 0 && IS_PRIMARY(p_info[i].num)) pri++; else if (p_info[i].id > 0 && IS_LOGICAL(p_info[i].num)) log++; if (is_extended(ext_info.id)) { if (log > 0) pri++; else { ext_info.first_sector = 0; ext_info.last_sector = 0; ext_info.offset = 0; ext_info.flags = 0; ext_info.id = FREE_SPACE; ext_info.num = PRIMARY; } } if (pri >= 4) { for (i = 0; i < num_parts; i++) if (p_info[i].id == FREE_SPACE || p_info[i].id == UNUSABLE) { if (is_extended(ext_info.id)) { if (p_info[i].first_sector >= ext_info.first_sector && p_info[i].last_sector <= ext_info.last_sector) { p_info[i].id = FREE_SPACE; p_info[i].num = LOGICAL; } else if (i > 0 && p_info[i-1].first_sector >= ext_info.first_sector && p_info[i-1].last_sector <= ext_info.last_sector) { p_info[i].id = FREE_SPACE; p_info[i].num = LOGICAL; } else if (i < num_parts-1 && p_info[i+1].first_sector >= ext_info.first_sector && p_info[i+1].last_sector <= ext_info.last_sector) { p_info[i].id = FREE_SPACE; p_info[i].num = LOGICAL; } else p_info[i].id = UNUSABLE; } else /* if (!is_extended(ext_info.id)) */ p_info[i].id = UNUSABLE; } else /* if (p_info[i].id > 0) */ while (0); /* Leave these alone */ } else { /* if (pri < 4) */ for (i = 0; i < num_parts; i++) { if (p_info[i].id == UNUSABLE) p_info[i].id = FREE_SPACE; if (p_info[i].id == FREE_SPACE) { if (is_extended(ext_info.id)) { if (p_info[i].first_sector >= ext_info.first_sector && p_info[i].last_sector <= ext_info.last_sector) p_info[i].num = LOGICAL; else if (i > 0 && p_info[i-1].first_sector >= ext_info.first_sector && p_info[i-1].last_sector <= ext_info.last_sector) p_info[i].num = PRI_OR_LOG; else if (i < num_parts-1 && p_info[i+1].first_sector >= ext_info.first_sector && p_info[i+1].last_sector <= ext_info.last_sector) p_info[i].num = PRI_OR_LOG; else p_info[i].num = PRIMARY; } else /* if (!is_extended(ext_info.id)) */ p_info[i].num = PRI_OR_LOG; } else /* if (p_info[i].id > 0) */ while (0); /* Leave these alone */ } }}static voidremove_part(int i) { int p; for (p = i; p < num_parts; p++) p_info[p] = p_info[p+1]; num_parts--; if (cur_part == num_parts) cur_part--;}static voidinsert_empty_part(int i, long long first, long long last) { int p; for (p = num_parts; p > i; p--) p_info[p] = p_info[p-1]; p_info[i].first_sector = first; p_info[i].last_sector = last; p_info[i].offset = 0; p_info[i].flags = 0; p_info[i].id = FREE_SPACE; p_info[i].num = PRI_OR_LOG; p_info[i].volume_label[0] = 0; p_info[i].fstype[0] = 0; p_info[i].ostype[0] = 0; num_parts++;}static voiddel_part(int i) { int num = p_info[i].num; if (i > 0 && (p_info[i-1].id == FREE_SPACE || p_info[i-1].id == UNUSABLE)) { /* Merge with previous partition */ p_info[i-1].last_sector = p_info[i].last_sector; remove_part(i--); } if (i < num_parts - 1 && (p_info[i+1].id == FREE_SPACE || p_info[i+1].id == UNUSABLE)) { /* Merge with next partition */ p_info[i+1].first_sector = p_info[i].first_sector; remove_part(i); } if (i > 0) p_info[i].first_sector = p_info[i-1].last_sector + 1; else p_info[i].first_sector = 0; if (i < num_parts - 1) p_info[i].last_sector = p_info[i+1].first_sector - 1; else p_info[i].last_sector = total_size - 1; p_info[i].offset = 0; p_info[i].flags = 0; p_info[i].id = FREE_SPACE; p_info[i].num = PRI_OR_LOG; if (IS_LOGICAL(num)) { /* We have a logical partition --> shrink the extended partition * if (1) this is the first logical drive, or (2) this is the * last logical drive; and if there are any other logical drives * then renumber the ones after "num". */ if (i == 0 || (i > 0 && IS_PRIMARY(p_info[i-1].num))) { ext_info.first_sector = p_info[i].last_sector + 1; ext_info.offset = 0; } if (i == num_parts-1 || (i < num_parts-1 && IS_PRIMARY(p_info[i+1].num))) ext_info.last_sector = p_info[i].first_sector - 1; for (i = 0; i < num_parts; i++) if (p_info[i].num > num) p_info[i].num--; } /* Clean up the rest of the partitions */ check_part_info();}static intadd_part(int num, int id, int flags, long long first, long long last, long long offset, int want_label, char **errmsg) { int i, pri = 0, log = 0; if (num_parts == MAXIMUM_PARTS) { *errmsg = _("Too many partitions"); return -1; } if (first < 0) { *errmsg = _("Partition begins before sector 0"); return -1; } if (last < 0) { *errmsg = _("Partition ends before sector 0"); return -1; } if (first >= total_size) { *errmsg = _("Partition begins after end-of-disk"); return -1; } if (last >= actual_size) { *errmsg = _("Partition ends after end-of-disk"); return -1; } if (last >= total_size) { *errmsg = _("Partition ends in the final partial cylinder"); return -1; } for (i = 0; i < num_parts; i++) { if (p_info[i].id > 0 && IS_PRIMARY(p_info[i].num)) pri++; else if (p_info[i].id > 0 && IS_LOGICAL(p_info[i].num)) log++; } if (is_extended(ext_info.id) && log > 0) pri++; if (IS_PRIMARY(num)) { if (pri >= 4) { return -1; /* no room for more */ } else pri++; } for (i = 0; i < num_parts && p_info[i].last_sector < first; i++); if (i < num_parts && p_info[i].id != FREE_SPACE) { if (last < p_info[i].first_sector) *errmsg = _("logical partitions not in disk order"); else if (first + offset <= p_info[i].last_sector && p_info[i].first_sector + p_info[i].offset <= last) *errmsg = _("logical partitions overlap"); else *errmsg = _("enlarged logical partitions overlap"); return -1; } if (i == num_parts || last > p_info[i].last_sector) { return -1; } if (is_extended(id)) { if (ext_info.id != FREE_SPACE) { return -1; /* second extended */ } else if (IS_PRIMARY(num)) { ext_info.first_sector = first; ext_info.last_sector = last; ext_info.offset = offset; ext_info.flags = flags; ext_info.id = id; ext_info.num = num; ext_info.volume_label[0] = 0; ext_info.fstype[0] = 0; ext_info.ostype[0] = 0; return 0; } else { return -1; /* explicit extended logical */ } } if (IS_LOGICAL(num)) { if (!is_extended(ext_info.id)) { print_warning(_("!!!! Internal error creating logical " "drive with no extended partition !!!!")); } else { /* We might have a logical partition outside of the extended * partition's range --> we have to extend the extended * partition's range to encompass this new partition, but we * must make sure that there are no primary partitions between * it and the closest logical drive in extended partition. */ if (first < ext_info.first_sector) { if (i < num_parts-1 && IS_PRIMARY(p_info[i+1].num)) { print_warning(_("Cannot create logical drive here -- would create two extended partitions")); return -1; } else { if (first == 0) { ext_info.first_sector = 0; ext_info.offset = first = offset; } else { ext_info.first_sector = first; } } } else if (last > ext_info.last_sector) { if (i > 0 && IS_PRIMARY(p_info[i-1].num)) { print_warning(_("Cannot create logical drive here -- would create two extended partitions")); return -1; } else { ext_info.last_sector = last; } } } } if (first != p_info[i].first_sector && !(IS_LOGICAL(num) && first == offset)) { insert_empty_part(i, p_info[i].first_sector, first-1); i++; } if (last != p_info[i].last_sector) insert_empty_part(i+1, last+1, p_info[i].last_sector); p_info[i].first_sector = first; p_info[i].last_sector = last; p_info[i].offset = offset; p_info[i].flags = flags;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -