MCPcopy Create free account
hub / github.com/1Panel-dev/1Panel / operateSwapWithFile

Function operateSwapWithFile

agent/app/service/device.go:435–464  ·  view source on GitHub ↗
(delete bool, req dto.SwapHelper)

Source from the content-addressed store, hash-verified

433}
434
435func operateSwapWithFile(delete bool, req dto.SwapHelper) error {
436 conf, err := os.ReadFile(defaultFstab)
437 if err != nil {
438 return fmt.Errorf("read file %s failed, err: %v", defaultFstab, err)
439 }
440 lines := strings.Split(string(conf), "\n")
441 newFile := ""
442 for _, line := range lines {
443 if len(line) == 0 {
444 continue
445 }
446 parts := strings.Fields(line)
447 if len(parts) == 6 && parts[0] == req.Path {
448 continue
449 }
450 newFile += line + "\n"
451 }
452 if !delete {
453 newFile += fmt.Sprintf("%s swap swap defaults 0 0\n", req.Path)
454 }
455 file, err := os.OpenFile(defaultFstab, os.O_WRONLY|os.O_TRUNC, 0640)
456 if err != nil {
457 return err
458 }
459 defer file.Close()
460 write := bufio.NewWriter(file)
461 _, _ = write.WriteString(newFile)
462 write.Flush()
463 return nil
464}

Callers 1

UpdateSwapMethod · 0.85

Calls 3

OpenFileMethod · 0.80
CloseMethod · 0.65
FlushMethod · 0.45

Tested by

no test coverage detected