<commit_before>// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build windows

// Package enforcement implements filesystem watching for configured required updates.
package enforcement

import (
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"

	"github.com/google/cabbie/cablib"

	"gopkg.in/fsnotify.v1"
	"golang.org/x/sys/windows/svc/debug"
	"github.com/google/glazier/go/helpers"
)

var (
	elog debug.Log

	errFileType    = errors.New("file is not json")
	errInvalidFile = errors.New("file path is invalid")
	errParsing     = errors.New("could not parse file content")
)

const enforceDir = `C:\ProgramData\Cabbie`

// Enforcements track any externally configured update enforcements.
type Enforcements struct {
	Required []string `json:"required"`
	Hidden   []string `json:"hidden"`
}

func enforcements(path string) (Enforcements, error) {
	var e Enforcements
	path = filepath.Clean(path)
	if filepath.Ext(path) != ".json" {
		return e, fmt.Errorf("%w: %q", errFileType, path)
	}
	b, err := helpers.PathExists(path)
	if err != nil {
		return e, fmt.Errorf("error determining %q existence: %v", path, err)
	}
	if !b {
		return e, fmt.Errorf("%w: %q", errInvalidFile, path)
	}
	data, err := ioutil.ReadFile(path)
	if err != nil {
		return e, fmt.Errorf("error reading file %q: %v", path, err)
	}
	if err := json.Unmarshal(data, &e); err != nil {<commit_msg>Use ProgramData environment variable to make enforcement file location more flexible.<commit_after>// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// +build windows

// Package enforcement implements filesystem watching for configured required updates.
package enforcement

import (
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"

	"github.com/google/cabbie/cablib"

	"gopkg.in/fsnotify.v1"
	"golang.org/x/sys/windows/svc/debug"
	"github.com/google/glazier/go/helpers"
)

var (
	elog debug.Log

	errFileType    = errors.New("file is not json")
	errInvalidFile = errors.New("file path is invalid")
	errParsing     = errors.New("could not parse file content")

	enforceDir = filepath.Join(os.Getenv("ProgramData"), `\Cabbie`)
)

// Enforcements track any externally configured update enforcements.
type Enforcements struct {
	Required []string `json:"required"`
	Hidden   []string `json:"hidden"`
}

func enforcements(path string) (Enforcements, error) {
	var e Enforcements
	path = filepath.Clean(path)
	if filepath.Ext(path) != ".json" {
		return e, fmt.Errorf("%w: %q", errFileType, path)
	}
	b, err := helpers.PathExists(path)
	if err != nil {
		return e, fmt.Errorf("error determining %q existence: %v", path, err)
	}
	if !b {
		return e, fmt.Errorf("%w: %q", errInvalidFile, path)
	}
	data, err := ioutil.ReadFile(path)
	if err != nil {
		return e, fmt.Errorf("error reading file %q: %v", path, err)
	}
	if err := json.Unmarshal(data, &e); err != nil {
{'fraction_non_alphanumeric': 0.12671059300557527, 'fraction_numerical': 0.00456158134820071, 'mean_word_length': 3.9214463840399003, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '4907624', 'n_tokens_mistral': 1280, 'n_tokens_neox': 1242, 'n_words': 426}