Only use index when ranging over slices of structs

This commit is contained in:
2026-02-09 21:28:26 -05:00
parent 7ecd143b81
commit 273e86d97e
4 changed files with 20 additions and 20 deletions
+3 -3
View File
@@ -119,10 +119,10 @@ func main() {
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
return
}
for _, deletion := range deletionsList {
affectedIDVanityPath := strings.Split(deletion.Name(), global.FSSpace)
for i := range deletionsList {
affectedIDVanityPath := strings.Split(deletionsList[i].Name(), global.FSSpace)
if affectedIDVanityPath[0] == *registerReq.OldDeviceID {
if err = os.Rename(deletionsDirRoot+deletion.Name(), deletionsDirRoot+registerReq.NewDeviceID+global.FSSpace+affectedIDVanityPath[1]+global.FSSpace+affectedIDVanityPath[2]); err != nil {
if err = os.Rename(deletionsDirRoot+deletionsList[i].Name(), deletionsDirRoot+registerReq.NewDeviceID+global.FSSpace+affectedIDVanityPath[1]+global.FSSpace+affectedIDVanityPath[2]); err != nil {
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
return
}
+10 -10
View File
@@ -364,16 +364,16 @@ func syncLists(sshClient *ssh.Client, sshEntryRoot, sshAgeDir string, sshIsWindo
// deletionSync removes entries from the client that have been deleted on the server (multi-client deletion).
func deletionSync(deletions []synccommon.Deletion) error {
var entryDeleted bool
for _, deletion := range deletions {
if !deletion.IsAgeFile {
for i := range deletions {
if !deletions[i].IsAgeFile {
entryDeleted = true // set a flag to indicate that at least one entry has been deleted (used to determine whether to print a gap between deletion and other messages)
fmt.Println(synccommon.AnsiDelete+deletion.VanityPath+back.AnsiReset, "has been sheared, removing locally (if it exists)")
fmt.Println(synccommon.AnsiDelete+deletions[i].VanityPath+back.AnsiReset, "has been sheared, removing locally (if it exists)")
}
if err := os.RemoveAll(global.GetRealPath(deletion.VanityPath)); err != nil {
if !deletion.IsAgeFile {
return errors.New("unable to shear " + deletion.VanityPath + " locally: " + err.Error())
if err := os.RemoveAll(global.GetRealPath(deletions[i].VanityPath)); err != nil {
if !deletions[i].IsAgeFile {
return errors.New("unable to shear " + deletions[i].VanityPath + " locally: " + err.Error())
}
return errors.New("unable to shear age file for " + deletion.VanityPath + " locally: " + err.Error())
return errors.New("unable to shear age file for " + deletions[i].VanityPath + " locally: " + err.Error())
}
}
if entryDeleted {
@@ -429,9 +429,9 @@ func RunJob() (*syncListsT, error) {
}
// add deletions info to sync lists
for _, deletion := range deletions {
if !deletion.IsAgeFile {
syncListsV.Delete = append(syncListsV.Delete, deletion.VanityPath)
for i := range deletions {
if !deletions[i].IsAgeFile {
syncListsV.Delete = append(syncListsV.Delete, deletions[i].VanityPath)
}
}
+4 -4
View File
@@ -95,17 +95,17 @@ func ShearLocal(vanityPath, clientDeviceID string, onlyShearAgeFile bool) (strin
// add the sheared vanityPath to the deletions list (if running on a server)
if onServer {
for _, device := range deviceIDList {
if device.Name() != clientDeviceID {
for i := range deviceIDList {
if deviceIDList[i].Name() != clientDeviceID {
if !onlyShearAgeFile {
f, err := os.OpenFile(global.CfgDir+global.PathSeparator+"deletions"+global.PathSeparator+device.Name()+global.FSSpace+"entry"+global.FSSpace+strings.ReplaceAll(vanityPath, "/", global.FSPath), os.O_CREATE|os.O_WRONLY, 0600)
f, err := os.OpenFile(global.CfgDir+global.PathSeparator+"deletions"+global.PathSeparator+deviceIDList[i].Name()+global.FSSpace+"entry"+global.FSSpace+strings.ReplaceAll(vanityPath, "/", global.FSPath), os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
// failure to add the target to the deletions list will exit the program and result in a client re-uploading the target (non-critical)
return "", false, err
}
_ = f.Close() // error ignored; if the file could be created, it can probably be closed
}
f, err := os.OpenFile(global.CfgDir+global.PathSeparator+"deletions"+global.PathSeparator+device.Name()+global.FSSpace+"age"+global.FSSpace+strings.ReplaceAll(vanityPath, "/", global.FSPath), os.O_CREATE|os.O_WRONLY, 0600)
f, err := os.OpenFile(global.CfgDir+global.PathSeparator+"deletions"+global.PathSeparator+deviceIDList[i].Name()+global.FSSpace+"age"+global.FSSpace+strings.ReplaceAll(vanityPath, "/", global.FSPath), os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
// failure to add the target to the deletions list will exit the program and result in a client re-uploading the target (non-critical)
return "", false, err
+3 -3
View File
@@ -31,9 +31,9 @@ func GetRemoteDataFromServer(clientDeviceID string) {
//// server time
fetchResp.ServerTime = time.Now().Unix()
//// deletions
for _, deletion := range deletionsList {
for i := range deletionsList {
// perform deletion if it is relevant to the current client device
affectedIDVanityPath := strings.Split(deletion.Name(), global.FSSpace)
affectedIDVanityPath := strings.Split(deletionsList[i].Name(), global.FSSpace)
if affectedIDVanityPath[0] == clientDeviceID {
var isAgeFile bool
if affectedIDVanityPath[1] == "age" {
@@ -42,7 +42,7 @@ func GetRemoteDataFromServer(clientDeviceID string) {
fetchResp.Deletions = append(fetchResp.Deletions, synccommon.Deletion{VanityPath: strings.ReplaceAll(affectedIDVanityPath[2], global.FSPath, "/"), IsAgeFile: isAgeFile})
// assume successful client deletion and remove deletions file (if assumption is somehow false, worst case scenario is that the client will re-upload the deleted entry)
if err = os.RemoveAll(global.CfgDir + global.PathSeparator + "deletions" + global.PathSeparator + deletion.Name()); err != nil {
if err = os.RemoveAll(global.CfgDir + global.PathSeparator + "deletions" + global.PathSeparator + deletionsList[i].Name()); err != nil {
fmt.Printf("{\"errMsg\":\"%s\"}", err.Error())
return
}