Exemple #1
0
func Put(log *logging.Logger, conn net.Conn, command *common.JSONCommand) {
	var res common.JSONResult

	if _, err := os.Stat(command.Element.Name); err == nil || os.IsExist(err) {
		os.Rename(command.Element.Name, command.Element.Name+"."+time.Now().String())
	}

	switch command.Element.Type {
	case "directory":
		os.Mkdir(command.Element.Name, 0755)
		res = fsSetAttrs(log, command)
	case "file":
		if hash, err := common.ReceiveFile(command.Element.Name, conn); hash != command.Element.Hash {
			log.Debug("Error: hash mismatch")
			res = common.JSONResult{Result: "ko", Message: "Hash mismatch"}
		} else if err != nil {
			log.Debug("Error:", err)
			res = common.JSONResult{Result: "ko", Message: "Error: " + err.Error()}
		} else {
			res = fsSetAttrs(log, command)
		}
	case "symlink":
		if err := os.Symlink(command.Element.Link, command.Element.Name); err != nil {
			log.Debug("Error:", err)
			res = common.JSONResult{Result: "ko", Message: "Error: " + err.Error()}
		} else {
			res = common.JSONResult{Result: "ok"}
		}
	}

	res.Send(conn)
}
Exemple #2
0
func fsSaveData(log *logging.Logger, cfg *ini.File, section *common.Section, data common.JSONFile, previous bool) {
	var item, source, dest, hash string
	var cmd common.JSONMessage
	var conn net.Conn
	var err error

	item = dataset.ConvertPath(data.Os, data.Name)
	source = dataset.Path(cfg, section, true) + string(filepath.Separator) + item
	dest = dataset.Path(cfg, section, false) + string(filepath.Separator) + item

	log.Debug("Save item: " + dest)

	switch data.Type {
	case "directory":
		os.MkdirAll(dest, 0755)
	case "symlink":
		if err := os.Symlink(data.Link, dest); err != nil {
			log.Error("Error when creating symlink for file %s", data.Name)
			log.Debug("Trace: %s", err.Error())
		}
	case "file":
		if previous {
			if section.Compressed {
				source = source + ".compressed"
				dest = dest + ".compressed"
			}

			if err = os.Link(source, dest); err != nil {
				log.Error("Error when link file %s", data.Name)
				log.Debug("Trace: " + err.Error())
			}
		} else {
			cmd.Context = "file"
			cmd.Command.Name = "get"
			cmd.Command.Element = data

			conn, err = network.GetSocket(cfg.Section(section.Name))
			if err != nil {
				log.Error("Error when opening connection with section " + section.Name)
				log.Debug("Trace: " + err.Error())
				return
			}
			defer conn.Close()

			cmd.Send(conn)

			if hash, err = common.ReceiveFile(dest, conn); err != nil {
				log.Error("Error when receiving file " + data.Name)
				log.Debug("Trace: " + err.Error())
			}

			// TODO: check file's hash
			if hash == "" {
				log.Error("Hash for file " + dest + " mismatch")
				// TODO: remove file if hash mismatch
			} else {
				log.Debug("Hash for file " + dest + " is " + hash)

				if section.Compressed {
					dataset.CompressFile(log, dest)
				}
			}
		}
	}
}