This file is indexed.

/usr/share/gocode/src/github.com/google/cups-connector/cups/ppdcache.go is in google-cloud-print-connector 0.0~git20151105.24.1902938-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
Copyright 2015 Google Inc. All rights reserved.

Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file or at
https://developers.google.com/open-source/licenses/bsd
*/

package cups

/*
#include "cups.h"
*/
import "C"
import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"sync"
	"unsafe"

	"github.com/google/cups-connector/cdd"
)

// This isn't really a cache, but an interface to CUPS' quirky PPD interface.
// The connector needs to know when a PPD changes, but the CUPS API can only:
// (1) fetch a PPD to a file
// (2) indicate whether a PPD file is up-to-date.
// So, this "cache":
// (1) maintains temporary file copies of PPDs for each printer
// (2) updates those PPD files as necessary
type ppdCache struct {
	cc         *cupsCore
	cache      map[string]*ppdCacheEntry
	cacheMutex sync.RWMutex
}

func newPPDCache(cc *cupsCore) *ppdCache {
	cache := make(map[string]*ppdCacheEntry)
	pc := ppdCache{
		cc:    cc,
		cache: cache,
	}
	return &pc
}

func (pc *ppdCache) quit() {
	pc.cacheMutex.Lock()
	defer pc.cacheMutex.Unlock()

	for printername, pce := range pc.cache {
		pce.free()
		delete(pc.cache, printername)
	}
}

// removePPD removes a cache entry from the cache.
func (pc *ppdCache) removePPD(printername string) {
	pc.cacheMutex.Lock()
	defer pc.cacheMutex.Unlock()

	if pce, exists := pc.cache[printername]; exists {
		pce.free()
		delete(pc.cache, printername)
	}
}

func (pc *ppdCache) getPPDCacheEntry(printername string) (*cdd.PrinterDescriptionSection, string, string, error) {
	pc.cacheMutex.RLock()
	pce, exists := pc.cache[printername]
	pc.cacheMutex.RUnlock()

	if !exists {
		pce, err := createPPDCacheEntry(printername)
		if err != nil {
			return nil, "", "", err
		}
		if err = pce.refresh(pc.cc); err != nil {
			pce.free()
			return nil, "", "", err
		}

		pc.cacheMutex.Lock()
		defer pc.cacheMutex.Unlock()

		if firstPCE, exists := pc.cache[printername]; exists {
			// Two entries were created at the same time. Remove the older one.
			delete(pc.cache, printername)
			go firstPCE.free()
		}
		pc.cache[printername] = pce
		description, manufacturer, model := pce.getFields()
		return &description, manufacturer, model, nil

	} else {
		if err := pce.refresh(pc.cc); err != nil {
			delete(pc.cache, printername)
			pce.free()
			return nil, "", "", err
		}
		description, manufacturer, model := pce.getFields()
		return &description, manufacturer, model, nil
	}
}

// Holds persistent data needed for calling C.cupsGetPPD3.
type ppdCacheEntry struct {
	printername  *C.char
	modtime      C.time_t
	filename     string
	description  cdd.PrinterDescriptionSection
	manufacturer string
	model        string
	mutex        sync.Mutex
}

// createPPDCacheEntry creates an instance of ppdCache with the name field set,
// all else empty. The caller must free the name and buffer fields with
// ppdCacheEntry.free()
func createPPDCacheEntry(name string) (*ppdCacheEntry, error) {
	file, err := ioutil.TempFile("", "cups-connector-ppd-")
	if err != nil {
		return nil, fmt.Errorf("Failed to create PPD cache entry file: %s", err)
	}
	defer file.Close()
	filename := file.Name()

	pce := &ppdCacheEntry{
		printername: C.CString(name),
		modtime:     C.time_t(0),
		filename:    filename,
	}

	return pce, nil
}

// getFields gets externally-interesting fields from this ppdCacheEntry under
// a lock. The description is passed as a value (copy), to protect the cached copy.
func (pce *ppdCacheEntry) getFields() (cdd.PrinterDescriptionSection, string, string) {
	pce.mutex.Lock()
	defer pce.mutex.Unlock()
	return pce.description, pce.manufacturer, pce.model
}

// free frees the memory that stores the name and buffer fields, and deletes
// the file named by the buffer field. If the file doesn't exist, no error is
// returned.
func (pce *ppdCacheEntry) free() {
	pce.mutex.Lock()
	defer pce.mutex.Unlock()

	C.free(unsafe.Pointer(pce.printername))
	os.Remove(pce.filename)
}

// refresh calls cupsGetPPD3() to refresh this PPD information, in
// case CUPS has a new PPD for the printer.
func (pce *ppdCacheEntry) refresh(cc *cupsCore) error {
	pce.mutex.Lock()
	defer pce.mutex.Unlock()

	ppdFilename, err := cc.getPPD(pce.printername, &pce.modtime)
	if err != nil {
		return err
	}

	if ppdFilename == nil {
		// Cache hit.
		return nil
	}

	// (else) Cache miss.
	defer C.free(unsafe.Pointer(ppdFilename))
	defer os.Remove(C.GoString(ppdFilename))

	// Read from CUPS temporary file.
	r, err := os.Open(C.GoString(ppdFilename))
	if err != nil {
		return err
	}
	defer r.Close()

	// Write to this PPD cache file.
	file, err := os.OpenFile(pce.filename, os.O_WRONLY|os.O_TRUNC, 0200)
	if err != nil {
		return fmt.Errorf("Failed to open already-created PPD cache file: %s", err)
	}
	defer file.Close()

	// Also write to a buffer for translation.
	var content bytes.Buffer

	// Write to these simultaneously.
	w := io.MultiWriter(&content, file)
	if _, err := io.Copy(w, r); err != nil {
		return err
	}

	description, manufacturer, model := translatePPD(content.String())
	if description == nil || manufacturer == "" || model == "" {
		return errors.New("Failed to parse PPD")
	}

	pce.description = *description
	pce.manufacturer = manufacturer
	pce.model = model

	return nil
}