- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
It doesn't necessarily matter if you can't modify the Watcher (which, incidentally was a condition introduced only halfway through to try to point the candidate in another direction, not a limitation from the outset).
Not being able to modify the Watcher to use flock()/lockf() (which any app like that should be doing in the first place if it's been written professionally) just means you have to get a mandatory lock, rather than an advisory lock.
Admin
Just kidding, of course. :-)
Admin
"Proper" file locking is absolutely supported on NFS under UNIX, even Windows supports locking over NFS.
Most systems just require an appropriate RPC daemon and portmap to be running (I think it's a Service in Windows). This is not that unusual, I've certainly used commercial/3rd party software under UNIX which requires file locking on NFS be enabled if you want to run it from an NFS share.
It's not "a liberal and unwarranted" assumption, as it's trivial to check if you got the lock (and something you would need to do anyway, even if you were writing to another location and then moving the file) - certainly it's less work than the conditional checking you'd have to do to handle using a temporary file location.
Admin
Heck, my first idea was "just send a signal when a file is done" too. Watcher just listens on port x, downloaders sends a signal to port y. Or a triple handshake.
Although I did get the "use a temp dir" when he said the watcher could not be changed.
Can't blame the recruit for assuming it's one of those M$ trick questions where for every solution you give, they dismiss it.
Admin
What a lot of old crap about locks, atomic moves, filesystem semantics, *nix etc. It's exhausting!
The simple solution has been suggested a couple of times above, and ignored, i.e. the semaphore file which is sent after the big file. The Watcher only registers the arrival of the semaphore file, and only processes the big file.
And don't tell me "the Watcher can't be modified." - this is the real world, and it's a piece of code; it can be modified.
Admin
That's what I call sound approach to creating a robust solution free of timing dependencies ;-)
Admin
Basic network I/O is easy, but often you also need security. You're probably going to need (by policy) to have some authentication for any network traffic. While you can use SSL to provide this (which makes it easy from the programming level) you now have to manage certificates.
It's therefore about two orders of magnitude easier to just use the filesystem which both programs already have access and are protected by the necessary permissions or ACLs, rather than having to add yet more authentication and authorization to make sure these things remain secure - especially if you're encumbered by an ISO27001 security process.
Admin
"Locking files is so 1990's - this is Web 2.0 baby!
We just try to append and hope for the best!"
Admin
The real WTF is the forum software, of course! If you could view posts by thread, it might make it a lot easier to check whether something was already said before (by the way: was it already mentioned that a file move from the temp directory might not be atomic?), or whether a certain post has already been replied to, at least by letting you easily ignore all the posts on a completely unrelated topic.
Admin
A diamond in a rough is a gem that stands out among a collection of dull rocks.
What you're talking about is a "rough diamond".
Inconceivable!
Admin
This is the real world; inter-department and inter-company politics and bureaucracy will ensure no-one gets access to the piece of code in order to modify it - assuming whoever is responsible actually feels responsible enough to do something
Admin
Yes, well I knew someone would miss the point completely.
Who says I don't lock files? I lock files if I need to; the point with my solution is it doesn't matter if the file is locked or not, it's simply not a factor. The "lock the file" solution is old crap not because locking files is wrong, or because I can't be bothered to do it. but because it's irrelevant. Oh and incidentally, I don't do any web development at all, I'm very much an old-fashioned dinosaur of a programmer with ancient standards and working practices that date back to 1986, so please don't accuse me of being new-style lazy.
Admin
I have to admit I failed this like everyone else it seems.
My first reaction is that the watcher should be invoked by the downloader when it has finished getting the file rather than constantly running.
If you can't do that I have some other suggestions but certainly one of them wasn't "download them to a temporary directory and move them when they're finished".
Why would you be able to "chain" an event to the end of the downloader, such as a move, but not chain the watcher launching then?
Admin
Erm.. Moving a file is always the right answer to this problem. In linux you can even move a file ontop of an existing file and it wont even break processes that have the original open. They can merrily keep using that file descriptor until they close and reopen the file.
Seriously... anyone who fails to get this right is not worth hiring. They're usually the ones that develop horrifically overcomplex systems for simple problems.
Admin
Well, the best self-advice I can squeeze out of this situation is thinking out loud at an interview. Basically its possible that the temporary directory solution was a candidate solution, but the developer thought about the possibility that it may not be on the same volume so he silently discarded it (and the probability is not that small either).
So... if you figure your reasoning is good, you can think-out-loud the discarded solutions too, possibly getting bonus points.
Admin
“Oh,” he scoffed, “so, we’re equals now, is that it?”
ROFL!! I highly doubt it!
Admin
Nice trackback, too.
Admin
I once went for an interview and was told to meet up directly at their clients offices, where he would appear and meet up with me to discuss the next project with them.
This company makes a well known open source CMS system.
The guy doesnt show up, and i have to act as a standin with their clients and i sit there and bullshit as good as i could.
20 minutes later he shows up for the still ongoing meeting.
Afterwards, i team up with him and we go to their offices where the actual interview would take place.
The interview consisted of 30 minutes of slides about their biggest customers (NASA, etc etc. NASA cant make their own CMS ? lol) and thats when i started to smell rats.
After the presentation they go on about how they dont want to hire anyone, they only set up franchises. So they wanted me to start my own company and enter into their franchise so they wouldnt have to hire me.
I went for a cigarette and left without saying anything.
Admin
Regarding story #2, I've had to deal with kluges like that in the past.
A larger number of years ago than I can comfortably think about, I worked in an installation where we had a Cray X-MP running something called CTSS (Cray Time Sharing System) and a bunch of VAXes, which acted more or less as front ends to the Cray. We had a film recorder system running on one of the VAXes and the Cray was connected to that sysem by means of something called Hyperchannel, back when that was Hot Stuff.
The Cray would deposit image files to be recorded on the film recorder for movies.
As with the problem posed, there was no way of telling when the file was complete (or if it actually was complete, since the Hyperchannel connection would occasionally go wonky).
It's difficult to describe how ugly and complicated this became.
It's not a trivial problem. Believe me. I have the scars to prove it.
Admin
In Windows, moving files across drives (letters/partitions) is done via a copy and then a delete behind the scenes (look it up and/or try it with a large enough file - the article does talk about multi-GB files). As such, the file can appear in the directory listing of the destination before it is fully "there".
Posix or not, assumption is a big thing, too.
Admin
In Windows, moving (not renaming) files across drives (letters/partitions) is done via a copy and then a delete behind the scenes (look it up and/or try it with a large enough file - the article does talk about multi-GB files). As such, the file can (and does) appear in the directory listing of the destination before it is fully "there". Oh, and be sure to try that on different filesystem types, too.
POSIX or not, assumption is also a big thing.
Remember, my original point was that these kind of "only one right answer"-type questions often take a lot of assmptions for granted.
Admin
Actually he's very much looking at how the person approached the problem. He presented a scenario and refined it to see how the interviewee would respond to a changing scenario.
Instead of using the new information (no EOF, can't modify the watcher) to step back and rethink how he would approach the problem he continued to simply modify his solution despite the fact that it was getting increasingly convoluted, complicated and time consuming. The interviewer wanted to see if the employee had an open mind to changing his proposed solution. Instead the employee started with the assumption that the file cannot be moved and decided to just figure out harder ways to do that rather then step back and ask if his initial assumption was correct.
Admin
Yea, mv is way too complicated.
Admin
So what about the scenario where the downloader runs on server xyz. It downloads a file from stuff.com and puts it in a shared folder \xyz\files. The watcher runs on server abc which is entirely outside your domain of control, you are not allowed access to this server in any way thus there is no possible way to trigger the watcher.
Granted there are times when downloading it to a temp directory will not work but they are very very very few and far between. I would much rather use the very simple, very reliable solution and only make things more complex when it is required rather then using the more complicated and less usable solution as my default.
Admin
Why is it all you dolts that are suggesting locking/semaphores/IPC and so forth don't seem to understand that none of those will work unless you can modify both the Watcher and the Downloader, and you were expressly told that wasn't possible?
It isn't really that hard to understand. Let me try one more time:
You cannot use a solution that requires modifying both sides of the process because you're NOT ALLOWED TO MODIFY BOTH SIDES OF THE PROCESS!
Does that help you at all? If not, please find another career you (collectively speaking to all people posting solutions that fail to comprehend the above) are more qualified for, like flipping burgers, washing dishes, or digging ditches. Thanks so much for playing.
Admin
Which is why everyone keeps stressing that you move the file, not copy the file. Please pay attention.
Admin
Really, brainiac? You found a solution that requires modification to the Watcher after you were expressly told that wasn't allowed? Wow! You're an immediate hire! For the new job listing for a janitor, of course; you're obviously not qualified for anything more technical.
See Security down the hall for your pass. They'll know what areas you're not allowed to access.
Admin
Joel H. is a tool. He doesn't seem to know enough about operating and file systems to understand the difference between a copy and a move operation. Therefore, he came up with yet another moronic description of a problem that doesn't exist to justify why he too would have failed to pass Jeremy's easy interview question.
Admin
How about checking timestamps too? If the file is unchanged for a minute and the file size hasn't changed, it's probably done.
If it's really that big of an issue, I'd probably use lsof or fuser too.
Admin
Sorry, but you're wrong. Thanks for playing. Don't bother trying again.
(For an explanation of why you're wrong, read the other 100+ posts in this thread that tell you.)
Admin
You don't use id est ("that is") to introduce an example, you use it to restate the previous sentence more clearly and succinctly. You use exempli gratia ("By grace of example") to introduce examples, always, no exceptions.
Admin
I think we've discovered TRWTF:
"Developers who don't actually listen to the customer and produce what they think the customer wants, rather than what they asked for."
Hoorah! Tea and Scones all round!
Admin
And to break the loop: chmod FTW. With a slight exaggeration, that's what permissions are made for.
Admin
Assuming the watcher doesn't crash when it can't read a file due to permission issues.
Admin
Admin
1 Stop shouting. 2 Get over yourself. Just because you think your solution is better than everyone else's, that doesn't mean they should be flipping burgers. 3 It's perfectly reasonable to ask why e.g. the Watcher can't be modified. Why not? No really, why not? Because someone says so? Well big flipping burger deal, maybe he's wrong and should be washing dishes. And if it can't be modified then let's write our own - even you could do that, right? Why jump through hoops to comply with a crummy piece of software that doesn't do what you want, when it easily could do? Maybe you'll shout back that I'm not "solving the problem as stated". Well so what, I'm getting things done, not writing overcomplicated solutions for foolish problems, in an attempt to justify my arrogance.
Admin
What in the name of all that's holy do you think "mandatory" file locks are, exactly?
"Thanks so much for playing." ... indeed.
Admin
Normally i would ignore posts like yours, but you seem like a bitch and i'm in a bitchy mood as well, so there you go :)
Admin
The correct solution is pretty obvious. You have the watcher and downloader walk into a talent agency. The watcher says to the talent agent, "We have a really amazing act. You should represent us."
The agent says, "Sorry, I don't represent family acts. They're a little too cute."
The downloader says, "Sir, if you just see our act, we know you would want to represent us."
The agent says, "OK. OK. I'll take a look."
"First I come out, wearing a tuxedo, playing Brahms. Just as the music reaches a crescendo, the downloader in an evening gown runs on stage and undresses me before dancing provocatively on top of the piano.
Just as I finish playing the song with my cock, the downloader strips and does a backflip off the piano in a split on stage. Once her naked ass hits the floor, my 7 year old daughter and 13 year old son rush on stage juggling flaming lawn darts. the downloader does a handstand and catches the lawn darts in her cunt, she then manages to queef them out, making her the third part of this juggling act.
The queefs force her to squeeze out a few turds, which I eagerly start smearing on my naked body, which arouses me quickly. Once I'm fully aroused my daughter and son take turns blowing me while the downloader straps on a monstrous dildo and begins reaming each child while i ejaculate in the eyes of my offspring.
Once I cum, I run into the audience, shit-covered body still sticky with cum and grab my parents and in-laws to involve them into the act. I strip them all nude and instruct them to start a circle jerk while screaming racial slurs. So my father and my father-in-law start screaming, "Fuck the niggers" while mutually masturbating, and my mother and mother-in-law begin diddling one another and chanting, "I hate spics and jews!" Once they reach a geriatric climax, the downloader uses their ejaculate to lube up her fist which she uses to start fisting me.
As my asshole is violated, I start playing double dutch with my kids, and once they get tangled in the ropes, start a torrid 69. All the sucking and slurping cause my in-laws and parents to get aroused again and they start sodomizing and fisting one another.
The downloader at this point has completely started dry-heaving, so she vomits all over my ass and my back. I line up each of my family members who take turns licking the chunks of spew off my back and out of my ass.
By now my children have to defecate so I tell them to shit in each other's favorite orifices. My son, ever the trooper takes a thick, dense shit in his sister's vagina while my daughter shits in my son's nose.
My young daughter also conveniently starts her menstrual cycle shortly thereafter, and the menses and boy-shit in her cunt make for great lube, as each of my in-laws begin fucking my daughter. My son, blinded in shit, heads back to the piano and does his best Stevie Wonder impression while the downloader runs back into the audience to grab a toddler from the crowd.
She begins stuffing this child into her vagina, while my parents begin screaming how she's possessed by Satan and start performing a nude exorcism on her. The power of christ compels them to kill the toddler, which also makes it easier to cram into the downloader's lovehole.
By now, I'm so horny and aroused that I start fucking the dead baby inside the downloader while my young son starts licking my asshole and fingering his paternal grandparents. My in-laws finish abusing my daughter and start wrestling each other, which culminates in a huge powerbomb through the piano bench. The impact shatters my mother-in-law's hips, leaving her crippled.
The strain of the throw caused my father's bad heart to seize, and he collapses in a heap on the stage. As he gurgles and foams at the mouth, my daughter runs over and begins rubbing her shit covered pussy lips all over my crippled mother-in-law.
The downloader grabs the wooden shards of the piano bench and begins playing her father's dying body like a xylophone. My son pulls his tongue out of my asshole and begins sucking his dying grandfather's cock.
I diall 911 and call for the paramedics who revive my father-in-law and then take turns fucking my daughter and eating the menses and shit out of her tight cunt.
Once he's conscious we all assemble in a large circle holding hands and chanting gibberish before launching into a rousing group impression of 'A Downs Syndrome' perspective on the horrors of the holocaust, 9/11 and the bombing of Pearl Harbor.
As we're moaning and screaming, my son runs off-stage to get the family dog. The dog runs over to my crippled mother-in-law and begins peeing on her. Once the dog finishes leaving her in a puddle of piss, my daughter stops blowing the paramedics to light the dog on fire.
The dog yelps and howls before collapsing. My son runs over to fuck the burnt corpse while screaming, "White is right!"as my daughter begins goose-stepping around the stage, squeezing shit out of her cunt and offering Nazi salutes to the audience.
My father-in-law begins raping my father, claiming that he's doing it for the forgotten Vietnam vets and POWs. My downloader puts my crippled mother-in-law on her shoulders as I put the downloader on my shoulders and we play a game of naked chicken.
Once my son finishes fucking the dead dog. He takes the pieces of the piano bench and begins crucifying the corpse. Once the dog is hung like jesus, he begins weeping at the foot of the cross, saying, "Why my god have you forsaken me?"
My daughter mounts the top of the crucifix, using it as a wooden dildo. My parents, my in-laws and the downloader join hands at the center of the stage and start singing "The Hills Are Alive With The Sound Of Music"
I grab the lawn darts and shove one up everyone's ass before heading back to the piano to finish off the show with a rendition of Freebird."
For the longest time, the agent just sits in silence. Finally, he manages, "That's a hell of an act. What do you call it?"
And the watcher says, "The Aristocrats!
Admin
We aren't talking about windows, and we aren't assuming that it goes across partitions - you don't have to deal with that case, so don't.
Admin
And what if the two directories don't actually reside on the same partition?
Admin
Ah, I see this point has already been flogged to death
Admin
The program runs on Windows, so there is no user permissions, what do you do now?
Admin
This solution depends entirely on whether the file copy is doing it one file at a time (ie rsync), but you could simply watch the directory and just work on any fire that isn't the newest one. Yeh, so you have to change the watcher script, and you need to add a little logic to deal with the last file (have the watcher monitor if the file has changed size in x seconds/minutes/whatever, or just have the downlaoder script drop an empty file at the very end of its copy).
I just assume this would be simple but also avoid any potential issues like moving atomicity when your file system is partitioned in different ways.
Admin
Interviewee tries to answer. Interviewer quickly interjects: "... it's Windows CE specifically and you are about to experience a power outage in a few seconds and the server is on fire. What do you do now? Huh? Hypothetically..."
Halp!
:p
Admin
(And neither is "cp", I tried both in this scenario...)
OK - so on one of my Linux boxes I created a ~858MB file filled with zeros by doing a cat /dev/zero > test.bin and then doing a ^C after a few seconds.
That file is located on one LVM filesystem using ext3. The other filesystem is ext2 on a software RAID mirror. Using two terminal sessions I started the copy from the src directory on one session, and kept doing ls -laF in the other session which was in the dest directory.
The filename appears immediately and its size grows while the mv (or cp) is in progress.
Result: mv Is Not Atomic.
Details: uname -a output: Linux LCARS 2.6.25.6-55.fc9.x86_64 #1 SMP Tue Jun 10 16:05:21 EDT 2008 x86_64 x86_64 x86_64 GNU/Linux
directory listings while mv in progress:
If you doubt the results, how about trying a test yourself before posting? Oh, and Windows does something similar.Thanks!
Admin
Nice reference to Hypothetical Questions
Admin
Where in the OP was it indicated what assumptions are and are not valid? Or did you just assume that the mv was not across filesystems/partitions?
AGAIN...
"Only one right answer" questions need to include the conditions (assumptions) in the scenario. Otherwise, only the people with limited experience with complex setups will assume that the mv would be atomic because the file would not have to cross filesystems/partitions, or because they just do not know any better.
More experienced people, that know (for example) that /tmp should be on its own filesystem so that you do not get DoSed out of you own box by something filling up /tmp with garbage, will consider the way the real world works, and may not come up with the "one right answer."
Admin
Let's try again...
Nice reference to Hypothetical Questions
Admin
I already said on page 1 that malicious config was out. Both directories are on the same filesystem, so shut up about non-atomic moves. move the syscall is always atomic. period.
No, I stated it in my solution.
you're the one with a hardon for /tmp. don't use /tmp.