<commit_before>// Copyright 2015 Google Inc. All Rights Reserved.
//
// 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 drive

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"path"
	"path/filepath"
	"reflect"
	"regexp"
	"runtime"
	"strconv"
	"strings"
	"sync"
	"time"

	"google.golang.org/api/googleapi"

	expirableCache "github.com/odeke-em/cache"
	spinner "github.com/odeke-em/cli-spinner"
	"github.com/odeke-em/drive/config"
)

var (
	// ErrRejectedTerms is empty "" because messages might be too
	// verbose to affirm a rejection that a user has already seen
	ErrRejectedTerms = errors.New("")
)

const (
	MimeTypeJoiner      = "-"
	RemoteDriveRootPath = "My Drive"

	FmtTimeString           = "2006-01-02T15:04:05.000Z"
	MsgClashesFixedNowRetry = "Clashes were fixed, please retry the operation"
)

const (
	MaxFailedRetryCount = uint32(20) // Arbitrary value
)

var DefaultMaxProcs = runtime.NumCPU()

var BytesPerKB = float64(1024)

type desktopEntry struct {
	name string
	url  string
	icon string
}

type playable struct {
	play  func()
	pause func()
	reset func()
	stop  func()
}

func noop() {
}

type tuple struct {
	first  interface{}
	second interface{}
	last   interface{}
}

func retryableErrorCheck(v interface{}) (ok, retryable bool) {
	pr, pOk := v.(*tuple)
	if pr == nil || !pOk {
		retryable = true
		return
	}

	if pr.last == nil {
		ok = true
		return
	}

	err, assertOk := pr.last.(*googleapi.Error)
	// In relation to https://github.com/google/google-api-go-client/issues/93
	// where not every error is of googleapi.Error instance e.g io timeout errors
	// etc, let's assume that non-nil errors are retryable

	if !assertOk {
		retryable = true
		return
	}

	if err == nil {
		ok = true
		return
	}

	statusCode := err.Code
	if statusCode >= 500 && statusCode <= 599 {
		retryable = true
		return
	}

	switch statusCode {
	case 401, 403:
		retryable = true

		// TODO: Add other errors
	}

	return
}<commit_msg>retry: false if immutable file mutation attempted<commit_after>// Copyright 2015 Google Inc. All Rights Reserved.
//
// 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 drive

import (
	"bufio"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"path"
	"path/filepath"
	"reflect"
	"regexp"
	"runtime"
	"strconv"
	"strings"
	"sync"
	"time"

	"google.golang.org/api/googleapi"

	expirableCache "github.com/odeke-em/cache"
	spinner "github.com/odeke-em/cli-spinner"
	"github.com/odeke-em/drive/config"
)

var (
	// ErrRejectedTerms is empty "" because messages might be too
	// verbose to affirm a rejection that a user has already seen
	ErrRejectedTerms = errors.New("")
)

const (
	MimeTypeJoiner      = "-"
	RemoteDriveRootPath = "My Drive"

	FmtTimeString           = "2006-01-02T15:04:05.000Z"
	MsgClashesFixedNowRetry = "Clashes were fixed, please retry the operation"
	MsgErrFileNotMutable    = "File not mutable"
)

const (
	MaxFailedRetryCount = uint32(20) // Arbitrary value
)

var DefaultMaxProcs = runtime.NumCPU()

var BytesPerKB = float64(1024)

type desktopEntry struct {
	name string
	url  string
	icon string
}

type playable struct {
	play  func()
	pause func()
	reset func()
	stop  func()
}

func noop() {
}

type tuple struct {
	first  interface{}
	second interface{}
	last   interface{}
}

func retryableErrorCheck(v interface{}) (ok, retryable bool) {
	pr, pOk := v.(*tuple)
	if pr == nil || !pOk {
		retryable = true
		return
	}

	if pr.last == nil {
		ok = true
		return
	}

	err, assertOk := pr.last.(*googleapi.Error)
	// In relation to https://github.com/google/google-api-go-client/issues/93
	// where not every error is of googleapi.Error instance e.g io timeout errors
	// etc, let's assume that non-nil errors are retryable

	if !assertOk {
		retryable = true
		return
	}

	if err == nil {
		ok = true
		return
	}

	if strings.EqualFold(err.Message, MsgErrFileNotMutable) {
		retryable = false
		return
	}

	statusCode := err.Code
	if statusCode >= 500 && statusCode <= 599 {
		retryable = true
		return
	}

	switch statusCode {
	case 401, 403:
		retryable = true

		// TODO: Add other errors
	}

	return
}
{'fraction_non_alphanumeric': 0.11128526645768025, 'fraction_numerical': 0.01920062695924765, 'mean_word_length': 3.3189509306260576, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '5341009', 'n_tokens_mistral': 1843, 'n_tokens_neox': 1715, 'n_words': 558}