Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e95e7ad

Browse filesBrowse files
committed
fix: cleanup additional temporary directories
1 parent a2f79df commit e95e7ad
Copy full SHA for e95e7ad

File tree

5 files changed

+24
-19
lines changed
Filter options

5 files changed

+24
-19
lines changed

‎commands/local_server_start.go

Copy file name to clipboardExpand all lines: commands/local_server_start.go
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@ var localServerStartCmd = &console.Command{
421421
if p.PHPServer != nil {
422422
<-p.PHPServer.StoppedChan
423423
}
424+
pidFile.CleanupDirectories()
424425
ui.Success("Stopped all processes successfully")
425426
}
426427
return nil

‎local/php/fpm.go

Copy file name to clipboardExpand all lines: local/php/fpm.go
+1-5Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,5 @@ env['LC_ALL'] = C
114114
}
115115

116116
func (p *Server) fpmConfigFile() string {
117-
path := filepath.Join(p.homeDir, fmt.Sprintf("php/%s/fpm-%s.ini", name(p.projectDir), p.Version.Version))
118-
if _, err := os.Stat(filepath.Dir(path)); os.IsNotExist(err) {
119-
_ = os.MkdirAll(filepath.Dir(path), 0755)
120-
}
121-
return path
117+
return filepath.Join(p.tempDir, fmt.Sprintf("fpm-%s.ini", p.Version.Version))
122118
}

‎local/php/php_builtin_server.go

Copy file name to clipboardExpand all lines: local/php/php_builtin_server.go
+1-6Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ package php
2121

2222
import (
2323
"fmt"
24-
"os"
2524
"path/filepath"
2625
)
2726

@@ -61,9 +60,5 @@ require $script;
6160
`)
6261

6362
func (p *Server) phpRouterFile() string {
64-
path := filepath.Join(p.homeDir, fmt.Sprintf("php/%s-router.php", name(p.projectDir)))
65-
if _, err := os.Stat(filepath.Dir(path)); os.IsNotExist(err) {
66-
_ = os.MkdirAll(filepath.Dir(path), 0755)
67-
}
68-
return path
63+
return filepath.Join(p.tempDir, fmt.Sprintf("%s-router.php", p.Version.Version))
6964
}

‎local/php/php_server.go

Copy file name to clipboardExpand all lines: local/php/php_server.go
+8-8Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type Server struct {
5050
logger zerolog.Logger
5151
StoppedChan chan bool
5252
appVersion string
53-
homeDir string
53+
tempDir string
5454
projectDir string
5555
documentRoot string
5656
passthru string
@@ -76,7 +76,6 @@ func NewServer(homeDir, projectDir, documentRoot, passthru, appVersion string, l
7676
Version: version,
7777
logger: logger.With().Str("source", "PHP").Str("php", version.Version).Str("path", version.ServerPath()).Logger(),
7878
appVersion: appVersion,
79-
homeDir: homeDir,
8079
projectDir: projectDir,
8180
documentRoot: documentRoot,
8281
passthru: passthru,
@@ -86,7 +85,13 @@ func NewServer(homeDir, projectDir, documentRoot, passthru, appVersion string, l
8685

8786
// Start starts a PHP server
8887
func (p *Server) Start(ctx context.Context, pidFile *pid.PidFile) (*pid.PidFile, func() error, error) {
89-
var pathsToRemove []string
88+
p.tempDir = pidFile.TempDirectory()
89+
if _, err := os.Stat(p.tempDir); os.IsNotExist(err) {
90+
if err = os.MkdirAll(p.tempDir, 0755); err != nil {
91+
return nil, nil, err
92+
}
93+
}
94+
9095
port, err := process.FindAvailablePort()
9196
if err != nil {
9297
p.logger.Debug().Err(err).Msg("unable to find an available port")
@@ -125,7 +130,6 @@ func (p *Server) Start(ctx context.Context, pidFile *pid.PidFile) (*pid.PidFile,
125130
return nil, nil, errors.WithStack(err)
126131
}
127132
p.proxy.Transport = &cgiTransport{}
128-
pathsToRemove = append(pathsToRemove, fpmConfigFile)
129133
binName = "php-fpm"
130134
workerName = "PHP-FPM"
131135
args = []string{p.Version.ServerPath(), "--nodaemonize", "--fpm-config", fpmConfigFile}
@@ -151,7 +155,6 @@ func (p *Server) Start(ctx context.Context, pidFile *pid.PidFile) (*pid.PidFile,
151155
if err := os.WriteFile(routerPath, phprouter, 0644); err != nil {
152156
return nil, nil, errors.WithStack(err)
153157
}
154-
pathsToRemove = append(pathsToRemove, routerPath)
155158
binName = "php"
156159
workerName = "PHP"
157160
args = []string{p.Version.ServerPath(), "-S", "127.0.0.1:" + strconv.Itoa(port), "-d", "variables_order=EGPCS", routerPath}
@@ -194,9 +197,6 @@ func (p *Server) Start(ctx context.Context, pidFile *pid.PidFile) (*pid.PidFile,
194197

195198
return phpPidFile, func() error {
196199
defer func() {
197-
for _, path := range pathsToRemove {
198-
os.RemoveAll(path)
199-
}
200200
e.CleanupTemporaryDirectories()
201201
p.StoppedChan <- true
202202
}()

‎local/pid/pidfile.go

Copy file name to clipboardExpand all lines: local/pid/pidfile.go
+13Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,19 @@ func (p *PidFile) WorkerPidDir() string {
232232
return filepath.Join(util.GetHomeDir(), "var", name(p.Dir))
233233
}
234234

235+
func (p *PidFile) TempDirectory() string {
236+
return filepath.Join(util.GetHomeDir(), "php", name(p.Dir))
237+
}
238+
239+
func (p *PidFile) CleanupDirectories() {
240+
os.RemoveAll(p.TempDirectory())
241+
// We don't want to force removal of log and pid files, we only want to
242+
// clean up empty directories. To do so we use `os.Remove` instead of
243+
// `os.RemoveAll`
244+
os.Remove(p.WorkerLogDir())
245+
os.Remove(p.WorkerPidDir())
246+
}
247+
235248
func (p *PidFile) LogReader() (io.ReadCloser, error) {
236249
logFile := p.LogFile()
237250
if err := os.MkdirAll(filepath.Dir(logFile), 0755); err != nil {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.