コード例 #1
0
ファイル: lex-manfs.c プロジェクト: alaramedfox/avian
 foreach(i, mounted_volumes) {
    if(strcmp(point, mountpoints[i].point) == 0) {
       device_t device = mountpoints[i].device;
       format_t filesystem = str_to_fs(fs);
       printf("Formatting %s as %s\n", point, fs);
       format_device(device, filesystem);
       return;
    }
 }
コード例 #2
0
int main(int argc, char **argv)
{
	char *device;
	unsigned drive;
	unsigned type;
	unsigned media_size;
	unsigned drive_size;
	int verify= 0;
	struct stat st0, st;
	char special[PATH_MAX + 1], mounted_on[PATH_MAX + 1];
	char version[MNTNAMELEN], rw_flag[MNTFLAGLEN];

	/* Option -v. */
	while (argc > 1 && argv[1][0] == '-') {
		char *p;

		for (p= argv[1]; *p == '-' || *p == 'v'; p++) {
			if (*p == 'v') verify= 1;
		}
		if (*p != 0) usage();
		argc--;
		argv++;
		if (strcmp(argv[0], "--") == 0) break;
	}

	if (argc < 2 || argc > 4) usage();

	/* Check if the caller has read-write permission.  Use the access()
	 * call to check with the real uid & gid.  This program is usually
	 * set-uid root.
	 */
	device= argv[1];
	if (stat(device, &st0) < 0
		|| access(device, R_OK|W_OK) < 0
		|| stat(device, &st) < 0
		|| (errno= EACCES, 0)	/* set errno for following tests */
		|| st.st_dev != st0.st_dev
		|| st.st_ino != st0.st_ino
	) {
		fatal(device);
	}

	if (!S_ISBLK(st.st_mode) || !isfloppy(st.st_rdev)) {
		fprintf(stderr, "format: %s: not a floppy device\n", device);
		exit(1);
	}

	drive= fl_drive(st.st_rdev);
	type= fl_type(st.st_rdev);

	/* The drive should not be mounted. */
	if (load_mtab("mkfs") < 0) exit(1);

	while (get_mtab_entry(special, mounted_on, version, rw_flag) == 0) {
		if (stat(special, &st) >= 0 && isfloppy(st.st_rdev)
					&& fl_drive(st.st_rdev) == drive) {
			fprintf(stderr, "format: %s is mounted on %s\n",
				device, mounted_on);
			exit(1);
		}
	}

	if (isflauto(type)) {
		/* Auto type 0 requires size(s). */
		unsigned long lmedia, ldrive;
		char *end;

		if (argc < 3) {
			fprintf(stderr,
			"format: no size specified for auto floppy device %s\n",
				device);
			usage();
		}

		lmedia= strtoul(argv[2], &end, 10);
		if (end == argv[2] || *end != 0 || lmedia > 20 * 1024)
			usage();

		if (argc == 4) {
			ldrive= strtoul(argv[3], &end, 10);
			if (end == argv[3] || *end != 0 || ldrive > 20 * 1024)
				usage();
		} else {
			ldrive= lmedia;
		}

		/* Silently correct wrong ordered sizes. */
		if (lmedia > ldrive) {
			media_size= ldrive;
			drive_size= lmedia;
		} else {
			media_size= lmedia;
			drive_size= ldrive;
		}

		/* A 1.44M drive can do 720k diskettes with no extra tricks.
		 * Diddle with the 720k params so it is found.
		 */
		if (media_size == 720 && drive_size == 1440)
			parameters[4 - 1].drive_size= 1440;

		/* Translate the auto type to a known type. */
		for (type= 1; type <= NR_TYPES; type++) {
			if (parameters[type - 1].media_size == media_size
				&& parameters[type - 1].drive_size == drive_size
			) break;
		}

		if (!isfltyped(type)) {
			fprintf(stderr,
			"format: can't format a %uk floppy in a %uk drive\n",
				media_size, drive_size);
			exit(1);
		}
	} else
	if (isfltyped(type)) {
		/* No sizes needed for a non-auto type. */

		if (argc > 2) {
			fprintf(stderr,
	"format: no sizes need to be specified for non-auto floppy device %s\n",
				device);
			usage();
		}
	} else
	if (isflpart(type)) {
		fprintf(stderr,
			"format: floppy partition %s can't be formatted\n",
			device);
		exit(1);
	} else {
		fprintf(stderr,
			"format: %s: can't format strange type %d\n",
			device, type);
	}

	format_device(drive, type, verify);
	exit(0);
}
コード例 #3
0
ファイル: nandroid.c プロジェクト: Atlant777/philz_touch_cwm6
int nandroid_restore_partition_extended(const char* backup_path, const char* mount_point, int umount_when_finished) {
    int ret = 0;
    char* name = basename(mount_point);

    nandroid_restore_handler restore_handler = NULL;
    const char *filesystems[] = { "yaffs2", "ext2", "ext3", "ext4", "vfat", "rfs", NULL };
    const char* backup_filesystem = NULL;
    Volume *vol = volume_for_path(mount_point);
    const char *device = NULL;
    if (vol != NULL)
        device = vol->device;

    char tmp[PATH_MAX];
    sprintf(tmp, "%s/%s.img", backup_path, name);
    struct stat file_info;
    if (0 != (ret = statfs(tmp, &file_info))) {
        // can't find the backup, it may be the new backup format?
        // iterate through the backup types
        printf("couldn't find old .img format\n");
        char *filesystem;
        int i = 0;
        while ((filesystem = filesystems[i]) != NULL) {
            sprintf(tmp, "%s/%s.%s.img", backup_path, name, filesystem);
            if (0 == (ret = statfs(tmp, &file_info))) {
                backup_filesystem = filesystem;
                restore_handler = unyaffs_wrapper;
                break;
            }
            sprintf(tmp, "%s/%s.%s.tar", backup_path, name, filesystem);
            if (0 == (ret = statfs(tmp, &file_info))) {
                backup_filesystem = filesystem;
                restore_handler = tar_extract_wrapper;
                break;
            }
            sprintf(tmp, "%s/%s.%s.dup", backup_path, name, filesystem);
            if (0 == (ret = statfs(tmp, &file_info))) {
                backup_filesystem = filesystem;
                restore_handler = dedupe_extract_wrapper;
                break;
            }
            i++;
        }

        if (backup_filesystem == NULL || restore_handler == NULL) {
            //ui_print("%s.img not found. Skipping restore of %s.\n", name, mount_point);
            ui_print("No %s backup found(img, tar, dup). Skipping restore of %s.\n", name, mount_point);
            return 0;
        }
        else {
            printf("Found new backup image: %s\n", tmp);
        }

        // If the fs_type of this volume is "auto" or mount_point is /data
        // and is_data_media, let's revert
        // to using a rm -rf, rather than trying to do a
        // ext3/ext4/whatever format.
        // This is because some phones (like DroidX) will freak out if you
        // reformat the /system or /data partitions, and not boot due to
        // a locked bootloader.
        // Other devices, like the Galaxy Nexus, XOOM, and Galaxy Tab 10.1
        // have a /sdcard symlinked to /data/media.
        // Or of volume does not exist (.android_secure), just rm -rf.
        if (vol == NULL || 0 == strcmp(vol->fs_type, "auto"))
            backup_filesystem = NULL;
        if (0 == strcmp(vol->mount_point, "/data") && is_data_media())
            backup_filesystem = NULL;
    }

    ensure_directory(mount_point);

    int callback = stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info) != 0;
    compute_archive_stats(tmp);

    ui_print("Restoring %s...\n", name);
    if (backup_filesystem == NULL) {
        if (0 != (ret = format_volume(mount_point))) {
            ui_print("Error while formatting %s!\n", mount_point);
            return ret;
        }
    }
    else if (0 != (ret = format_device(device, mount_point, backup_filesystem))) {
        ui_print("Error while formatting %s!\n", mount_point);
        return ret;
    }

    if (0 != (ret = ensure_path_mounted(mount_point))) {
        ui_print("Can't mount %s!\n", mount_point);
        return ret;
    }

    if (restore_handler == NULL)
        restore_handler = get_restore_handler(mount_point);
    if (restore_handler == NULL) {
        ui_print("Error finding an appropriate restore handler.\n");
        return -2;
    }
    if (0 != (ret = restore_handler(tmp, mount_point, callback))) {
        ui_print("Error while restoring %s!\n", mount_point);
        return ret;
    }

    if (umount_when_finished) {
        ensure_path_unmounted(mount_point);
    }
    
    return 0;
}
コード例 #4
0
int nandroid_restore_partition_extended(const char* backup_path, const char* mount_point, int umount_when_finished) {
    int ret = 0;
    char* name = basename(mount_point);

    nandroid_restore_handler restore_handler = NULL;
    const char *filesystems[] = { "yaffs2", "ext2", "ext3", "ext4", "vfat", "rfs", NULL };
    const char* backup_filesystem = NULL;
    Volume *vol = volume_for_path(mount_point);
    const char *device = NULL;
    if (vol != NULL)
        device = vol->device;

    char tmp[PATH_MAX];
    sprintf(tmp, "%s/%s.img", backup_path, name);
    struct stat file_info;
    if (strcmp(backup_path, "-") == 0) {
        if (vol)
            backup_filesystem = vol->fs_type;
        restore_handler = tar_extract_wrapper;
        strcpy(tmp, "/proc/self/fd/0");
    }
    else if (0 != (ret = statfs(tmp, &file_info))) {
        // can't find the backup, it may be the new backup format?
        // iterate through the backup types
        printf("找不到默认\n");
        char *filesystem;
        int i = 0;
        while ((filesystem = filesystems[i]) != NULL) {
            sprintf(tmp, "%s/%s.%s.img", backup_path, name, filesystem);
            if (0 == (ret = statfs(tmp, &file_info))) {
                backup_filesystem = filesystem;
                restore_handler = unyaffs_wrapper;
                break;
            }
            sprintf(tmp, "%s/%s.%s.tar", backup_path, name, filesystem);
            if (0 == (ret = statfs(tmp, &file_info))) {
                backup_filesystem = filesystem;
                restore_handler = tar_extract_wrapper;
                break;
            }
            sprintf(tmp, "%s/%s.%s.dup", backup_path, name, filesystem);
            if (0 == (ret = statfs(tmp, &file_info))) {
                backup_filesystem = filesystem;
                restore_handler = dedupe_extract_wrapper;
                break;
            }
            i++;
        }

        if (backup_filesystem == NULL || restore_handler == NULL) {
            ui_print("%s.镜像没找到,因此跳过该处恢复. %s.\n", name, mount_point);
            return 0;
        }
        else {
            printf("找到新的可恢复镜像: %s\n", tmp);
        }
    }

    // If the fs_type of this volume is "auto" or mount_point is /data
    // and is_data_media, let's revert
    // to using a rm -rf, rather than trying to do a
    // ext3/ext4/whatever format.
    // This is because some phones (like DroidX) will freak out if you
    // reformat the /system or /data partitions, and not boot due to
    // a locked bootloader.
    // Other devices, like the Galaxy Nexus, XOOM, and Galaxy Tab 10.1
    // have a /sdcard symlinked to /data/media.
    // Or of volume does not exist (.android_secure), just rm -rf.
    if (vol == NULL || 0 == strcmp(vol->fs_type, "auto"))
        backup_filesystem = NULL;
    if (0 == strcmp(vol->mount_point, "/data") && is_data_media())
        backup_filesystem = NULL;

    ensure_directory(mount_point);

    int callback = stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info) != 0;

    ui_print("正在恢复 %s...\n", name);
    if (backup_filesystem == NULL) {
        if (0 != (ret = format_volume(mount_point))) {
            ui_print("格式化下列分区失败 %s!\n", mount_point);
            return ret;
        }
    }
    else if (0 != (ret = format_device(device, mount_point, backup_filesystem))) {
        ui_print("格式化下列分区失败 %s!\n", mount_point);
        return ret;
    }

    if (0 != (ret = ensure_path_mounted(mount_point))) {
        ui_print("挂载下列分区失败 %s!\n", mount_point);
        return ret;
    }

    if (restore_handler == NULL)
        restore_handler = get_restore_handler(mount_point);

    // override restore handler for undump
    if (strcmp(backup_path, "-") == 0) {
        restore_handler = tar_undump_wrapper;
    }

    if (restore_handler == NULL) {
        ui_print("找不到合适的方案\n");
        return -2;
    }

    if (0 != (ret = restore_handler(tmp, mount_point, callback))) {
        ui_print("恢复失败: %s!\n", mount_point);
        return ret;
    }

    if (umount_when_finished) {
        ensure_path_unmounted(mount_point);
    }

    return 0;
}
コード例 #5
0
ファイル: nandroid.c プロジェクト: Arakmar/cwm5-g3mod
int nandroid_restore_partition_extended(const char* backup_path, const char* mount_point, int umount_when_finished) {
    int ret = 0;
    char* name = basename(mount_point);

    nandroid_restore_handler restore_handler = NULL;
    const char *filesystems[] = { "yaffs2", "ext2", "ext3", "ext4", "vfat", "rfs", NULL };
    const char* backup_filesystem = NULL;
    Volume *vol = volume_for_path(mount_point);
    const char *device = NULL;
    if (vol != NULL)
        device = vol->device;

    char tmp[PATH_MAX];
    sprintf(tmp, "%s/%s.img", backup_path, name);
    struct stat file_info;
    if (0 != (ret = statfs(tmp, &file_info))) {
        // can't find the backup, it may be the new backup format?
        // iterate through the backup types
        printf("couldn't find default\n");
        char *filesystem;
        int i = 0;
        while ((filesystem = filesystems[i]) != NULL) {
            sprintf(tmp, "%s/%s.%s.img", backup_path, name, filesystem);
            if (0 == (ret = statfs(tmp, &file_info))) {
                backup_filesystem = filesystem;
                restore_handler = unyaffs_wrapper;
                break;
            }
            sprintf(tmp, "%s/%s.%s.tar", backup_path, name, filesystem);
            if (0 == (ret = statfs(tmp, &file_info))) {
                backup_filesystem = filesystem;
                restore_handler = tar_extract_wrapper;
                break;
            }
            i++;
        }

        if (backup_filesystem == NULL || restore_handler == NULL) {
            ui_print("%s.img not found. Skipping restore of %s.\n", name, mount_point);
            return 0;
        }
        else {
            printf("Found new backup image: %s\n", tmp);
        }

        // If the fs_type of this volume is "auto", let's revert to using a
        // rm -rf, rather than trying to do a ext3/ext4/whatever format.
        // This is because some phones (like DroidX) will freak out if you
        // reformat the /system or /data partitions, and not boot due to
        // a locked bootloader.
        // The "auto" fs type preserves the file system, and does not
        // trigger that lock.
        // Or of volume does not exist (.android_secure), just rm -rf.
        if (vol == NULL || 0 == strcmp(vol->fs_type, "auto"))
            backup_filesystem = NULL;
    }
    else {
        // Force legacy tar extraction to old CWM2 backup files
        restore_handler = tar_extract_wrapper_legacy;
    }

    ensure_directory(mount_point);

    int callback = stat("/sdcard/clockworkmod/.hidenandroidprogress", &file_info) != 0;

    ui_print("Restoring %s...\n", name);
    if (backup_filesystem == NULL) {
        if (0 != (ret = format_volume(mount_point))) {
            ui_print("Error while formatting %s!\n", mount_point);
            return ret;
        }
    }
    else if (0 != (ret = format_device(device, mount_point, backup_filesystem))) {
        ui_print("Error while formatting %s!\n", mount_point);
        return ret;
    }

    if (0 != (ret = ensure_path_mounted(mount_point))) {
        ui_print("Can't mount %s!\n", mount_point);
        return ret;
    }

    if (restore_handler == NULL)
        restore_handler = get_restore_handler(mount_point);
    if (restore_handler == NULL) {
        ui_print("Error finding an appropriate restore handler.\n");
        return -2;
    }
    if (0 != (ret = restore_handler(tmp, mount_point, callback))) {
        ui_print("Error while restoring %s!\n", mount_point);
        return ret;
    }

    if (umount_when_finished) {
        ensure_path_unmounted(mount_point);
    }
    
    return 0;
}