Remove the setting of file permissions #2057

This commit is contained in:
advplyr 2023-09-06 07:12:11 -05:00
parent 10011bd6a3
commit f02992dd4d
13 changed files with 2 additions and 176 deletions

View File

@ -9,7 +9,6 @@ const rateLimit = require('./libs/expressRateLimit')
const { version } = require('../package.json')
// Utils
const filePerms = require('./utils/filePerms')
const fileUtils = require('./utils/fileUtils')
const Logger = require('./Logger')
@ -52,11 +51,9 @@ class Server {
if (!fs.pathExistsSync(global.ConfigPath)) {
fs.mkdirSync(global.ConfigPath)
filePerms.setDefaultDirSync(global.ConfigPath, false)
}
if (!fs.pathExistsSync(global.MetadataPath)) {
fs.mkdirSync(global.MetadataPath)
filePerms.setDefaultDirSync(global.MetadataPath, false)
}
this.watcher = new Watcher()

View File

@ -1,7 +1,6 @@
const Sequelize = require('sequelize')
const Path = require('path')
const fs = require('../libs/fsExtra')
const filePerms = require('../utils/filePerms')
const Logger = require('../Logger')
const SocketAuthority = require('../SocketAuthority')
const Library = require('../objects/Library')
@ -43,7 +42,6 @@ class LibraryController {
const direxists = await fs.pathExists(folder.fullPath)
if (!direxists) { // If folder does not exist try to make it and set file permissions/owner
await fs.mkdir(folder.fullPath)
await filePerms.setDefault(folder.fullPath)
}
} catch (error) {
Logger.error(`[LibraryController] Failed to ensure folder dir "${folder.fullPath}"`, error)
@ -137,8 +135,6 @@ class LibraryController {
if (!success) {
return res.status(400).send(`Invalid folder directory "${path}"`)
}
// Set permissions on newly created path
await filePerms.setDefault(path)
}
}

View File

@ -6,7 +6,6 @@ const SocketAuthority = require('../SocketAuthority')
const Database = require('../Database')
const libraryItemFilters = require('../utils/queries/libraryItemFilters')
const filePerms = require('../utils/filePerms')
const patternValidation = require('../libs/nodeCron/pattern-validation')
const { isObject } = require('../utils/index')
@ -90,8 +89,6 @@ class MiscController {
})
}
await filePerms.setDefault(firstDirPath)
res.sendStatus(200)
}

View File

@ -6,7 +6,6 @@ const fs = require('../libs/fsExtra')
const { getPodcastFeed, findMatchingEpisodes } = require('../utils/podcastUtils')
const { getFileTimestampsWithIno, filePathToPOSIX } = require('../utils/fileUtils')
const filePerms = require('../utils/filePerms')
const LibraryItem = require('../objects/LibraryItem')
@ -49,7 +48,6 @@ class PodcastController {
return false
})
if (!success) return res.status(400).send('Invalid podcast path')
await filePerms.setDefault(podcastPath)
const libraryItemFolderStats = await getFileTimestampsWithIno(podcastPath)

View File

@ -4,7 +4,6 @@ const Path = require('path')
const Audnexus = require('../providers/Audnexus')
const { downloadFile } = require('../utils/fileUtils')
const filePerms = require('../utils/filePerms')
class AuthorFinder {
constructor() {
@ -40,7 +39,6 @@ class AuthorFinder {
if (!await fs.pathExists(authorDir)) {
await fs.ensureDir(authorDir)
await filePerms.setDefault(authorDir)
}
var imageExtension = url.toLowerCase().split('.').pop()

View File

@ -5,7 +5,6 @@ const fs = require('../libs/fsExtra')
const workerThreads = require('worker_threads')
const Logger = require('../Logger')
const Task = require('../objects/Task')
const filePerms = require('../utils/filePerms')
const { writeConcatFile } = require('../utils/ffmpegHelpers')
const toneHelpers = require('../utils/toneHelpers')
@ -201,10 +200,6 @@ class AbMergeManager {
Logger.debug(`[AbMergeManager] Moving m4b from ${task.data.tempFilepath} to ${task.data.targetFilepath}`)
await fs.move(task.data.tempFilepath, task.data.targetFilepath)
// Set file permissions and ownership
await filePerms.setDefault(task.data.targetFilepath)
await filePerms.setDefault(task.data.itemCachePath)
task.setFinished()
await this.removeTask(task, false)
Logger.info(`[AbMergeManager] Ab task finished ${task.id}`)

View File

@ -1,7 +1,6 @@
const Path = require('path')
const fs = require('../libs/fsExtra')
const stream = require('stream')
const filePerms = require('../utils/filePerms')
const Logger = require('../Logger')
const { resizeImage } = require('../utils/ffmpegHelpers')
@ -14,29 +13,20 @@ class CacheManager {
}
async ensureCachePaths() { // Creates cache paths if necessary and sets owner and permissions
var pathsCreated = false
if (!(await fs.pathExists(this.CachePath))) {
await fs.mkdir(this.CachePath)
pathsCreated = true
}
if (!(await fs.pathExists(this.CoverCachePath))) {
await fs.mkdir(this.CoverCachePath)
pathsCreated = true
}
if (!(await fs.pathExists(this.ImageCachePath))) {
await fs.mkdir(this.ImageCachePath)
pathsCreated = true
}
if (!(await fs.pathExists(this.ItemCachePath))) {
await fs.mkdir(this.ItemCachePath)
pathsCreated = true
}
if (pathsCreated) {
await filePerms.setDefault(this.CachePath)
}
}
@ -74,9 +64,6 @@ class CacheManager {
const writtenFile = await resizeImage(libraryItem.media.coverPath, path, width, height)
if (!writtenFile) return res.sendStatus(500)
// Set owner and permissions of cache image
await filePerms.setDefault(path)
if (global.XAccel) {
Logger.debug(`Use X-Accel to serve static file ${writtenFile}`)
return res.status(204).header({ 'X-Accel-Redirect': global.XAccel + writtenFile }).send()
@ -160,9 +147,6 @@ class CacheManager {
let writtenFile = await resizeImage(author.imagePath, path, width, height)
if (!writtenFile) return res.sendStatus(500)
// Set owner and permissions of cache image
await filePerms.setDefault(path)
var readStream = fs.createReadStream(writtenFile)
readStream.pipe(res)
}

View File

@ -3,7 +3,6 @@ const Path = require('path')
const Logger = require('../Logger')
const readChunk = require('../libs/readChunk')
const imageType = require('../libs/imageType')
const filePerms = require('../utils/filePerms')
const globals = require('../utils/globals')
const { downloadFile, filePathToPOSIX, checkPathIsFile } = require('../utils/fileUtils')
@ -111,7 +110,6 @@ class CoverManager {
Logger.info(`[CoverManager] Uploaded libraryItem cover "${coverFullPath}" for "${libraryItem.media.metadata.title}"`)
await filePerms.setDefault(coverFullPath)
libraryItem.updateMediaCover(coverFullPath)
return {
cover: coverFullPath
@ -149,8 +147,6 @@ class CoverManager {
await this.cacheManager.purgeCoverCache(libraryItem.id)
Logger.info(`[CoverManager] Downloaded libraryItem cover "${coverFullPath}" from url "${url}" for "${libraryItem.media.metadata.title}"`)
await filePerms.setDefault(coverFullPath)
libraryItem.updateMediaCover(coverFullPath)
return {
cover: coverFullPath
@ -222,7 +218,6 @@ class CoverManager {
error: 'Failed to copy cover to dir'
}
}
await filePerms.setDefault(newCoverPath)
await this.removeOldCovers(coverDirPath, '.' + imgtype.ext)
Logger.debug(`[CoverManager] cover copy success`)
coverPath = newCoverPath
@ -263,8 +258,6 @@ class CoverManager {
const success = await extractCoverArt(audioFileWithCover.metadata.path, coverFilePath)
if (success) {
await filePerms.setDefault(coverFilePath)
libraryItem.updateMediaCover(coverFilePath)
return coverFilePath
}

View File

@ -1,6 +1,5 @@
const Path = require('path')
const fs = require('../libs/fsExtra')
const filePerms = require('../utils/filePerms')
const DailyLog = require('../objects/DailyLog')
@ -25,13 +24,11 @@ class LogManager {
async ensureLogDirs() {
await fs.ensureDir(this.DailyLogPath)
await fs.ensureDir(this.ScanLogPath)
await filePerms.setDefault(Path.posix.join(global.MetadataPath, 'logs'), true)
}
async ensureScanLogDir() {
if (!(await fs.pathExists(this.ScanLogPath))) {
await fs.mkdir(this.ScanLogPath)
await filePerms.setDefault(this.ScanLogPath)
}
}

View File

@ -6,7 +6,6 @@ const fs = require('../libs/fsExtra')
const { getPodcastFeed } = require('../utils/podcastUtils')
const { removeFile, downloadFile } = require('../utils/fileUtils')
const filePerms = require('../utils/filePerms')
const { levenshteinDistance } = require('../utils/index')
const opmlParser = require('../utils/parsers/parseOPML')
const opmlGenerator = require('../utils/generators/opmlGenerator')
@ -96,7 +95,6 @@ class PodcastManager {
if (!(await fs.pathExists(this.currentDownload.libraryItem.path))) {
Logger.warn(`[PodcastManager] Podcast episode download: Podcast folder no longer exists at "${this.currentDownload.libraryItem.path}" - Creating it`)
await fs.mkdir(this.currentDownload.libraryItem.path)
await filePerms.setDefault(this.currentDownload.libraryItem.path)
}
let success = false

View File

@ -6,7 +6,6 @@ const date = require('../libs/dateAndTime')
const Logger = require('../Logger')
const Library = require('../objects/Library')
const { LogLevel } = require('../utils/constants')
const filePerms = require('../utils/filePerms')
const { secondsToTimestamp } = require('../utils/index')
class LibraryScan {
@ -127,7 +126,6 @@ class LibraryScan {
logLines.push(JSON.stringify(l))
})
await fs.writeFile(outputPath, logLines.join('\n') + '\n')
await filePerms.setDefault(outputPath)
Logger.info(`[LibraryScan] Scan log saved "${outputPath}"`)
}

View File

@ -1,120 +0,0 @@
const fs = require('../libs/fsExtra')
const Path = require('path')
const Logger = require('../Logger')
// Modified from:
// https://github.com/isaacs/chmodr/blob/master/chmodr.js
// If a party has r, add x
// so that dirs are listable
const dirMode = mode => {
if (mode & 0o400)
mode |= 0o100
if (mode & 0o40)
mode |= 0o10
if (mode & 0o4)
mode |= 0o1
return mode
}
const chmodrKid = (p, child, mode, uid, gid, cb) => {
if (typeof child === 'string')
return fs.lstat(Path.resolve(p, child), (er, stats) => {
if (er)
return cb(er)
stats.name = child
chmodrKid(p, stats, mode, uid, gid, cb)
})
if (child.isDirectory()) {
chmodr(Path.resolve(p, child.name), mode, uid, gid, er => {
if (er)
return cb(er)
var _path = Path.resolve(p, child.name)
fs.chmod(_path, dirMode(mode)).then(() => {
fs.chown(_path, uid, gid, cb)
})
})
} else {
var _path = Path.resolve(p, child.name)
fs.chmod(_path, mode).then(() => {
fs.chown(_path, uid, gid, cb)
})
}
}
const chmodr = (p, mode, uid, gid, cb) => {
fs.readdir(p, { withFileTypes: true }, (er, children) => {
// any error other than ENOTDIR means it's not readable, or
// doesn't exist. give up.
if (er && er.code !== 'ENOTDIR') return cb(er)
if (er) { // Is a file
return fs.chmod(p, mode).then(() => {
fs.chown(p, uid, gid, cb)
})
}
if (!children.length) {
return fs.chmod(p, dirMode(mode)).then(() => {
fs.chown(p, uid, gid, cb)
})
}
let len = children.length
let errState = null
const then = er => {
if (errState) return
if (er) return cb(errState = er)
if (--len === 0) {
return fs.chmod(p, dirMode(mode)).then(() => {
fs.chown(p, uid, gid, cb)
})
}
}
children.forEach(child => chmodrKid(p, child, mode, uid, gid, then))
})
}
// Set custom permissions
module.exports.set = (path, mode, uid, gid, silent = false) => {
return new Promise((resolve) => {
if (!silent) Logger.debug(`[FilePerms] Setting permission "${mode}" for uid ${uid} and gid ${gid} | "${path}"`)
chmodr(path, mode, uid, gid, resolve)
})
}
// Default permissions 0o744 and global Uid/Gid
module.exports.setDefault = (path, silent = false) => {
const mode = 0o744
const uid = global.Uid
const gid = global.Gid
return new Promise((resolve) => {
if (isNaN(uid) || isNaN(gid)) {
return resolve()
}
if (!silent) Logger.debug(`Setting permission "${mode}" for uid ${uid} and gid ${gid} | "${path}"`)
chmodr(path, mode, uid, gid, resolve)
})
}
// Default permissions 0o744 and global Uid/Gid
// Used for setting default permission to initial config/metadata directories
module.exports.setDefaultDirSync = (path, silent = false) => {
const mode = 0o744
const uid = global.Uid
const gid = global.Gid
if (isNaN(uid) || isNaN(gid)) {
return true
}
if (!silent) Logger.debug(`[FilePerms] Setting dir permission "${mode}" for uid ${uid} and gid ${gid} | "${path}"`)
try {
fs.chmodSync(path, mode)
fs.chownSync(path, uid, gid)
return true
} catch (error) {
Logger.error(`[FilePerms] Error setting dir permissions for path "${path}"`, error)
return false
}
}

View File

@ -1,5 +1,4 @@
const fs = require('../../libs/fsExtra')
const filePerms = require('../filePerms')
const package = require('../../../package.json')
const Logger = require('../../Logger')
const { getId } = require('../index')
@ -177,9 +176,7 @@ function generate(libraryItem, outputPath) {
fileString += `title=${chapter.title}\n`
})
}
return fs.writeFile(outputPath, fileString).then(() => {
return filePerms.setDefault(outputPath, true).then(() => true)
}).catch((error) => {
return fs.writeFile(outputPath, fileString).then(() => true).catch((error) => {
Logger.error(`[absMetaFileGenerator] Failed to save abs file`, error)
return false
})
@ -216,9 +213,7 @@ function generateFromNewModel(libraryItem, outputPath) {
fileString += `title=${chapter.title}\n`
})
}
return fs.writeFile(outputPath, fileString).then(() => {
return filePerms.setDefault(outputPath, true).then(() => true)
}).catch((error) => {
return fs.writeFile(outputPath, fileString).then(() => true).catch((error) => {
Logger.error(`[absMetaFileGenerator] Failed to save abs file`, error)
return false
})