Friday, 18 June 2010

Why its good to understand FileShare semantics

I got caught out by the same issue twice in a long period of time and judging by the Google results I was getting searching for the issue, its either not very common or I missed the plot completely.

The start of the story is my implementing log4net in an ASP.NET application hosted on godaddy. Now godaddy.com has the speciality of running ASP.NET apps under medium trust IIS. That poses some restrictions but its pretty straightforward to get log4net running using by using the log4net documentation. For a rolling log file appender you must remember that access is limited to folders under your application root. You'll have to set permissions to read/write for that folder in the godaddy.com hosting control panel.

The stumbling block for me came when I tried to use FileZilla to download the log files. FileZilla was erroring out with a "file in use, access denied error" no matter what. I went back to the log4net appender config and set up MinimalLock. Which means a lock on the file is held only for the period of a write by log4net. I thought great that would be the end of that, but I was still getting issues trying to download with FileZilla.

I then decided to switch to take a different tack and wrote an ASP.NET webpage that displayed the contents of the log file. My thinking was that it would be the same process accessing the file so it should be all good.

Now my code to access the log file from the webpage essentially boiled down to

File.ReadAllText(path)

I still got the access denied problem. I now decided to eliminate godaddy from the equation. I thought it would be a simple exercise initially, famous last words, so I ran the logging code directly on godaddy.com at first. To my surprise I could reproduce this locally.

It then occurred to me that I didnt really know in what mode File.ReadAllText was opening the file, though I had hoped it was with minimal restrictions; "read only". I took the file access into my own hands by doing something akin to:

new StreamReader(new FileStream(path, FileAccess, FileShare))

keeping to my idea of a minimalist thread I put in FileShare.Read. Even with the IntelliSense help it seems like a good idea -> all I want is to read, write ?

The problem still did not go away. I then decided to pull out the big guns and see whats really going on cause I was getting frustrated by this point. So I reached for procmon and monitored the file access calls. After looking at the output for a bit it dawned on me something I had cause to encounter a couple of years back.

The semantics of FileShare are that it defines how else you allow another component to open the file you are working with. Now log4net, the first open of the file, opened it with FileShare.ReadWrite. This means the file can be opened a subsequent time for reading or writing. The issue arose because of my paranoiac Read only view which was my initial knee jerk reaction to the access denied errors.

I was trying to open the file with FileShare.Read which meant that I tried (from the perspective of the OS) to mandate others could only open the file read only, which of course clashes with what is already set on the file initially by log4net i.e. FileShare.ReadWrite.


To summarize, if you get access denied errors accessing files from code that you are the author of then do not forget your sharing semantics.

Sunday, 13 June 2010

Germany over everything

Definitely over the Socceroos. After England's unconvincing performance, this was the first of the heavyweights coming out in emphatic fashion.

It was a very entertaining match that could have ended with an even higher tally for the Germans than the 4-0 it was. The Australians let Germany play free flowing attacking football with some very very nice combinations. Though they scored 4 goals it looked like they could still tweak some nuts and bolts especially when finishing. The "other" number 13, Mueller, would surely have been more clinical.

There are probably a million and one reports from the match, so I'll leave with one thought: it struck me as strange, though comforting, that the German national team at the World Cup had players called: Podolski, Miroslav, Oezil, Khedira, Cacau, Gomez and Marko. We have all come a long way.

In search of childhood - strudel

I sometimes try to recreate experiences from my childhood. Its not always easy and most times the experience is not the same. In one area though I do have consistent success and that is food. Trying to recreate what I saw my parents and grandparents make is something that has gone well.

It seems the farther I am from the places I grew up in the more I am inclined to bring back the tastes of those places, especially the way I remember them growing up. This time I take a stab at making a traditional apple strudel the way my grandmother used to make it. I do have my grandma's recipe at my disposal but the most important ingredient, flour, is so different here in California that I will hack the recipe. This recipe has been adapted to fit your screen in California.

Also if you are not comfortable flying by the seat of your pants, then this recipe is not for you as there are few exact measurements.

Ingredients

  • 8 sheets ready made filo pastry - I used the Safeway brand
  • a handful of raisins
  • 3 cooking apples - granny smith, bramley etc
  • 2 eating apples - fuji for example
  • plain breadcrumbs - this soaks up the apple juice, use more if you are using juicier apples
  • butter - about 1 stick, melted
  • raisins - soak in brandy or rum
  • walnuts or pecan nuts
  • sugar - brown or white use depending on how sweet your apple mixture is
  • cinnamon - 1-2 tbsp

Execution

  • Set your oven to 350F.
  • Peel, core and grate the apples. Use a coarse enough grate, you don't want apple mush at the start.
  • Add breadcrumbs, sugar, nuts, cinnamon and raisins to the grated apples. For me the other ingredients act as a complement and I will add them in appropriately small quantities. Its above all an apple strudel after all.
  • Follow the preparation instructions on your ready made filo pastry to get 4 sheets. Roll up half the apple mixture in the first 4 sheets and use the rest for the next.
  • Bake at 350 for 35 to 40 minutes.

Result

The end result I found was a good enough approximation of my grandma's apple strudel. The original 'pulled' pastry required for the strudel is a butter/margarine based thin flaky pastry much like filo. the last part of its preparation entails rolling it out and pulling at it by hand to make it thing enough. I recall vividly helping my grandma with this. It was always interesting to find out how thin I could make it go without cracking. Also the original would always call for walnuts, but I didn't have any at hand when I set off to make this.

Filo pastry makes the strudel a bit harder and more brittle than the original. I found that the strudel was better the next day as it had absorbed some moisture and the flakiness was reduced.