Esempio n. 1
0
func (s *StepCreateFloppy) Run(state multistep.StateBag) multistep.StepAction {
	if len(s.Files) == 0 {
		log.Println("No floppy files specified. Floppy disk will not be made.")
		return multistep.ActionContinue
	}

	ui := state.Get("ui").(packer.Ui)
	ui.Say("Creating floppy disk...")

	// Create a temporary file to be our floppy drive
	floppyF, err := ioutil.TempFile("", "packer")
	if err != nil {
		state.Put("error",
			fmt.Errorf("Error creating temporary file for floppy: %s", err))
		return multistep.ActionHalt
	}
	defer floppyF.Close()

	// Set the path so we can remove it later
	s.floppyPath = floppyF.Name()

	log.Printf("Floppy path: %s", floppyF.Name())

	// Set the size of the file to be a floppy sized
	if err := floppyF.Truncate(1440 * 1024); err != nil {
		state.Put("error", fmt.Errorf("Error creating floppy: %s", err))
		return multistep.ActionHalt
	}

	// BlockDevice backed by the file for our filesystem
	log.Println("Initializing block device backed by temporary file")
	device, err := fs.NewFileDisk(floppyF)
	if err != nil {
		state.Put("error", fmt.Errorf("Error creating floppy: %s", err))
		return multistep.ActionHalt
	}

	// Format the block device so it contains a valid FAT filesystem
	log.Println("Formatting the block device with a FAT filesystem...")
	formatConfig := &fat.SuperFloppyConfig{
		FATType: fat.FAT12,
		Label:   "packer",
		OEMName: "packer",
	}
	if err := fat.FormatSuperFloppy(device, formatConfig); err != nil {
		state.Put("error", fmt.Errorf("Error creating floppy: %s", err))
		return multistep.ActionHalt
	}

	// The actual FAT filesystem
	log.Println("Initializing FAT filesystem on block device")
	fatFs, err := fat.New(device)
	if err != nil {
		state.Put("error", fmt.Errorf("Error creating floppy: %s", err))
		return multistep.ActionHalt
	}

	// Get the root directory to the filesystem
	log.Println("Reading the root directory from the filesystem")
	rootDir, err := fatFs.RootDir()
	if err != nil {
		state.Put("error", fmt.Errorf("Error creating floppy: %s", err))
		return multistep.ActionHalt
	}

	// Go over each file and copy it.
	for _, filename := range s.Files {
		ui.Message(fmt.Sprintf("Copying: %s", filepath.Base(filename)))
		if err := s.addSingleFile(rootDir, filename); err != nil {
			state.Put("error", fmt.Errorf("Error adding file to floppy: %s", err))
			return multistep.ActionHalt
		}
	}

	// Set the path to the floppy so it can be used later
	state.Put("floppy_path", s.floppyPath)

	return multistep.ActionContinue
}
Esempio n. 2
0
func (s *StepCreateFloppy) Run(state multistep.StateBag) multistep.StepAction {
	if len(s.Files) == 0 && len(s.Directories) == 0 {
		log.Println("No floppy files specified. Floppy disk will not be made.")
		return multistep.ActionContinue
	}

	s.FilesAdded = make(map[string]bool)

	ui := state.Get("ui").(packer.Ui)
	ui.Say("Creating floppy disk...")

	// Create a temporary file to be our floppy drive
	floppyF, err := ioutil.TempFile("", "packer")
	if err != nil {
		state.Put("error",
			fmt.Errorf("Error creating temporary file for floppy: %s", err))
		return multistep.ActionHalt
	}
	defer floppyF.Close()

	// Set the path so we can remove it later
	s.floppyPath = floppyF.Name()

	log.Printf("Floppy path: %s", s.floppyPath)

	// Set the size of the file to be a floppy sized
	if err := floppyF.Truncate(1440 * 1024); err != nil {
		state.Put("error", fmt.Errorf("Error creating floppy: %s", err))
		return multistep.ActionHalt
	}

	// BlockDevice backed by the file for our filesystem
	log.Println("Initializing block device backed by temporary file")
	device, err := fs.NewFileDisk(floppyF)
	if err != nil {
		state.Put("error", fmt.Errorf("Error creating floppy: %s", err))
		return multistep.ActionHalt
	}

	// Format the block device so it contains a valid FAT filesystem
	log.Println("Formatting the block device with a FAT filesystem...")
	formatConfig := &fat.SuperFloppyConfig{
		FATType: fat.FAT12,
		Label:   "packer",
		OEMName: "packer",
	}
	if err := fat.FormatSuperFloppy(device, formatConfig); err != nil {
		state.Put("error", fmt.Errorf("Error creating floppy: %s", err))
		return multistep.ActionHalt
	}

	// The actual FAT filesystem
	log.Println("Initializing FAT filesystem on block device")
	fatFs, err := fat.New(device)
	if err != nil {
		state.Put("error", fmt.Errorf("Error creating floppy: %s", err))
		return multistep.ActionHalt
	}

	// Get the root directory to the filesystem and create a cache for any directories within
	log.Println("Reading the root directory from the filesystem")
	rootDir, err := fatFs.RootDir()
	if err != nil {
		state.Put("error", fmt.Errorf("Error creating floppy: %s", err))
		return multistep.ActionHalt
	}
	cache := fsDirectoryCache(rootDir)

	// Utility functions for walking through a directory grabbing all files flatly
	globFiles := func(files []string, list chan string) {
		for _, filename := range files {
			if strings.IndexAny(filename, "*?[") >= 0 {
				matches, _ := filepath.Glob(filename)
				if err != nil {
					continue
				}

				for _, match := range matches {
					list <- match
				}
				continue
			}
			list <- filename
		}
		close(list)
	}

	var crawlDirectoryFiles []string
	crawlDirectory := func(path string, info os.FileInfo, err error) error {
		if !info.IsDir() {
			crawlDirectoryFiles = append(crawlDirectoryFiles, path)
			ui.Message(fmt.Sprintf("Adding file: %s", path))
		}
		return nil
	}
	crawlDirectoryFiles = []string{}

	// Collect files and copy them flatly...because floppy_files is broken on purpose.
	var filelist chan string
	filelist = make(chan string)
	go globFiles(s.Files, filelist)

	ui.Message("Copying files flatly from floppy_files")
	for {
		filename, ok := <-filelist
		if !ok {
			break
		}

		finfo, err := os.Stat(filename)
		if err != nil {
			state.Put("error", fmt.Errorf("Error trying to stat : %s : %s", filename, err))
			return multistep.ActionHalt
		}

		// walk through directory adding files to the root of the fs
		if finfo.IsDir() {
			ui.Message(fmt.Sprintf("Copying directory: %s", filename))

			err := filepath.Walk(filename, crawlDirectory)
			if err != nil {
				state.Put("error", fmt.Errorf("Error adding file from floppy_files : %s : %s", filename, err))
				return multistep.ActionHalt
			}

			for _, crawlfilename := range crawlDirectoryFiles {
				s.Add(cache, crawlfilename)
				s.FilesAdded[crawlfilename] = true
			}

			crawlDirectoryFiles = []string{}
			continue
		}

		// add just a single file
		ui.Message(fmt.Sprintf("Copying file: %s", filename))
		s.Add(cache, filename)
		s.FilesAdded[filename] = true
	}
	ui.Message("Done copying files from floppy_files")

	// Collect all paths (expanding wildcards) into pathqueue
	ui.Message("Collecting paths from floppy_dirs")
	var pathqueue []string
	for _, filename := range s.Directories {
		if strings.IndexAny(filename, "*?[") >= 0 {
			matches, err := filepath.Glob(filename)
			if err != nil {
				state.Put("error", fmt.Errorf("Error adding path %s to floppy: %s", filename, err))
				return multistep.ActionHalt
			}

			for _, filename := range matches {
				pathqueue = append(pathqueue, filename)
			}
			continue
		}
		pathqueue = append(pathqueue, filename)
	}
	ui.Message(fmt.Sprintf("Resulting paths from floppy_dirs : %v", pathqueue))

	// Go over each path in pathqueue and copy it.
	for _, src := range pathqueue {
		ui.Message(fmt.Sprintf("Recursively copying : %s", src))
		err = s.Add(cache, src)
		if err != nil {
			state.Put("error", fmt.Errorf("Error adding path %s to floppy: %s", src, err))
			return multistep.ActionHalt
		}
	}
	ui.Message("Done copying paths from floppy_dirs")

	// Set the path to the floppy so it can be used later
	state.Put("floppy_path", s.floppyPath)

	return multistep.ActionContinue
}