func (ss *StoreServer) createVolumeHandler(w http.ResponseWriter, r *http.Request) {
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	var volIDIP VolumeIDIP
	if err = json.Unmarshal(body, &volIDIP); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	id := volIDIP.ID
	volIDStr := fmt.Sprintf("%d", id)
	volPath := filepath.Join(ss.volumeDir, volIDStr+".vol")
	needleMapPath := filepath.Join(ss.volumeDir, fmt.Sprintf("needle_map_vol%d", id))
	file, err := os.OpenFile(volPath, os.O_RDWR|os.O_CREATE, 0644)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	v, err := storage.NewVolume(id, file, needleMapPath, ss.garbageThreshold)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	ss.localVolIDIPs = append(ss.localVolIDIPs, volIDIP)
	bytes, err := json.Marshal(ss.localVolIDIPs)
	if err = ioutil.WriteFile(filepath.Join(ss.volumeDir, "volIDIPs.json"), bytes, 0644); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
	ss.volumeMap[id] = v
}
Beispiel #2
0
func (ss *StoreServer) loadVolumes(volumeDir string) error {
	dirs, err := ioutil.ReadDir(volumeDir)
	if err != nil {
		return err
	}
	for _, dir := range dirs {
		volName := dir.Name()
		if !dir.IsDir() && strings.HasSuffix(volName, ".vol") {
			volPath := filepath.Join(ss.volumeDir, volName)
			file, err := os.OpenFile(volPath, os.O_RDWR|os.O_CREATE, 0644)
			if err != nil {
				return err
			}
			idStr := volName[:len(volName)-len(".vol")]
			id, err := newVolumeID(idStr)
			if err != nil {
				return err
			}
			needleMapPath := filepath.Join(ss.volumeDir, fmt.Sprintf("needle_map_vol%d", id))
			v, err := storage.NewVolume(id, file, needleMapPath, ss.garbageThreshold)
			if err != nil {
				return err
			}
			ss.volumeMap[id] = v
		}
	}
	return nil
}