handleAutoAccepts handles adding and sharing folders for devices that have AutoAcceptFolders set to true.
(deviceID protocol.DeviceID, folder protocol.Folder, ccDeviceInfos *clusterConfigDeviceInfo, cfg config.FolderConfiguration, haveCfg bool, defaultFolderCfg config.FolderConfiguration)
| 1763 | // handleAutoAccepts handles adding and sharing folders for devices that have |
| 1764 | // AutoAcceptFolders set to true. |
| 1765 | func (m *model) handleAutoAccepts(deviceID protocol.DeviceID, folder protocol.Folder, ccDeviceInfos *clusterConfigDeviceInfo, cfg config.FolderConfiguration, haveCfg bool, defaultFolderCfg config.FolderConfiguration) (config.FolderConfiguration, bool) { |
| 1766 | if !haveCfg { |
| 1767 | defaultPathFs := fs.NewFilesystem(defaultFolderCfg.FilesystemType.ToFS(), defaultFolderCfg.Path) |
| 1768 | var pathAlternatives []string |
| 1769 | if alt := fs.SanitizePath(folder.Label); alt != "" { |
| 1770 | pathAlternatives = append(pathAlternatives, alt) |
| 1771 | } |
| 1772 | if alt := fs.SanitizePath(folder.ID); alt != "" { |
| 1773 | pathAlternatives = append(pathAlternatives, alt) |
| 1774 | } |
| 1775 | if len(pathAlternatives) == 0 { |
| 1776 | slog.Error("Failed to auto-accept folder due to lack of path alternatives", folder.LogAttr(), deviceID.LogAttr()) |
| 1777 | return config.FolderConfiguration{}, false |
| 1778 | } |
| 1779 | for _, path := range pathAlternatives { |
| 1780 | // Make sure the folder path doesn't already exist. |
| 1781 | if _, err := defaultPathFs.Lstat(path); !fs.IsNotExist(err) { |
| 1782 | continue |
| 1783 | } |
| 1784 | |
| 1785 | // Attempt to create it to make sure it does, now. |
| 1786 | fullPath := filepath.Join(defaultFolderCfg.Path, path) |
| 1787 | if err := defaultPathFs.MkdirAll(path, 0o700); err != nil { |
| 1788 | slog.Error("Failed to create path for auto-accepted folder", folder.LogAttr(), slogutil.FilePath(fullPath), slogutil.Error(err)) |
| 1789 | continue |
| 1790 | } |
| 1791 | |
| 1792 | fcfg := newFolderConfiguration(m.cfg, folder.ID, folder.Label, defaultFolderCfg.FilesystemType, fullPath) |
| 1793 | fcfg.Devices = append(fcfg.Devices, config.FolderDeviceConfiguration{ |
| 1794 | DeviceID: deviceID, |
| 1795 | }) |
| 1796 | |
| 1797 | if len(ccDeviceInfos.remote.EncryptionPasswordToken) > 0 || len(ccDeviceInfos.local.EncryptionPasswordToken) > 0 { |
| 1798 | fcfg.Type = config.FolderTypeReceiveEncrypted |
| 1799 | // Override the user-configured defaults, as normally done by the GUI |
| 1800 | fcfg.FSWatcherEnabled = false |
| 1801 | if fcfg.RescanIntervalS != 0 { |
| 1802 | minRescanInterval := 3600 * 24 |
| 1803 | if fcfg.RescanIntervalS < minRescanInterval { |
| 1804 | fcfg.RescanIntervalS = minRescanInterval |
| 1805 | } |
| 1806 | } |
| 1807 | fcfg.Versioning.Reset() |
| 1808 | // Other necessary settings are ensured by FolderConfiguration itself |
| 1809 | } else { |
| 1810 | ignores := m.cfg.DefaultIgnores() |
| 1811 | if err := m.setIgnores(fcfg, ignores.Lines); err != nil { |
| 1812 | slog.Error("Failed to apply default ignores to auto-accepted folder", folder.LogAttr(), slogutil.FilePath(fullPath), slogutil.Error(err)) |
| 1813 | } |
| 1814 | } |
| 1815 | |
| 1816 | slog.Info("Auto-accepted folder", fcfg.LogAttr(), slogutil.FilePath(fcfg.Path)) |
| 1817 | return fcfg, true |
| 1818 | } |
| 1819 | slog.Error("Failed to auto-accept folder due to path conflict", folder.LogAttr(), deviceID.LogAttr()) |
| 1820 | return config.FolderConfiguration{}, false |
| 1821 | } else { |
| 1822 | if slices.Contains(cfg.DeviceIDs(), deviceID) { |
no test coverage detected