<commit_before>// Copyright 2014 The jenkins-client-launcher Authors. All rights reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.

package environment

import (
	"os"
	"github.com/jkellerer/jenkins-client-launcher/launcher/util"
	"path/filepath"
	"time"
	"strings"
)

// Min time to persist entries inside the folders selected for cleanup.
var minCleanupTTL = time.Hour * 6

// The min interval when folders selected for cleanup are monitored.
var minMonitoringInterval = time.Hour * 2

// Defines an object which continuously watches and cleans the temporary directory of
// files that haven't been modified for maxTTLInTempDirectories (default 24 hours).
type LocationCleaner struct {
	util.AnyConfigAcceptor

	tickers []*time.Ticker
}

func NewLocationCleaner() *LocationCleaner {
	p := new(LocationCleaner)
	p.tickers = []*time.Ticker{}
	return p
}

func (self *LocationCleaner) Name() string {
	return "Directory Cleaner"
}

func (self *LocationCleaner) Prepare(config *util.Config) {
	for _, ticker := range self.tickers {
		ticker.Stop()
	}
	self.tickers = self.tickers[0:0]


	for _, setting := range config.Maintenance.CleanupSettingsList {
		if !setting.Enabled {
			continue
		}
		self.initializeLocation(setting)
	}<commit_msg>Fix/Cleanup: Fixed detection of workspace path by asking Jenkins instead of assuming current working directory.<commit_after>// Copyright 2014 The jenkins-client-launcher Authors. All rights reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.

package environment

import (
	"os"
	"github.com/jkellerer/jenkins-client-launcher/launcher/util"
	"path/filepath"
	"time"
	"strings"
	"fmt"
	"encoding/xml"
)

// Min time to persist entries inside the folders selected for cleanup.
var minCleanupTTL = time.Hour * 6

// The min interval when folders selected for cleanup are monitored.
var minMonitoringInterval = time.Hour * 2

type JenkinsNodeConfig struct {
	XMLName     xml.Name `xml:"slave"`
	Name        string   `xml:"name"`
	RemoteFS    string   `xml:"remoteFS"`
}

// Returns the current configuration of this Jenkins node from the Jenkins server.
func GetJenkinsNodeConfig(config *util.Config) (*JenkinsNodeConfig, error) {
	if response, err := config.CIGet(fmt.Sprintf("/computer/%s/config.xml", config.ClientName)); err == nil {
		defer response.Body.Close()
		if response.StatusCode == 200 {
			config := &JenkinsNodeConfig{}
			err = xml.NewDecoder(response.Body).Decode(config)
			return config, err
		} else {
			return nil, fmt.Errorf(response.Status)
		}
	} else {
		return nil, err
	}
}

// Defines an object which continuously watches and cleans the temporary directory of
// files that haven't been modified for maxTTLInTempDirectories (default 24 hours).
type LocationCleaner struct {
	util.AnyConfigAcceptor

	tickers       []*time.Ticker
	workspacePath string
}

func NewLocationCleaner() *LocationCleaner {
	p := new(LocationCleaner)
	p.tickers = []*time.Ticker{}
	return p
}

func (self *LocationCleaner) Name() string {
	return "Directory Cleaner"
}

func (self *LocationCleaner) Prepare(config *util.Config) {
	for _, ticker := range self.tickers {
		ticker.Stop()
	}

	self.tickers = self.tickers[0:0]
	self.workspacePath = self.getWorkspacePath(config)

	for _, setting := range config.Maintenance.CleanupSettingsList {
		if !setting.Enabled {
			continue
		}
		self.initializeLocation(setting)
	}
}

func (self *LocationCleaner) getWorkspacePath(config *util.Config) string {
	baseDir := ""
	if nodeConfig, err := GetJenkinsNodeConfig(config); err == nil && nodeConfig.RemoteFS != "" {
		baseDir = filepath.FromSlash(nodeConfig.RemoteFS)
	} else {
		baseDir, _ = os.Getwd()
	}
	return filepath.Join(baseDir, "workspace")
{'fraction_non_alphanumeric': 0.10334827313472185, 'fraction_numerical': 0.006063801740047456, 'mean_word_length': 4.306293706293706, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 7}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5193254', 'n_tokens_mistral': 1242, 'n_tokens_neox': 1162, 'n_words': 358}