<commit_before>/*
 * Minio Client (C) 2014, 2015 Minio, Inc.
 *
 * 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.
 */

package main

import (
	"encoding/json"
	"os"
	"path/filepath"

	"github.com/minio/minio/pkg/probe"
)

func newSharedURLs() *sharedURLsV2 {
	return newSharedURLsV2()
}

func migrateSharedURLsV1ToV2() {
	if !isSharedURLsDataFileExists() {
		return
	}
	sURLsV2 := newSharedURLsV2()

	// try to load latest version if possible
	sURLsV2, err := loadSharedURLsV2()
	if err != nil {
		switch err.ToGoError().(type) {
		case *json.UnmarshalTypeError:
			// try to load V1 if possible
			var sURLsV1 *sharedURLsV1
			sURLsV1, err = loadSharedURLsV1()
			fatalIf(err.Trace(), "Unable to load shared url version ‘1.0.0’.")
			if sURLsV1.Version != "1.0.0" {
				fatalIf(errDummy().Trace(), "Invalid version loaded ‘"+sURLsV1.Version+"’.")
			}
			for key, value := range sURLsV1.URLs {
				value.Message.Key = key
				sURLsV2.URLs = append(sURLsV2.URLs, value)
			}
			err = saveSharedURLsV2(sURLsV2)
			fatalIf(err.Trace(), "Unable to save new shared url version ‘1.1.0’.")
		default:
			fatalIf(err.Trace(), "Unable to load shared url version ‘1.1.0’.")
		}
	}
}

func getSharedURLsDataDir() (string, *probe.Error) {
	configDir, err := getMcConfigDir()
	if err != nil {
		return "", err.Trace()
	}

	sharedURLsDataDir := filepath.Join(configDir, globalSharedURLsDataDir)
	return sharedURLsDataDir, nil
}

func isSharedURLsDataDirExists() bool {
	shareDir, err := getSharedURLsDataDir()
	fatalIf(err.Trace(), "Unable to determine share folder.")<commit_msg>Fix a null pointer bug in share URL migration<commit_after>/*
 * Minio Client (C) 2014, 2015 Minio, Inc.
 *
 * 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.
 */

package main

import (
	"encoding/json"
	"os"
	"path/filepath"

	"github.com/minio/minio/pkg/probe"
)

func newSharedURLs() *sharedURLsV2 {
	return newSharedURLsV2()
}

func migrateSharedURLsV1ToV2() {
	if !isSharedURLsDataFileExists() {
		return
	}

	// try to load latest version if possible
	sURLsV2, err := loadSharedURLsV2()
	if err != nil {
		switch err.ToGoError().(type) {
		case *json.UnmarshalTypeError:
			// try to load V1 if possible
			var sURLsV1 *sharedURLsV1
			sURLsV1, err = loadSharedURLsV1()
			fatalIf(err.Trace(), "Unable to load shared url version ‘1.0.0’.")
			if sURLsV1.Version != "1.0.0" {
				fatalIf(errDummy().Trace(), "Invalid version loaded ‘"+sURLsV1.Version+"’.")
			}
			sURLsV2 = newSharedURLsV2()
			for key, value := range sURLsV1.URLs {
				value.Message.Key = key
				sURLsV2.URLs = append(sURLsV2.URLs, value)
			}
			err = saveSharedURLsV2(sURLsV2)
			fatalIf(err.Trace(), "Unable to save new shared url version ‘1.1.0’.")
		default:
			fatalIf(err.Trace(), "Unable to load shared url version ‘1.1.0’.")
		}
	}
}

func getSharedURLsDataDir() (string, *probe.Error) {
	configDir, err := getMcConfigDir()
	if err != nil {
		return "", err.Trace()
	}

	sharedURLsDataDir := filepath.Join(configDir, globalSharedURLsDataDir)
	return sharedURLsDataDir, nil
}

func isSharedURLsDataDirExists() bool {
	shareDir, err := getSharedURLsDataDir()
	fatalIf(err.Trace(), "Unable to determine share folder.")
{'fraction_non_alphanumeric': 0.11850610485994734, 'fraction_numerical': 0.02106775197510175, 'mean_word_length': 3.90951821386604, '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': '3556989', 'n_tokens_mistral': 1541, 'n_tokens_neox': 1426, 'n_words': 427}