readdirSync cannot read encrypted home folder when accessing it directly - node.js

const fs = require("fs")
//const HOW = "/home/test/everything"
// const HOW = "/home/test/"
// This one fails. My home is encrypted and it cannot read directories, it gets the .Private file. I want to read files and directories in my home folder. But can't.
const HOW = "/home/test/folder/"
// This one works for some reason. It lists all the directories in the folder.
// const HOW = "folder"
// This one works as well
var list = walk(HOW)
console.log(list)
// How do I get contents of /home/test (which happens to be my home folder).
// I'm both root and "test" user of the computer.
I'd like to have walk() work on /home/test/.
The code that fails:
var walk = function(dir) {
var results = []
var list = fs.readdirSync(dir)
list.forEach(function(file) {
file = dir + '/' + file
var stat = fs.statSync(file)
if (stat && stat.isDirectory()) results = results.concat(walk(file))
else results.push(file)
})
return results
}
The exact line causing it (stack trace): var stat = fs.statSync(file)
The error is:
Error: ENOENT: no such file or directory, stat '/home/test/.Private/###############################################################'
Where # is an amount of letters whose importance to safety is unknown to me.
Node.js doesn't have a problem addressing any folder contained within my home folder, but cannot address the home folder itself. Neither my own account nor root account can get access to it.

I think you are adding an unnecessary /. Try changing
const HOW = "/home/test/folder/"
to
const HOW = "/home/test/folder"

Related

How to edit this code to wait for a file fully uploaded with node.js?

Hi I am trying to download a file and when it downloads it first has the extension .tmp or .crdownload, then after it is fully downloaded the extension changes to whatever the correct extension is, for example .png. I want to wait until the file no longer has the extension .tmp or .crdownload and then save the new file path to a variable. How can I do this? I have looked here on Stack Overflow but it is not answering all my questions. This is a unique and specific question. That is what I have so far. I don't mind if you don't use this code as long as the resulting code waits for the extension to change and saves the path to a variable as a string.
Code:
var downloadanduploadpath = "C:/Users/user1/Downloads";
//ptc mean path to check. have differnt name so the variable name of the placeholder in the function call is not the same name as the variable name in the function
var ptc = downloadanduploadpath;
var pathtocheck = ptc;
var timetowaitforimagefiledownloaded = 5000;
var fs = require('fs');
//let dirToCheck = pathtocheck;
var filecheckerfiles = fs.readdirSync(pathtocheck);
var filecheckerlatestPath = `${pathtocheck}/${filecheckerfiles[0]}`;
var filecheckerlatestTimeStamp = fs.statSync(filecheckerlatestPath).mtime.getTime();
filecheckerfiles.forEach(filecheckerfile => {
var filecheckerpath = `${pathtocheck}/${filecheckerfile}`;
var filecheckertimeStamp = fs.statSync(filecheckerpath).mtime.getTime();
if (filecheckertimeStamp > filecheckerlatestTimeStamp) {
filecheckerlatestTimeStamp = filecheckertimeStamp;
lastdownloadedimagetimestamp = filecheckerlatestTimeStamp;
lastdownloadedimage = filecheckerpath;
}
});
//get the extention of the last downloaded image
var lastdownloadedimageextention = lastdownloadedimage.split(".").pop();
//"png" || "jpg" || "jpeg" || "gif" ||
//if the file is a .tmp the image is not fully downloaded. run code inside to wait for it to not be a tmp file showing that the image if fully downloaded
if((lastdownloadedimageextention == "tmp") || (lastdownloadedimageextention == "crdownload") ){
//Show the file path to the console
console.log(lastdownloadedimage);
//show file not fill downloaded message in console
console.log("Image not yet fully downloaded.");
//show preparing to wait message in console
console.log("Preparing to wait for full download.");
//get all the files in the folder directory/path
var filecheckerfindpathbytimestamp = fs.readdirSync(pathtocheck);
console.log("hi1");
//define variable to store the path of the last donwloaded image foudn by timestamp
var lastdownloadedimagefoundbytimestamppath;
console.log("hi2");
//use the timestamp as an index to get the last downloaded file and then check the file at this timestamp(the last downloaded image) and check its file path.
//using the timestamp as an image lets us check the file without using the name because the name changes from .tmp to .png for example, so we are unable to get the
//the file by its name because of this name , so we use the timestamp to specifc the file we want to check the extention of instead
//find path of the last downloaded file by using the time to find it
filecheckerfindpathbytimestamp.forEach(filecheckerfindpathbytimestampfile => {
var filecheckerfindpathbytimestamppath = `${pathtocheck}/${filecheckerfindpathbytimestampfile}`;
console.log("hi3");
var filecheckerfindpathbytimestamptimeStamp = fs.statSync(filecheckerfindpathbytimestamppath).mtime.getTime();
console.log("hi4");
if (filecheckerfindpathbytimestamptimeStamp == lastdownloadedimagetimestamp) {
console.log("hi5");
lastdownloadedimagefoundbytimestamppath = filecheckerfindpathbytimestamppath;
console.log("hi6");
}
});
console.log("hi7");
//keep checking the file that ends in .tmp untill it changes from .tmp . once the file is no longer endng in .tmp indicating that the file is
//fully downloaded this path that is looking for the fiel path including the .tmp will no longer be valid because of the file path becomeing .png for example
// that it has fully downloaded, and the file path will not return true because it is no longer valid with the .tmp extention.
//once this path is no longer valid we will know that the file has fully downloaded because that path no longer ends in .tmp. We can then end the while loop.
//because file if fully downloaded.
//
while(fs.existsSync(lastdownloadedimagefoundbytimestamppath) == true){
//while((fs.existsSync(lastdownloadedimage)) == true){
//Show the file path to the console
console.log(lastdownloadedimage);
//Wait code
//Wait on the image converting page
await page2.type('.jsoninputtextarea', " ", {delay:timetowaitforimagefiledownloaded});
//show waiting for file fully download message in console
console.log("Waiting for image to fully download.");
}
//create varaible to find path of the last downloaded file by using the time to find it now that it is fully downloaded
var filecheckerfindfullydownloadedfiletimestamp = fs.readdirSync(pathtocheck);
//find path of the last downloaded file by using the time to find it now that it is fully downloaded
filecheckerfindfullydownloadedfiletimestamp.forEach(filecheckerfindfullydownloadedfilefile => {
var filecheckerfindfullydownloadedfilepath = `${pathtocheck}/${filecheckerfindfullydownloadedfilefile}`;
console.log("hi8");
var filecheckerfindfullydownloadedfiletimeStamp = fs.statSync(filecheckerfindfullydownloadedfilepath).mtime.getTime();
console.log("hi9");
if (filecheckerfindfullydownloadedfiletimeStamp == lastdownloadedimagetimestamp) {
console.log("hi10");
lastdownloadedimagefoundbytimestamppath = filecheckerfindfullydownloadedfilepath;
console.log("hi11");
}
});
console.log("hi12");
//Set lastdownloaded image to the path the now ends in the file extention (example .png) now instead of .tmp
//
lastdownloadedimage = lastdownloadedimagefoundbytimestamppath;
}
//Remove the path the of the image so we only have image name and file type
//Before: C:/Users/edtec/Downloads/image.png After: image.png
lastdownloadedimage = lastdownloadedimage.replace(downloadanduploadpath, "");
//Return lastdownloadedimage
//return lastdownloadedimage;
//}
//lastdownloadedimage = getlastdownloadedimage(downloadanduploadpath);
//console.log(lastdownloadedimage);
console.log(lastdownloadedimage);

Problem with traversing file system using Node.js

I keep getting the error 'Error: ENOENT: no such file or directory, open 't1.txt''
when I run the function starting in the top-level directory pictured below.
I think it has to with 'fs.readFileSync()' attempting to read a file's contents from a directory different than the one fs is declared in, though I am not quite sure.
Picture of directory structure
/* the built-in Node.js 'fs' module is included in our program so we can work with the computer's file system */
const fs = require('fs');
// used to store the file contents of the files encountered
const fileContents = {};
/* stores the duplicate files as subarrays within the array, where the first element of the subarray is the duplicate file, and the second element is the original */
let duplicateFiles = [];
const traverseFileSystem = function (currentPath) {
// reads the contents of the current directory and returns them in an array
let files = fs.readdirSync(currentPath);
// for-in loop is used to iterate over contents of directory
for (let i in files) {
let currentFile = currentPath + '/' + files[i];
// retrieves the stats of the current file and assigns them to a variable
let stats = fs.statSync(currentFile);
// it's determined if the 'currentFile' is actually a file
if (stats.isFile()) {
/* if the file's contents are in the 'fileContents' object, then a new file has been encountered */
if(fileContents[fs.readFileSync(files[i])] === undefined) {
// the file's contents are set as the key, and the file path as the value
fileContents[fs.readFileSync(files[i])] = currentFile;
}
// otherwise, the file's contents already exist in the 'fileContents' object, which means a duplicate file has been found
else {
duplicateFiles.push([fileContents[fs.readFileSync(files[i])], currentFile]);
}
}
/* if the 'file' is actually a directory, traverseFileSystem() is called recursively on that directory */
else if (stats.isDirectory()) {
traverseFileSystem(currentFile);
}
}
return duplicateFiles;
};
You have fs.readFileSync(files[i]) which should be fs.readFileSync(currentFile) based on your code. Haven't confirmed your logic, but this should solve the error you are currently getting.

Node search for file upwards

I'm looking for a function in node.js to search upwards in the filesystem to check if a given file exists and if so get its content.
For example, if I have the following folder structure:
* root
|--dir1
| |-dir2
| |-dir3
|...
If I'm in dir3 I want to search for a given file that could be in any folder if I go up .. but not in their subfolders. So what I want is a simple way to check in current folder for the file if it doesn't exist go up one folder and search there, until you find the file or you are in the root folder.
I think it's easy to write by yourself. You can use fs.readdir or fs.readdirSync.
Synchronous solution might look like this: (not tested)
var fs = require('fs');
var process = require('process');
var path = require('path');
function findFile(filename, startdir) {
if(!startdir) statdir = process.cwd();
while(true) {
var list = fs.readdirSync(startdir);
if((index = list.indexOf(filename)) != -1)
// found
return fs.readFileSync(path.join([startdir, filename]));
else if(startdir == '/')
// root dir, file not found
return null;
else
startdir = path.normalize(path.join([startdir, '..']));
}
}
There is also an NPM package - fileUp that does this.
From the Readme:
const path = require('path');
const findUp = require('find-up');
(async () => {
console.log(await findUp('unicorn.png'));
//=> '/Users/sindresorhus/unicorn.png'
console.log(await findUp(['rainbow.png', 'unicorn.png']));
//=> '/Users/sindresorhus/unicorn.png'
console.log(await findUp(async directory => {
const hasUnicorns = await findUp.exists(path.join(directory, 'unicorn.png'));
return hasUnicorns && directory;
}, {type: 'directory'}));
//=> '/Users/sindresorhus'
})();

How to read from a specified file which is in a folder below without knowing the absolute path

So I have my project directory:
Here I read ArticleFile:
function _getDataFromFile() {
var jsonArray = csvjson.toObject(fs.readFileSync('ArticleFile.csv', { encoding: 'utf8' }));
var result = [];
for (var idx = 0; idx < jsonArray.length; idx++) {
var currArt = jsonArray[idx];
// if (!checkIfElementIsArticle(currArt)) throw "loaded object IS NOT an article!";
result.push(new Article(currArt.imageLocation, currArt.title, currArt.description, parseInt(currArt.quantity),parseInt(currArt.price)));
}
return result;
}
The problem is if "ArticleFile.csv" is in lets say contentsKopie I have to know the absolute path such as : C:\Users\noone_000\Desktop\BSD\Hausübungen\WebServer\WebServer\contentsKopie\ArticleFile.csv . How can I set the path like: fs.readFileSync('/ContentsCopie/ArticleFile.csv', { encoding: 'utf8' })
PS: csvjson is a module (require("csvjson"))
When you start the path with / it is interpreted as an absolute path. If you want to open a file in sub directory, you can use ./ContentsCopie/ArticleFile.csv
The . in front of the slash means that the path is relative to the current directory.
Conversely, if you needed to go up a level you can prefix your path with ..
Isn't ./contentsKopie/ArticleFile.csv enough?
When using /contentsKopie/ArticleFile.csv it's actually looking in C:/contentsKopie/ArticleFile.csv. So you have to prepend your path with a . to tell it to start at the working directory (WebServer here).
Otherwise, you can have the absolute path of the current file with __dirname then you could compose your path using the path module from node.
var path = require('path');
var article = path.join(__dirname, './contentsKopie/ArticleFile.csv');
This way, you'll get the absolute path of your file.

NodeJS - Copy and Rename all contents in existing directory recursively

I have a directory with folders and files within. I want to copy the entire directory with all its contents to a different location while renaming all the files to something more meaningful. I want to use nodejs to complete this series of operations. What is an easy way to do it, other than moving it one by one and renaming it one by one?
Thanks.
-- Thanks for the comment! So here is an example directory that I have in mind:
-MyFridge
- MyFood.txt
- MyApple.txt
- MyOrange.txt
- ...
- MyDrinks
- MySoda
- MyDietCoke.txt
- MyMilk.txt
- ...
- MyDesserts
- MyIce
...
I want to replace "My" with "Tom," for instance, and I also would like to rename "My" to Tom in all the text files. I am able to copy the directory to a different location using node-fs-extra, but I am having a hard time with renaming the file names.
Define your own tools
const fs = require('fs');
const path = require('path');
function renameFilesRecursive(dir, from, to) {
fs.readdirSync(dir).forEach(it => {
const itsPath = path.resolve(dir, it);
const itsStat = fs.statSync(itsPath);
if (itsPath.search(from) > -1) {
fs.renameSync(itsPath, itsPath.replace(from, to))
}
if (itsStat.isDirectory()) {
renameFilesRecursive(itsPath.replace(from, to), from, to)
}
})
}
Usage
const dir = path.resolve(__dirname, 'src/app');
renameFilesRecursive(dir, /^My/, 'Tom');
renameFilesRecursive(dir, /\.txt$/, '.class');
fs-jetpack has a pretty nice API to deal with problems like that...
const jetpack = require("fs-jetpack");
// Create two fs-jetpack contexts that point
// to source and destination directories.
const src = jetpack.cwd("path/to/source/folder");
const dst = jetpack.cwd("path/to/destination");
// List all files (recursively) in the source directory.
src.find().forEach(path => {
const content = src.read(path, "buffer");
// Transform the path however you need...
const transformedPath = path.replace("My", "Tom");
// Write the file content under new name in the destination directory.
dst.write(transformedPath, content);
});

Resources