Lab 13.2 Watching

Lab 13.2 Watching is actually located in the ch-13 labs-1 folder and the 13.1 - Read Directory and Write File is located in the labs-2 folder but whatever, I can overlook that. I need help solving the 13.2 Watching lab. The lab instructions state, "The goal is to ensure that the answer variable is set to the newly created file." and my actual results are the newly created file every time as confirmed in the assert actual and when I log the file creation. The assert always logs the previously created file as expected though. I must be missing something and would appreciate some direction.
Comments
-
thanks for the heads up on the ordering, it's being corrected now.
Can you post some code as a starting point for a discussion about this?
0 -
So I put my solution below, my tests passed, I made some comments to describe the program.
First the program creates a file named project,
Then it adds a file that is a 'pre-existing file' to project,
Later the program uses the fs.watch() function to watch the directory we created, project. After the process begins watching the directory the next part of the program involves creating a new file, a new directory, and changing the permissions.Each time the one of these events occurs the callback function is called.
Within the callback function we must create some logic so that we only assign the variable answer to a newly created file in the project directory.
I achieved this by using the stats object. Importantly we must avoid the newly created directory, to select for files we can use the fs.stats.isFile() method. The other piece of logic is birth time === change time.
The code passes.
The comments were not rendering correctly so I used various commenting characters to coerce the markdown to render correctly.
The OUTPUT is at the bottom. 😀
'use strict' const assert = require('assert') const { join } = require('path') const fs = require('fs') const { promisify } = require('util') const timeout = promisify(setTimeout) const project = join(__dirname, 'project') try { fs.rmdirSync(project, {recursive: true}) } catch (err) { console.error(err) } /* project directory is made */ fs.mkdirSync(project) let answer = '' async function writer () { const { open, chmod, mkdir } = fs.promises const pre = join(project, Math.random().toString(36).slice(2)) /* pre-existing file is created */ const handle = await open(pre, 'w') await handle.close() await timeout(500) exercise(project) const file = join(project, Math.random().toString(36).slice(2)) const dir = join(project, Math.random().toString(36).slice(2)) /* another file is made */ const add = await open(file, 'w') await add.close() /* a directory is created */ await mkdir(dir) /* Permissions are changed */ await chmod(pre, 0o644) await timeout(500) assert.strictEqual( answer, file, 'answer should be the file (not folder) which was added' ) console.log('passed!') process.exit() } writer().catch((err) => { console.error(err) process.exit(1) }) function exercise (project) { const files = new Set(fs.readdirSync(project)) fs.watch(project, (evt, filename) => { try { const filepath = join(project, filename) const stat = fs.statSync(filepath) /* TODO - only set the answer variable if the filepath // is both newly created AND does not point to a directory //============================ */ const { birthtimeMs, ctimeMs } = stat; /*isFile() filters out dir events // birthtimeMs and ctimeMs selects for file creation events. */ if (stat.isFile() && birthtimeMs === ctimeMs) { answer = filepath console.log(answer) } /* //============================= */ } catch (err) { } }) }
OUTPUT
/home/ec2-user/environment/project/hkzktf59p0l passed!
1 -
hey @andrewpartrickmiller - great work! would you mind putting solutions inside
<details>
elements, e.g:<details> <summary> SPOILERS </summary> {YOUR CODE HERE} </details>
1 -
I used if (fs.statSync(project).isDirectory() && birthtimeMs === ctimeMs) instead of isFile()
Someone can tell why we should use this method about fs.statSync(filepath) and not use isDirectory()?0 -
Hello @davidmarkclements !
Doing this exercise, I'm facing a problem. It seems that the
chmod
applied on thepre
file doesn't trigger any event.Just changing this in the
exercise
methodif(stat.isFile()) { answer = filepath }
And it passed, while I was waiting for it to fail because the last file which should trigger an event is the
pre
one.I'm working on Windows 10. Do you have any idea ?
Thanks !
0 -
@fanly
fs.statSync(project).isDirectory() === false
would be a more accurate implementation thanisFile()
because isFile is for regular files (e.g. not sockets etc) where is !isDirectory() would be for any non-directory. So you are more correct.1 -
@axel.latour - can you try this on on latest v12 or v14, it may be a windows related bug
0 -
@davidmarkclements Thanks for your answer, I did it using v12, just tried with v14, and still the same issue
0 -
ok thanks @axel.latour I'm going to need to check this out on Windows 10
0 -
Hello @axel.latour !
Apparently
fsPromises.chmod(path, mode)
on Windows only has an effect for modes0o111, 0o444, 0o555
which make the file read-only.For example:
await chmod(pre, 0o444)
is effective and triggers the'change'
event.0 -
great observation @jorams, content is being updated on your suggestion
0
Categories
- All Categories
- 49 LFX Mentorship
- 102 LFX Mentorship: Linux Kernel
- 551 Linux Foundation Boot Camps
- 295 Cloud Engineer Boot Camp
- 118 Advanced Cloud Engineer Boot Camp
- 52 DevOps Engineer Boot Camp
- 53 Cloud Native Developer Boot Camp
- 4 Express Training Courses
- 4 Express Courses - Discussion Forum
- 1.9K Training Courses
- 18 LFC110 Class Forum
- 6 LFC131 Class Forum
- 25 LFD102 Class Forum
- 150 LFD103 Class Forum
- 17 LFD121 Class Forum
- LFD137 Class Forum
- 61 LFD201 Class Forum
- LFD210 Class Forum
- LFD210-CN Class Forum
- 1 LFD213 Class Forum - Discontinued
- 128 LFD232 Class Forum
- LFD237 Class Forum
- 23 LFD254 Class Forum
- 597 LFD259 Class Forum
- 102 LFD272 Class Forum
- 1 LFD272-JP クラス フォーラム
- LFD273 Class Forum
- 2 LFS145 Class Forum
- 24 LFS200 Class Forum
- 739 LFS201 Class Forum
- 1 LFS201-JP クラス フォーラム
- 3 LFS203 Class Forum
- 69 LFS207 Class Forum
- 300 LFS211 Class Forum
- 54 LFS216 Class Forum
- 47 LFS241 Class Forum
- 41 LFS242 Class Forum
- 37 LFS243 Class Forum
- 11 LFS244 Class Forum
- 33 LFS250 Class Forum
- 1 LFS250-JP クラス フォーラム
- LFS251 Class Forum
- 140 LFS253 Class Forum
- 1K LFS258 Class Forum
- 10 LFS258-JP クラス フォーラム
- 92 LFS260 Class Forum
- 129 LFS261 Class Forum
- 32 LFS262 Class Forum
- 79 LFS263 Class Forum
- 15 LFS264 Class Forum
- 11 LFS266 Class Forum
- 17 LFS267 Class Forum
- 17 LFS268 Class Forum
- 23 LFS269 Class Forum
- 203 LFS272 Class Forum
- 1 LFS272-JP クラス フォーラム
- LFS281 Class Forum
- 221 LFW211 Class Forum
- 167 LFW212 Class Forum
- SKF100 Class Forum
- 901 Hardware
- 219 Drivers
- 74 I/O Devices
- 44 Monitors
- 115 Multimedia
- 208 Networking
- 101 Printers & Scanners
- 85 Storage
- 761 Linux Distributions
- 88 Debian
- 66 Fedora
- 15 Linux Mint
- 13 Mageia
- 24 openSUSE
- 141 Red Hat Enterprise
- 33 Slackware
- 13 SUSE Enterprise
- 356 Ubuntu
- 477 Linux System Administration
- 41 Cloud Computing
- 69 Command Line/Scripting
- Github systems admin projects
- 95 Linux Security
- 77 Network Management
- 108 System Management
- 49 Web Management
- 66 Mobile Computing
- 23 Android
- 29 Development
- 1.2K New to Linux
- 1.1K Getting Started with Linux
- 536 Off Topic
- 131 Introductions
- 216 Small Talk
- 21 Study Material
- 816 Programming and Development
- 275 Kernel Development
- 507 Software Development
- 928 Software
- 260 Applications
- 184 Command Line
- 3 Compiling/Installing
- 76 Games
- 316 Installation
- 59 All In Program
- 59 All In Forum
Upcoming Training
-
August 20, 2018
Kubernetes Administration (LFS458)
-
August 20, 2018
Linux System Administration (LFS301)
-
August 27, 2018
Open Source Virtualization (LFS462)
-
August 27, 2018
Linux Kernel Debugging and Security (LFD440)