|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "os" |
| 10 | + "os/exec" |
| 11 | + "path" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "github.com/adrg/xdg" |
| 15 | + "github.com/spf13/cobra" |
| 16 | +) |
| 17 | + |
| 18 | +// LogEntry represents a parsed journalctl log line |
| 19 | +type LogEntry struct { |
| 20 | + Message string `json:"MESSAGE"` |
| 21 | + Timestamp string `json:"__REALTIME_TIMESTAMP"` |
| 22 | + Priority string `json:"PRIORITY"` |
| 23 | + Unit string `json:"_SYSTEMD_UNIT"` |
| 24 | +} |
| 25 | + |
| 26 | +func journaldReader(cmd *cobra.Command, args []string) { |
| 27 | + ctx := cmd.Context() |
| 28 | + driveName := args[0] |
| 29 | + logs, errs := startJournalReader(ctx, driveName) |
| 30 | + |
| 31 | + go func() { |
| 32 | + for l := range logs { |
| 33 | + handleLogEntry(l, driveName) |
| 34 | + } |
| 35 | + }() |
| 36 | + |
| 37 | + go func() { |
| 38 | + for err := range errs { |
| 39 | + fmt.Printf("Error: %v\n", err) |
| 40 | + } |
| 41 | + }() |
| 42 | + |
| 43 | + <-ctx.Done() |
| 44 | +} |
| 45 | + |
| 46 | +func startJournalReader(ctx context.Context, name string) (<-chan LogEntry, <-chan error) { |
| 47 | + logs := make(chan LogEntry) |
| 48 | + errs := make(chan error, 1) |
| 49 | + |
| 50 | + cmd := exec.Command( |
| 51 | + "journalctl", |
| 52 | + "--output=json", |
| 53 | + "--follow", |
| 54 | + "--user", |
| 55 | + "--since=now", |
| 56 | + fmt.Sprintf("--unit=%s", driveNameToUnitName(name)), |
| 57 | + ) |
| 58 | + stdout, err := cmd.StdoutPipe() |
| 59 | + if err != nil { |
| 60 | + errs <- fmt.Errorf("failed to get stdout: %w", err) |
| 61 | + close(logs) |
| 62 | + close(errs) |
| 63 | + return logs, errs |
| 64 | + } |
| 65 | + |
| 66 | + if err := cmd.Start(); err != nil { |
| 67 | + errs <- fmt.Errorf("failed to start journalctl: %w", err) |
| 68 | + close(logs) |
| 69 | + close(errs) |
| 70 | + return logs, errs |
| 71 | + } |
| 72 | + |
| 73 | + go func() { |
| 74 | + defer close(logs) |
| 75 | + defer close(errs) |
| 76 | + |
| 77 | + scanner := bufio.NewScanner(stdout) |
| 78 | + for scanner.Scan() { |
| 79 | + select { |
| 80 | + case <-ctx.Done(): |
| 81 | + _ = cmd.Process.Kill() |
| 82 | + return |
| 83 | + default: |
| 84 | + var entry LogEntry |
| 85 | + line := scanner.Text() |
| 86 | + if err := json.Unmarshal([]byte(line), &entry); err != nil { |
| 87 | + errs <- fmt.Errorf("failed to parse log line: %w", err) |
| 88 | + continue |
| 89 | + } |
| 90 | + logs <- entry |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if err := scanner.Err(); err != nil && err != io.EOF { |
| 95 | + errs <- fmt.Errorf("scanner error: %w", err) |
| 96 | + } |
| 97 | + |
| 98 | + if err := cmd.Wait(); err != nil { |
| 99 | + errs <- fmt.Errorf("journalctl command error: %w", err) |
| 100 | + } |
| 101 | + }() |
| 102 | + |
| 103 | + return logs, errs |
| 104 | +} |
| 105 | + |
| 106 | +func handleLogEntry(entry LogEntry, driveName string) { |
| 107 | + if !strings.Contains(entry.Message, "insufficientParentPermissions") { |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + fileName := strings.TrimSpace(strings.SplitN(entry.Message, ":", 3)[1]) |
| 112 | + filePath := fileNameToPath(driveName, fileName) |
| 113 | + |
| 114 | + // make sure file still exists |
| 115 | + if _, err := os.Stat(filePath); err != nil { |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + title := fmt.Sprintf("Drive Error: %s", driveName) |
| 120 | + message := fmt.Sprintf(`You have insufficient permissions to write a file: |
| 121 | +
|
| 122 | +- %s |
| 123 | +
|
| 124 | +Make sure to move the file you just created to another location immediately! |
| 125 | + `, path.Join(getDriveDataPath(driveName), filePath)) |
| 126 | + if err := sendDesktopNotificationError(title, message); err != nil { |
| 127 | + fmt.Printf("Failed to send notification: %v\n", err) |
| 128 | + } |
| 129 | + |
| 130 | + // open file selector to select the file location |
| 131 | + title = "Select File Location" |
| 132 | + message = fmt.Sprintf("Select a new location for the file:\n\n%s", filePath) |
| 133 | + |
| 134 | + // open the file selector in the "overview" folder with all other drives |
| 135 | + suggestedPath := path.Join(xdg.Home, "google", path.Base(filePath)) |
| 136 | + fmt.Println(suggestedPath) |
| 137 | + newFilePath, err := openFileSelector(title, message, suggestedPath) |
| 138 | + if err != nil { |
| 139 | + if strings.Contains(err.Error(), "exit status 1") { |
| 140 | + fmt.Println("File selector was cancelled, skipping...") |
| 141 | + return |
| 142 | + } |
| 143 | + fmt.Printf("Failed to open file selector: %v\n", err) |
| 144 | + return |
| 145 | + } |
| 146 | + if newFilePath == "" { |
| 147 | + fmt.Println("No file selected, skipping...") |
| 148 | + return |
| 149 | + } |
| 150 | + if err := moveFile(filePath, newFilePath); err != nil { |
| 151 | + title = "Error Moving File" |
| 152 | + message = fmt.Sprintf("Failed to move file:\n\n%s", err) |
| 153 | + if err := sendDesktopNotificationError(title, message); err != nil { |
| 154 | + fmt.Printf("Failed to send notification: %v\n", err) |
| 155 | + } |
| 156 | + fmt.Printf("Failed to move file: %v\n", err) |
| 157 | + return |
| 158 | + } |
| 159 | + title = "File Moved" |
| 160 | + message = fmt.Sprintf("File moved to:\n\n%s", newFilePath) |
| 161 | + if err := sendDesktopNotificationInfo(title, message); err != nil { |
| 162 | + fmt.Printf("Failed to send notification: %v\n", err) |
| 163 | + } |
| 164 | + fmt.Printf("File %q moved to: %s\n", filePath, newFilePath) |
| 165 | +} |
| 166 | + |
| 167 | +// moveFile moves a file from oldPath to newPath by copying the file and removing the old file. |
| 168 | +// This is a workaround for the issue where os.Rename fails across different filesystems (and rclone mounts). |
| 169 | +func moveFile(src, dest string) error { |
| 170 | + in, err := os.Open(src) |
| 171 | + if err != nil { |
| 172 | + return err |
| 173 | + } |
| 174 | + defer in.Close() // nolint:errcheck |
| 175 | + |
| 176 | + info, err := in.Stat() |
| 177 | + if err != nil { |
| 178 | + return err |
| 179 | + } |
| 180 | + |
| 181 | + // create new file. This will overwrite the file if it exists |
| 182 | + out, err := os.OpenFile(dest, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, info.Mode()) |
| 183 | + if err != nil { |
| 184 | + if strings.Contains(err.Error(), "is a directory") { |
| 185 | + return fmt.Errorf("Destination %q is a directory.\n\nYou cant replace a file with a directory!", dest) // nolint:staticcheck |
| 186 | + } |
| 187 | + return err |
| 188 | + } |
| 189 | + defer out.Close() // nolint:errcheck |
| 190 | + |
| 191 | + _, err = io.Copy(out, in) |
| 192 | + if err != nil { |
| 193 | + return err |
| 194 | + } |
| 195 | + |
| 196 | + // Close the files before removing |
| 197 | + in.Close() // nolint:errcheck |
| 198 | + out.Close() // nolint:errcheck |
| 199 | + |
| 200 | + // Remove the original file |
| 201 | + if err = os.Remove(src); err != nil { |
| 202 | + return err |
| 203 | + } |
| 204 | + |
| 205 | + return nil |
| 206 | +} |
| 207 | + |
| 208 | +func sendDesktopNotificationError(title, message string) error { |
| 209 | + cmd := exec.Command("zenity", "--error", "--text", message, "--title", title) |
| 210 | + if err := cmd.Run(); err != nil { |
| 211 | + return fmt.Errorf("failed to send notification: %w", err) |
| 212 | + } |
| 213 | + return nil |
| 214 | +} |
| 215 | + |
| 216 | +func sendDesktopNotificationInfo(title, message string) error { |
| 217 | + cmd := exec.Command("zenity", "--info", "--text", message, "--title", title) |
| 218 | + if err := cmd.Run(); err != nil { |
| 219 | + return fmt.Errorf("failed to send notification: %w", err) |
| 220 | + } |
| 221 | + return nil |
| 222 | +} |
| 223 | + |
| 224 | +func openFileSelector(title, message, fileName string) (string, error) { |
| 225 | + cmd := exec.Command( |
| 226 | + "zenity", |
| 227 | + "--file-selection", |
| 228 | + "--save", |
| 229 | + "--confirm-overwrite", |
| 230 | + "--title", title, |
| 231 | + "--text", message, |
| 232 | + "--filename", fileName, |
| 233 | + ) |
| 234 | + output, err := cmd.Output() |
| 235 | + if err != nil { |
| 236 | + return "", fmt.Errorf("failed to open file selector: %w", err) |
| 237 | + } |
| 238 | + return strings.TrimSpace(string(output)), nil |
| 239 | +} |
0 commit comments