Exemplo n.º 1
0
func put(log *logging.Logger, section *common.Section, cfg *ini.File, cmd *common.JSONMessage) {
	var conn net.Conn
	var err error

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

	cmd.Send(conn)

	if cmd.Command.Element.Type == "file" {
		transfered := dataset.Path(cfg, section, false) + string(filepath.Separator) + cmd.Command.Element.Name
		if err := common.SendFile(transfered, conn); err != nil {
			log.Debug("Error when sending file: ", err.Error())
		}

	}
}
Exemplo n.º 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)
				}
			}
		}
	}
}