// Append the simulated printing time to the end of the file void MassStorage::RecordSimulationTime(const char *printingFilename, uint32_t simSeconds) { const char * const GCodeDir = reprap.GetPlatform().GetGCodeDir(); FileStore * const file = OpenFile(GCodeDir, printingFilename, OpenMode::append, 0); bool ok = (file != nullptr); if (ok) { // Check whether there is already simulation info at the end of the file, in which case we should replace it constexpr size_t BufferSize = 100; String<BufferSize> buffer; const size_t bytesToRead = (size_t)min<FilePosition>(file->Length(), BufferSize); const FilePosition seekPos = file->Length() - bytesToRead; ok = file->Seek(seekPos); time_t lastModtime = 0; if (ok) { ok = (file->Read(buffer.GetRef().Pointer(), bytesToRead) == (int)bytesToRead); if (ok) { lastModtime = GetLastModifiedTime(GCodeDir, printingFilename); // save the last modified time to that we can restore it later buffer[bytesToRead] = 0; // this is OK because String<N> has N+1 bytes of storage const char* const pos = strstr(buffer.c_str(), FileInfoParser::SimulatedTimeString); if (pos != nullptr) { ok = file->Seek(seekPos + (pos - buffer.c_str())); // overwrite previous simulation time } if (ok) { buffer.printf("%s: %" PRIu32 "\n", FileInfoParser::SimulatedTimeString, simSeconds); ok = file->Write(buffer.c_str()); if (ok) { ok = file->Truncate(); // truncate file in case we overwrote a previous longer simulation time } } } } if (!file->Close()) { ok = false; } if (ok && lastModtime != 0) { ok = SetLastModifiedTime(GCodeDir, printingFilename, lastModtime); } } if (!ok) { reprap.GetPlatform().MessageF(ErrorMessage, "Failed to append simulated print time to file %s\n", printingFilename); } }
static bool OpenDropboxHandler (struct Handler *handler_p, Resource *resource_p, MEM_FLAG resource_mem, const char * const mode_s) { bool success_flag = false; DropboxHandler *dropbox_handler_p = (DropboxHandler *) handler_p; if (mode_s) { const char *cached_filename_s = NULL; char *user_id_s = dropbox_handler_p -> dh_client_p -> t.secret; time_t last_modified = 0; const char * const protocol_s = GetDropboxHandlerProtocol (handler_p); if (*mode_s != 'w') { /* Get the timestamp of the file (if it exists) on the server */ if (!GetLastModifiedTime (dropbox_handler_p, resource_p -> re_value_s, &last_modified)) { printf ("Couldn't get last modified time for %s\n", resource_p -> re_value_s); } } cached_filename_s = GetMappedFilename (protocol_s, user_id_s, resource_p -> re_value_s, &last_modified); if (cached_filename_s) { success_flag = true; } else { /* * If we need to access the file, cache a local copy */ char *buffer_s = NULL; FlushCachedFile (dropbox_handler_p); buffer_s = (char *) AllocMemory (L_tmpnam); if (buffer_s) { FILE *temp_f = NULL; tmpnam (buffer_s); temp_f = fopen (buffer_s, "wb+"); if (temp_f) { void *output_p = NULL; int res; /* * If we're not creating a file from scratch, then download * any existing file from the server */ if (*mode_s != 'w') { res = drbGetFile (dropbox_handler_p -> dh_client_p, &output_p, DRBOPT_PATH, resource_p -> re_value_s, DRBOPT_IO_DATA, temp_f, DRBOPT_IO_FUNC, fwrite, DRBOPT_END); if (res == DRBERR_OK) { dropbox_handler_p -> dh_dropbox_filename_s = buffer_s; /* If we're not appending, then rewind to the start */ if (*mode_s != 'a') { rewind (dropbox_handler_p -> dh_local_copy_f); } success_flag = true; } /* if (res != DBERR_OK) */ else if ((res == 404) && (*mode_s == 'a')) { /* file doesn't exist, but it's ok as we're writing to it */ success_flag = true; } else { } } /* if (*mode_s != 'w') */ else { success_flag = true; } if (success_flag) { if (!SetMappedFilename (protocol_s, user_id_s, resource_p -> re_value_s, buffer_s, last_modified)) { printf ("failed to set filename for handler cache\n"); success_flag = false; } } /* if (success_flag) */ } /* if (temp_f) */ } /* if (buffer_s) */ } /* if (!cached_filename_s) */ } return success_flag; }
/* * nftw takes this as an argument about what to do * what we want is the filename which is in path */ int DirList(const char *path, const struct stat *ptr, int flag, struct FTW *ftwbuf) { isDirectory = 0; while (!uploadAll && updateFile && (fgets(tmp, sizeof(tmp), fp) != NULL)) { if (!strstr(tmp, path)) { printf("Not cached!\n"); return EXIT_FAILURE; } len = strlen(tmp)-1; if (tmp[len] == '\n') tmp[len] = 0; /* read filename and last modified time */ fileName = strtok(tmp, "\t"); cachedFileTime = strtok(NULL, "\t"); isDirectory = atoi(strtok(NULL, "\t")); /* !0 indicates a directory */ /* check if the file read from the file is valid or not */ if ((fp1 = fopen(fileName, "r")) == NULL) { printf("Invalid filename or "); printf("%s has been deleted since last caching.\n", fileName); /* DeleteFileOnServer(p); */ } /* if it exists compare its cached time to its current time */ else { fclose(fp1); /* get its latest modified time and check it to cached */ currentFileTime = GetLastModifiedTime((const char *) fileName); equalTimes = TimeComparsion(currentFileTime, cachedFileTime); if (equalTimes) { fileTransferSucceeded = NetworkConnection(fileName, isDirectory); if (!fileTransferSucceeded) { printf("\nFATAL: Transfer failed in updating %s.\n", fileName); return EXIT_FAILURE; } printf("File: %s has been updated.\n", fileName); } free(currentFileTime); } } /* print the filename and last modified date to .names */ if (updateFile && fp != NULL) { currentFileTime = GetLastModifiedTime(path); fflush(fp); fprintf(fp, "%s\t%s\t%d\tDirectory Level=%d\tFlags=%d\n", path, currentFileTime, ptr->st_mode &S_IFDIR, ftwbuf->level, flag); free(currentFileTime); } /* when no cached file exists */ if (uploadAll) { printf("uploading... %s\n", path); /* is directory? */ if (ptr->st_mode &S_IFDIR) isDirectory = 1; fileTransferSucceeded = NetworkConnection(path, isDirectory); if (!fileTransferSucceeded) printf("\nTransfer failed for %s.\n", path); } return 0; }