diff --git a/server/Server.js b/server/Server.js index 6afab875..eb8045f2 100644 --- a/server/Server.js +++ b/server/Server.js @@ -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() diff --git a/server/controllers/LibraryController.js b/server/controllers/LibraryController.js index db55521d..ae36686a 100644 --- a/server/controllers/LibraryController.js +++ b/server/controllers/LibraryController.js @@ -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) } } diff --git a/server/controllers/MiscController.js b/server/controllers/MiscController.js index 942cda70..3220eddf 100644 --- a/server/controllers/MiscController.js +++ b/server/controllers/MiscController.js @@ -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) } diff --git a/server/controllers/PodcastController.js b/server/controllers/PodcastController.js index ba91ad94..1447dc03 100644 --- a/server/controllers/PodcastController.js +++ b/server/controllers/PodcastController.js @@ -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) diff --git a/server/finders/AuthorFinder.js b/server/finders/AuthorFinder.js index fcc22cad..9c2a3b4f 100644 --- a/server/finders/AuthorFinder.js +++ b/server/finders/AuthorFinder.js @@ -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() diff --git a/server/managers/AbMergeManager.js b/server/managers/AbMergeManager.js index 4c041b8b..4ced1390 100644 --- a/server/managers/AbMergeManager.js +++ b/server/managers/AbMergeManager.js @@ -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}`) diff --git a/server/managers/CacheManager.js b/server/managers/CacheManager.js index f92f0e48..7ce9ca66 100644 --- a/server/managers/CacheManager.js +++ b/server/managers/CacheManager.js @@ -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) } diff --git a/server/managers/CoverManager.js b/server/managers/CoverManager.js index 821eb453..f2545985 100644 --- a/server/managers/CoverManager.js +++ b/server/managers/CoverManager.js @@ -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 } diff --git a/server/managers/LogManager.js b/server/managers/LogManager.js index 789cd877..0b01f32f 100644 --- a/server/managers/LogManager.js +++ b/server/managers/LogManager.js @@ -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) } } diff --git a/server/managers/PodcastManager.js b/server/managers/PodcastManager.js index d8d32c5c..5dec2152 100644 --- a/server/managers/PodcastManager.js +++ b/server/managers/PodcastManager.js @@ -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 diff --git a/server/scanner/LibraryScan.js b/server/scanner/LibraryScan.js index 432ff3cc..98174a5a 100644 --- a/server/scanner/LibraryScan.js +++ b/server/scanner/LibraryScan.js @@ -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}"`) } diff --git a/server/utils/filePerms.js b/server/utils/filePerms.js deleted file mode 100644 index b32f3040..00000000 --- a/server/utils/filePerms.js +++ /dev/null @@ -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 - } -} \ No newline at end of file diff --git a/server/utils/generators/abmetadataGenerator.js b/server/utils/generators/abmetadataGenerator.js index 6795a9ff..ff82ac33 100644 --- a/server/utils/generators/abmetadataGenerator.js +++ b/server/utils/generators/abmetadataGenerator.js @@ -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 })