JcrPackage getSize() is always 0 - aem

In my code (AEM 6.1 SP 1) I build packages programmatically. All works fine, except that I want to log the package size - and whatever I tried, it was always 0.
Here's a current code snippet:
OutputStream out = new FileOutputStream(backupPath + "/" + fileName);
JcrPackageManager packageManager = packaging.getPackageManager(resolver.adaptTo(Session.class));
packageManager.assemble(jcrPackage.getDefinition(), listener, out);
logger.trace("Package size is {}.", jcrPackage.getSize());
I tried to close the package and reopen it afterwards, I committed the resource resolver, refreshed the session, but the result was the same.
Any ideas?

The way getSize works for JcrPackageImpl is that it looks for the "jcr:content" node on the path of the package and tries to get the property "jcr:data" and the length of this property value is returned in the size.
Can you confirm if the path/node you are trying to store the package is "jcr:content" node, My assumption is that its not, thats why you are getting 0 in the package size.

After much trial and error and the hints from Ameesh and Abhishek I found out what the problem was:
assemble(JcrPackageDefinition definition,
ProgressTrackerListener listener, OutputStream out)
does in fact not store the assembled package in the repository. That's why the size is 0. The package is passed to the OutputStream instead - which, in my case, saved the package to disk.

Related

Crash inside http_client constructor (Casablanca SDK)

I'm trying to use Casablanca to consume a REST api.
I've been following the microsoft tutorial, how ever i'm getting a crash and I cannot figure it out.
I'm using visual studio 2017 with C++11
I've codded a function GetRequest() that do work when used in a new empty project, but when I try to use it on my Project (Very big project with millions of code lines).
I'm crashing in the constructor of http_client, in the file xmemory0 line 118.
const uintptr_t _Ptr_container = _Ptr_user[-1];
This is a link to the callstack : https://i.imgur.com/lBm0Hv7.png
void RestManager::GetRequest()
{
auto fileStream = std::make_shared<ostream>();
// Open stream to output file.
pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
{
*fileStream = outFile;
// Create http_client to send the request.
http_client client(U("XXX/XXX.svc/"));
// Build request URI and start the request.
uri_builder builder(U("/IsLive"));
builder.append_query(U("q"), U("cpprestsdk github"));
return client.request(methods::GET, builder.to_string());
})
// Handle response headers arriving.
.then([=](http_response response)
{
printf("Received response status code:%u\n", response.status_code());
// Write response body into the file.
return response.body().read_to_end(fileStream->streambuf());
})
// Close the file stream.
.then([=](size_t)
{
return fileStream->close();
});
// Wait for all the outstanding I/O to complete and handle any exceptions
try
{
requestTask.wait();
}
catch (const std::exception &e)
{
printf("Error exception:%s\n", e.what());
}
}
EDIT : I just want to add that the http_client constructor is the issue. It always crash inside it no matter what I send as parameter.
The wierd thing is that it's not crashing when i just make a main() that call this function.
I guess it must be due to some memory issues, however I have no idea how could I debug that.
Does anyone would have an idea about it?
Thanks and have a great day!
I've experienced a similar issue on ubuntu. It works in an empty project, but crashes randomly when put into an existing large project, complaining memory corruptions.
Turns out that the existing project loaded a proprietary library, which is using cpprestsdk (casablanca) internally. Even cpprestsdk is static linked, its symbols are still exported as Weak Symbols. So either my code crashes, or the proprietary library crashes.
Ideally, my project can be divided into several libraries, and load them with RTLD_LOCAL to avoid symbol clashes. But the proprietary library in my project only accept RTLD_GLOBAL, otherwise it crashes... So the import order and flags become important:
dlopen("my-lib-uses-cpprest", RTLD_LOCAL); //To avoid polluting the global
dlopen("proprietary-lib-with-built-in-cpprest", RTLD_GLOBAL); //In my case, this lib must be global
dlopen("another-lib-uses-cpprest", RTLD_DEEPBIND); //To avoid being affected by global
"it will probably never concern anyone."
I agree with that.
I guess this issues was very specific, and it will probably never concern anyone, but still I'm going to update on everything I found out about it.
On this project, we are using custom allocator, if i'm not wrong, it's not possible to give our custom allocator to this lib, which result to many random crash.
A good option to fix it would be to use the static version to this lib, however, since we are using a lot of dynamic lib, this option wasn't possible for us.
If you are on my case, I would advice to use the libcurl and rapidjson, it's a bit harder to use, but you can achieve the same goal.

Can script globals be set from an invokable function?

I tried to set a script global from within whatever.rs:
uint32_t a = 2;
void set_a_from_float(float x) {
// more processing in real life
a = (uint32_t) x;
}
ScriptC_whatever w = new ScriptC_whatever(mRS);
w.invoke_set_a_from_float(3.0f);
Log.d("ContrivedExample:", ""+w.get_a()); // logs 2
This is a silly example and I know I can just use the automatically generated getters/setters, but this error still seems counter-intuitive.
Why wouldn't this work?
The reflected .java file will cache the initial value set in the script. If the value is updated from a .set() the cached value will be updated. .get() returns the cached value.
For performance reason we do not update the cached value when written from the script. To send a value back to a .java file you can either read back from an rs_allocation or use rsSendToClient*() from the script.

Where is the source code for Gadgeteer.StorageDevice?

I'm trying to debug some unexpected behaviour with the GHI Gadgeteer SDCard module whereby saving a file to an SDCard silently results in no file appearing.
The source code for the SDCard module is available (from the root navigate to Main/Modules/GHIElectronics/SDCard/Software/SDCard/SDCard_42/SDCard_42.cs). The line in my code that's not doing what I expect is
sdCard.GetStorageDevice().WriteFile("picture.bmp", picture.PictureData);
Looking at the GHI source code GetStorageDevice() is simple:
public StorageDevice GetStorageDevice()
{
return _device;
}
and _device is declared as
private StorageDevice _device;
Downloading the code I see that _device is of type Gadgeteer.StorageDevice. Where do I find the source code for that class?
SD Card Module
You must first mount the sdCard before you can use it:
sdCard.MountSDCard();
To make sure you see this, you should "wire up" the Mounted and Unmounted event handlers beforehand, though:
void ProgramStarted() {
sdCard.SDCardMounted += new SDCard.SDCardMountedEventHandler(sdCard_SDCardMounted);
sdCard.SDCardUnmounted += new SDCard.SDCardUnmountedEventHandler(sdCard_SDCardUnmounted);
}
void sdCard_SDCardUnmounted(SDCard sender) {
Debug.Print("The SD card has been unmounted");
Debug.Print("DO NOT try to access it without mounting it again first");
}
void sdCard_SDCardMounted(SDCard sender, GT.StorageDevice SDCard) {
Debug.Print("SD card has been successfully mounted. You can now read/write/create/delete files");
Debug.Print("Unmount before removing");
}
If none of these are your problems, I would suggest breaking down your GetStorageDevice() call as follows:
string rootDirectory = sdCard.GetStorageDevice().RootDirectory;
// What format is `picture`?
// I am going to assume System.Drawing.Bitmap for this example.
picture.Save(rootDirectory + "\\picture.bmp", ImageFormat.Bmp;
If you can not use the Bitmap.Save Method, you would use other conventional StreamWriter techniques.
I can not actually test this to see if it works, however, as I do not have one of these SD Card Modules. I just looked at the sample code on the SD Card Module Tutorial.
If it helps, vote it up. If it solves your problem, mark it as the answer.
I got an answer on the TINYCLR forum. It is in Main/GadgeteerCore/Gadgeteer42/Utilities.cs the current version (at the time of writing) is: http://gadgeteer.codeplex.com/SourceControl/changeset/view/24955#200043

How to delete a file while the application is running

I have developed a C# application, in the application the users choose a photo for each record. However the user should also be able to change the pre-selected photo with a newer one. When the user changes the photo the application first deletes the old photo from the application directory then copies the new photo, but when it does that the application gives an exception because the file is used by the application so it cannot be deleted while the application is running. Does any one have a clue how to sort this out? I appreciate your help
This is the exception
The process cannot access the file
'D:\My
Projects\Hawkar'sProject\Software\Application\bin\Debug\Photos\John
Smith.png' because it is being used by
another process.
//defining a string where contains the file source path
string fileSource = Open.FileName;
//defining a string where it contains the file name
string fileName = personNameTextBox.Text + ".png" ;
//defining a string which specifies the directory of the destination file
string fileDest = dir + #"\Photos\" + fileName;
if (File.Exists(fileDest))
{
File.Delete(fileDest);
//this is a picturebox for showing the images
pbxPersonal.Image = Image.FromFile(dir + #"\Photos\" + "No Image.gif");
File.Copy(fileSource, fileDest);
}
else
{
File.Copy(fileSource, fileDest);
}
imageIDTextBox.Text = fileDest;
First of all, you code is not good.
The new image is only copied if there is currently no image (else).
But if there is an old image, you only delete this image, but never copy the newer one (if).
The code should better look like this:
if (File.Exists(fileDest))
{
File.Delete(fileDest);
}
File.Copy(fileSource, fileDest);
imageIDTextBox.Text = fileDest;
This code should work, but if you are getting an exception that the file is already in use, you should check "where" you use the file. Perhaps you are reading the file at program start. Check all parts of your program you are accessing these user files if there are some handles open.
Thanks a lot for your help and sorry for the mistake in the code I just saw it, my original code is just like as you have written but I don't know maybe when I posted accidentally I've put like this. when the application runs there is a picturebox which the image of each record is shown, that is why the application is giving an exception when I want to change the picture because it has been used once by the picturebox , however I've also tried to load another picture to the picture box before deleting the original one but still the same. I've modified the above could if you want to examine it

How can I validate an image file in Perl?

How would I validate that a jpg file is a valid image file. We are having files written to a directory using FTP, but we seem to be picking up the file before it has finished writing it, creating invalid images. I need to be able to identify when it is no longer being written to. Any ideas?
Easiest way might just be to write the file to a temporary directory and then move it to the real directory after the write is finished.
Or you could check here.
JPEG::Error
[arguments: none] If the file reference remains undefined after a call to new, the file is to be considered not parseable by this module, and one should issue some error message and go to another file. An error message explaining the reason of the failure can be retrieved with the Error method:
EDIT:
Image::TestJPG might be even better.
You're solving the wrong problem, I think.
What you should be doing is figuring out how to tell when whatever FTPd you're using is done writing the file - that way when you come to have the same problem for (say) GIFs, DOCs or MPEGs, you don't have to fix it again.
Precisely how you do that depends rather crucially on what FTPd on what OS you're running. Some do, I believe, have hooks you can set to trigger when an upload's done.
If you can run your own FTPd, Net::FTPServer or POE::Component::Server::FTP are customizable to do the right thing.
In the absence of that:
1) try tailing the logs with a Perl script that looks for 'upload complete' messages
2) use something like lsof or fuser to check whether anything is locking a file before you try and copy it.
Again looking at the FTP issue rather than the JPG issue.
I check the timestamp on the file to make sure it hasn't been modified in the last X (5) mins - that way I can be reasonably sure they've finished uploading
# time in seconds that the file was last modified
my $last_modified = (stat("$path/$file"))[9];
# get the time in secs since epoch (ie 1970)
my $epoch_time = time();
# ensure file's not been modified during the last 5 mins, ie still uploading
unless ( $last_modified >= ($epoch_time - 300)) {
# move / edit or what ever
}
I had something similar come up once, more or less what I did was:
var oldImageSize = 0;
var currentImageSize;
while((currentImageSize = checkImageSize(imageFile)) != oldImageSize){
oldImageSize = currentImageSize;
sleep 10;
}
processImage(imageFile);
Have the FTP process set the readonly flag, then only work with files that have the readonly flag set.