Пример #1
0
func runFileChmod(base_path, path string) bool {
	listener := utils.GetListenerFromDir(base_path)
	rel_path := utils.GetRelativePath(listener, path)
	dbItem, err := datastore.GetOne(base_path, rel_path)
	if err != nil {
		log.Errorf("Error occurred trying to get %s from DB\nError: %s", rel_path, err.Error())
		return false
	}
	fsItem, err := utils.GetFileInfo(path)
	if err != nil {
		log.Errorf("Could not find item on filesystem: %s\nError:%s", path, err.Error())
	}
	if dbItem.Perms != fsItem.Perms {
		iPerm, _ := strconv.Atoi(dbItem.Perms)
		mode := int(iPerm)
		if _, err := os.Stat(path); os.IsNotExist(err) {
			log.Infof("File no longer exists returning")
			return true
		} else {
			err := os.Chmod(path, os.FileMode(mode))
			if err != nil {
				log.Errorf("Error occurred changing file modes: %s", err.Error())
				return false
			}
			return true
		}
	} else {
		log.Info("File modes are correct changing nothing")
		return false
	}
	return true
}
Пример #2
0
func GetNodeCopy(item utils.DataTable, listener string, uid, gid int, perms string) bool {
	cfg := utils.GetConfig()
	aliveNodes := nodeinfo.GetNodes()
	for _, node := range aliveNodes {
		log.Infof("Trying download from: %s", node.NodeIPs)
		nIPs := strings.Split(node.NodeIPs, ",")
		for _, ipAddress := range nIPs {
			resp, err := getData(ipAddress, cfg.ServerConfig.ListenPort, listener, utils.GetRelativePath(listener, item.Path))
			if err == nil {
				defer resp.Body.Close()
				if resp.Status == "404" {
					log.Infof("File not found: %s", item.Path)
					return false
				}
				size, err := utils.FileWrite(item.Path, resp.Body, uid, gid, perms)
				if err != nil {
					log.Infof("Cannot write file: %s", item.Path)
					return false
				} else {
					log.Infof("%s with %v bytes downloaded", item.Path, size)
					return true
				}
				return false
			} else {
				return false
			}
		}
	}
	return false
}
Пример #3
0
func runFileRemove(base_path, path string) bool {
	listener := utils.GetListenerFromDir(base_path)
	rel_path := utils.GetRelativePath(listener, path)
	// Remove the item from the database (it's already removed from filesystem
	datastore.Remove(listener, rel_path)
	// Remove the item from the backup storage location
	storage.RemoveFile(rel_path, listener)
	return true
}
Пример #4
0
func runFileCreateUpdate(base_path, path, operation string) bool {
	listener := utils.GetListenerFromDir(base_path)
	rel_path := utils.GetRelativePath(listener, path)
	fsItem, err := utils.GetFileInfo(path)

	if err != nil {
		log.Infof("Error getting file details for %s: %+v", path, err)
	}

	log.Infof("Putting in storage:-> %s", rel_path)
	storage.PutFile(path, listener)
	log.Infof("Creating/Updating:-> %s", rel_path)
	return datastore.Insert(listener, fsItem)
}
Пример #5
0
func PathExists(listener, path string) bool {
	setdbstoreEngine()
	return dbstore.PathExists(listener, utils.GetRelativePath(listener, path))
}
Пример #6
0
func SysPathWatcher(path string) {
	log.Infof("Starting new watcher for %s:", path)
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		log.Criticalf("Cannot create new watcher for %s\n %s", path, err.Error())
	}
	defer watcher.Close()
	done := make(chan bool)
	go func() {
		listener := utils.GetListenerFromDir(path)
		rel_path := utils.GetRelativePath(listener, path)
		for {
			select {
			case event := <-watcher.Events:
				//logs.WriteLn("event:", event)
				if event.Op&fsnotify.Chmod == fsnotify.Chmod {
					if !utils.MatchesIgnore(path, event.Name) {
						runFileChmod(path, event.Name)
					}
				}
				if event.Op&fsnotify.Rename == fsnotify.Rename {

					if !utils.MatchesIgnore(path, event.Name) {
						if checksumItem(path, rel_path, event.Name) {
							log.Infof("Rename occurred on:", event.Name)
							runFileRename(path, event.Name)
						}
					}

				}
				if (event.Op&fsnotify.Create == fsnotify.Create) || (event.Op&fsnotify.Write == fsnotify.Write) {

					if checksumItem(path, rel_path, event.Name) {
						if !utils.MatchesIgnore(path, event.Name) {
							log.Infof("New / Modified File: %s", event.Name)
							runFileCreateUpdate(path, event.Name, "create")
						}

					}
				}

				if event.Op&fsnotify.Remove == fsnotify.Remove {
					if !utils.MatchesIgnore(path, event.Name) {
						runFileRemove(path, event.Name)
						log.Infof("Removed File: %s", event.Name)
					}

				}
			case err := <-watcher.Errors:
				log.Errorf("error:", err)
			}
		}

	}()
	err = watcher.Add(path)
	if err != nil {
		log.Criticalf("Cannot add watcher to %s\n %s", path, err.Error())
	}
	<-done

}